Jump to content
  • Advertisement
Sign in to follow this  
Thevenin

Structures [Very quick question].

This topic is 4837 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I have a structure like so..
struct gtTest
{
char lacString[50];
}goTest;
Can I....
free(goTest.lacString);
goTest.lacString = calloc(100, sizeof(char));

Share this post


Link to post
Share on other sites
Advertisement
No.

You can however...

struct gtTest
{
char *lacString;
}goTest;

...

goTest.lacString=(char *)malloc(50*sizeof(char));

...


free(goTest.lacString);
goTest.lacString = calloc(100, sizeof(char));

Share this post


Link to post
Share on other sites
You will sometimes get very strange run-time errors if you do something like:
char a[50];
free( a );
or
delete [] a;

It's equivalent to doing:

free( "a static string" );

Technically you're passing a pointer to free(), but it's a pointer to memory that's owned and initialized by the operating system (i.e. part of the .exe), not the program's heap. It's similar to self-modifying code and it tends to cause freakouts :)

Share this post


Link to post
Share on other sites
Even if you pass something to free that wasn't dynamically allocated, the system will try to look for a header a few bytes earlier than the address you pass... If that's not a heap header, you're hosed.

Share this post


Link to post
Share on other sites
Would it not be on the heap?

o.O

I question how I haven't ran into a stack overflow, since I allocate 500,000 such objects...

Edit: Oh erm... I do it like such...

struct gtTest *gaoTest;

gaoTest = calloc(500000, sizeof(struct gtTest));


In this case, it should most certianly be on the heap correct?

Share this post


Link to post
Share on other sites
yes, it's on the heap IF the object it's in is on the heap

... but in your initial example, the item goTest is not declared on the heap ... nor the stack, it is declared in the data segment of the program.

In your example of creating a big block of these items, all of their memory is on the heap, but they are not individual allocations from the heap ... so they cannot be individually returned ... you can only return the whole block when you are done with it.

Share this post


Link to post
Share on other sites
Ok, than I will use the "char *lacString;" method xor pointed out, since I need to be able to resize the string.

Thanks everyone! [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!