Thread: Static Vs Dyanmic Allocation

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    43

    Static Vs Dyanmic Allocation

    In cases were you have to return a pointer to memory that will not be modified outside its creating function, is it OK to use a static variable instead of calling malloc?

    I'm fine with using malloc when I don't know the amount of memory I need before runtime, however, in cases where I do, I feel its wasteful -and not to mention more error prone- to use the heap when a simple static var would work. On the other hand, I don't like calling bzero to clear the static object on each subsequent call. I feel it goes against the purpose of the static variable, which is to hold its state between

    calls.What do you guys think?
    Last edited by Dren; 1 Week Ago at 07:22 PM. Reason: Format

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,962
    Quote Originally Posted by Dren
    In cases were you have to return a pointer to memory that will not be modified outside its creating function, is it OK to use a static variable instead of calling malloc?
    It can be okay, but it is error prone. A common kind of mistake would be when the caller calls the function twice, possibly with the second call hidden inside another function call, not expecting the value to change (as it "will not be modified outside its creating function" after all), but it does! Related to this, such an approach would not be threadsafe.

    Quote Originally Posted by Dren
    I'm fine with using malloc when I don't know the amount of memory I need before runtime, however, in cases where I do, I feel its wasteful -and not to mention more error prone- to use the heap when a simple static var would work. On the other hand, I don't like calling bzero to clear the static object on each subsequent call. I feel it goes against the purpose of the static variable, which is to hold its state between calls.What do you guys think?
    Use a fixed size array instead then, one that is passed to the function as a pointer to its first element.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    43
    Great, I followed your advice and passed a pointer to a storage array in most circumstances.In the few cases where this would prove inconvenient I used the static method, but reset its elements to 0 before using it, so I know it won't have left over data.

    Many thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C static variables + dynamic memory allocation
    By dralexpe in forum C Programming
    Replies: 3
    Last Post: 06-25-2012, 06:58 PM
  2. Replies: 7
    Last Post: 11-18-2011, 07:49 PM
  3. Static memory allocation
    By jimhuf in forum C Programming
    Replies: 5
    Last Post: 10-07-2010, 11:34 AM
  4. Static Memory allocation
    By p3rry in forum C Programming
    Replies: 25
    Last Post: 12-23-2008, 08:30 AM
  5. dyanmic array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2002, 03:53 AM

Tags for this Thread