Question about structures and pointers

so, guys, I have a question. here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

using namespace std;

struct character
{
	int age;
	char gender;
	char *name;
};

int main()
{
	character *hero;

	hero = new character;

	hero->age = 20;
	hero->gender = 'M';
	hero->name = "David";

	delete hero;

	system("pause");

	return 0;
}


is it safe to create a pointer to a struct that has a pointer as a member and then delete it? I'm asking this because I'm not allocating any memory to the pointer "name". won't it cause some memory leak, even after deleting "hero"?
Last edited on
You need to allocate memory separately. By the way, you should use strcpy to copy a string to it.


http://www.cplusplus.com/reference/cstring/strcpy/
what you mean, by allocating memory separately, is adding
hero->name = new char[64];
, right? and I guess
delete hero;
will do the rest, right?

and what do you mean by use strcpy?
you mean that I should create a new char[64] variable instead, and then copy it to hero->name instead?
Is there a reason you're not allowed to use std::string?
no reason. I only want to learn more about it. maybe someday I'll bump into it for whatever the reason.
No, you'll have to delete the allocated data.

With char pointers you shouldn't really have to allocate any memory if you're hardcoding a value like that to it, however if it's anything else you'll have to.

I'm agreeing with LB here, you should use std::string.
Last edited on
ok, I got it. I'll use string. but just for learning purposes, now, if you can tell me... why won't this work?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

using namespace std;

struct character
{
	int age;
	char gender;
	char *name;
};

int main()
{
	character *hero;

	hero = new character;

	hero->age = 20;
	hero->gender = 'M';
	hero->name = new char[64];
	hero->name = "David";

	cout << hero->name << " : " << hero->gender << " : " << hero->age << endl << endl;

	delete hero->name;
	delete hero;

	system("pause");

	return 0;
}


the program crashes now that I added
delete hero->name;
Try something like this. Just remember, you must be careful while dealing with pointers, because they need to be allocated and deallocated. In my example below, i've done it inside its constructor//destructor. Regarding to your crash, it is because you're trying to point to a literal instead of a pointer. That's why we use here "strcpy".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include <iostream>
#include <cstring>



struct character
{
    int age;
    char gender,*name;

    character()
    {
        name = new char[64];
    }
    ~character()
    {
        delete[] name; { name = nullptr; }
        gender = 0;
    }

    void display()
    {
        printf("Age:%d\nGender:%c\nName:%s\n",age,gender,name);
    }
};

int main()
{
    character *hero;

    hero = new character;

    hero->age = 20;
    hero->gender = 'M';
    strcpy(hero->name,"example");


    hero->display();

    delete hero;
    return 0;
}
Last edited on
On line 21, you change the name pointer from pointing to the block of 64 characters to instead pointing to the string literal "David". You have a memory leak and then you try to call delete[] on the string literal, which crashes your program.

C-style strings are the absolute most horrible thing to learn this early in C++. Postpone your learning of them for a year or two. They're almost never used in C++.
Last edited on
As I said before, bufige's memory allocation is unnecessary. Simply assign it to "David" without deleting it. It isn't required to allocate memory dynamically here.
Indeed it is. But what the OP wants to learn how to deal with pointers,most probably, to reuse them somewhere. That's what i think from it's first post, and that is what i'm trying to point him.
Last edited on
Yes, but this scenario isn't an ideal one. I could think of several that would be much better.
thanks, guys :)
thank you bufige for showing me how to do it.
thank all of you for advising me to use string
I guess I have to wait until I learn more about C for this kind of stuff
and that's not gonna happen in the near future
I'll just use string afterall ^_^
Topic archived. No new replies allowed.