Jump to content
  • Advertisement
Sign in to follow this  
Alpha_ProgDes

Quick pointer in function question

This topic is 5009 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

would you do this to free a newly allocated pointer that resides in a function.
char* returnString () {   //used for example purposes

  char * string = malloc (16*sizeof(char));
  string[0] = 'a';
  string[1] = 'b';
  string[2] = '\0';

  return string;
}

int main () {

   char * example = returnString();
   free (example);  // question about this too. is that right? and why?
   free (returnString());  // here's my attempt to free the malloc'd string in the function

   return 0;;
}

please give wise counsel. thank you.

Share this post


Link to post
Share on other sites
Advertisement
char * example = returnString();
free(example);
Doing free(returnString()) would free different allocated memory every time it was called (and wouldn't let you use the allocated memory/string anyway).

All that said, this isn't terribly good practice. Sometimes it's unavoidable (in C), but as much as possible you want to minimize the amount of cleanup your user's code has to perform. This approach scatters memory management all over the place. One alternative is to perform pool allocation, releasing the entire pool at a later time (when the program exits or the system is terminated).

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
char * example = returnString();
free(example);
Doing free(returnString()) would free different allocated memory every time it was called (and wouldn't let you use the allocated memory/string anyway).

All that said, this isn't terribly good practice. Sometimes it's unavoidable (in C), but as much as possible you want to minimize the amount of cleanup your user's code has to perform. This approach scatters memory management all over the place. One alternative is to perform pool allocation, releasing the entire pool at a later time (when the program exits or the system is terminated).


What is a pool allocation and how do you do it?


Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
would you do this to free a newly allocated pointer that resides in a function.


Here's the thing - except for 'static' variables, pointers - nor fresh allocations that they point at - can meaningfully be said to 'reside in a function'. When returnString() is called, there is a temporary pointer 'string' on the stack, which eventually makes its way into the return value, but there is no permanent storage there. And there is a new allocation of memory off somewhere in the distance, at the location indicated by the pointer, but it doesn't really 'belong' to anyone - it's on the heap - sometimes called the "freestore" - for good reason.

Thus:

char * example = returnString();
free (example); // question about this too. is that right? and why?

That's 'right' (it works), because 'example' is now a copy of the pointer value (and 'string' no longer exists; it has fallen off of the stack) where the allocated memory is. Thus you can call free() through it.

That said, here be dragons - you need to be careful about who is responsible for cleaning what up. In garbage-collected languages like Java, you can return new objects with impunity; coming back to C or C++ can be a bit of a shock. It requires different design philosophy, really.


free (returnString()); // here's my attempt to free the malloc'd string in the function


This re-calls the returnString function, creating another memory allocation, and frees that allocation before anyone can do anything with it. So it's "correct", but ultimately useless. You see what I mean about the allocation not "residing in the function" - you can't idiomatically say "free the memory allocated by this function", because it allocates every time it is called, and it could be called more than once.

Since there was a question about pool allocation: This basically just means having some collection of "empty objects" somewhere, and fetching one out of "storage" when needed. In C, the container might be managed as a linked list of char[16]'s, or as a dynamically resized char[16*NUM_ELEMENTS] with pointers being returned to every 16th byte (ouch! Here *really* be dragons, with array bounds safety and so forth...)

Share this post


Link to post
Share on other sites
Pool allocation:

You basically write your own malloc.

You have say an array, with 1M elements.

You also have a bit of accounting so you know which elements have been freed, and which are still needed. And how many are used.

For eg. you need 253 itemsd.

You go up to the memory manager and go "Yo, give me a pointer to my 253 items!"
And the memory manager goes and gets the 253 items. (sequencially in the array) and gives you a pointer to them.

Now What you do, is you keep track of which elements have been deallocated, which are allocated, and you try to keep all the allocated stuff together, with no unallocated gaps.

You also keep track of who has what meory where, so you can keep reusing elements. You can also go and deallocate all the memory a funtion/subsystem/x has (you can do this w/ bitwise logic.) when called for.

How to do this?
You set up a MAC for each function

Its SSSMMMMMUUUUUU, now when you are requested to do deallocation for a unit, it gives its unit adress.

So you & off all the rest, and compare it to your records. If its a match, then go.

Now using this you could say get an entier subsystem to deallocate all its memory at one. Nifty Heh?

Now you might be asking yourself, now what happens when that memory runs out?
What you do, is you allocate new memory Twice as big as the memory you currently have. you then do a memery copy, and free the memory you were occupying.

All in all, its a pretty good system. So for eg. you can have a restricted acess function:
Acess(*p, *id) Which only works if your allowed to acess p. (so one function can't overrite data from another function.

Nifty Huh?

Any questions on this system. Pm away. Or just post. either way's fine.
From,
Nice coder

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!