Thread: using malloc multiple times on one variable

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    using malloc multiple times on one variable

    I have a char* Log_String. Throught the course of my program the string Log_String points to will change. Is it better to use malloc each time I assign different string to Log_String or is it better to use malloc once at the beginning and allocate enough memory to handle the largest expected string?

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    it depends on a lot of things, like the variance between the largest possible string and the average or smallest, memory requirements and speed requirements. it's always going to be a trade off, so pick the trade off that works best for your situation.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    I'm assuming using malloc a bunch of times is slower but uses the least amount of memory at any given time...

    If I use malloc several times, do I need to free to variable first each time?

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    calling malloc is slower, but allocating a big block one time wastes memory. that's the trade off.
    Quote Originally Posted by Bladactania
    If I use malloc several times, do I need to free to variable first each time?
    yes

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Thanks, as always.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You could also try malloc(), then a bunch of realloc() calls then one free() when you don't need it anymore.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Bladactania View Post
    I have a char* Log_String. Throught the course of my program the string Log_String points to will change. Is it better to use malloc each time I assign different string to Log_String or is it better to use malloc once at the beginning and allocate enough memory to handle the largest expected string?
    A simple approximate rule is to start with a smaller buffer, and expand it when you need to, but never shrink it. The idea being that if you've seen a large piece of data once, you're likely to see another large piece of data again.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Yes but what if I see a large piece of data at the very beginning and very end but only small parts inbetween? in a short program that might not matter, but my program might be running for many hours.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In that case, you may be using memory that you ended up not needing (but you didn't know you wouldn't need it for a while). Also, the odds that the OS will actually get the memory back, even if you free it, isn't that high either. (You don't get just 12 bytes if you ask for 12 bytes, there is some minimal allocation size almost always.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring multiple character variable.
    By rajabadsha in forum C Programming
    Replies: 6
    Last Post: 05-12-2006, 12:15 PM
  2. Consuming same Web Service multiple times
    By cfriend in forum C# Programming
    Replies: 2
    Last Post: 01-10-2006, 09:59 AM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM
  5. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM