Thread: dilemmas posed by c FAQs

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    19

    dilemmas posed by c FAQs

    Dear Board

    I am trying to tackle memory allocation/leakage in my program. I have functions which I need to pass arrays of strings and strings. In general the string sizes, array sizes, and sizes of element strings of arrays are not fixed. I prefer to allocate them dynamically unless I could not figure out a way. If I fail at dynamically allocating I might try to allocate these to the maximum size possible but I prefer not to do that horrible thing.

    These functions are called repeatedly inside a loop from the beginning to the end of the program existence and in general this program could be up forever. Briefly it's trying to
    read a database log file and shipping transactions constantly.

    When it works properly it's leaking memory like crazy. When it does not it seg faults.

    Question 7.22

    The above FAQ says that I must free any pointer that's being called by malloc.

    My conclusion is that if that is true then it's impossible to pass dynamically allocated variable to C function.

    Neither of the following would work according to this faq.

    Code:
    char *abc (){
    
    char *a;
    a=malloc(10);
    *a="abcd";
    return a;
    
    }
    
    
    void abc(char *a) {
    
    a=malloc(10);
    *a="abcd";
    
    }
    Consider another faq

    Question 7.22

    This says that I must also free local pointer. How am I supposed to make above two functions work if I must free what I want to return? Isn't "a" from two examples I posted considered local pointer?

  2. #2
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    Notice that "10" above is a variable in the real program.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Your not suppose to free a in your abc() function, I presume that what is meant is that you are responsible for freeing it, ie in main or what ever function that make use of 'a' once you don't need it anymore.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    You "presume" mean you are not sure of your answer?

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Notice also that you can not do this:

    Code:
    a=malloc(10);
    *a="abcd";
    You need to use strcpy or some similar function to achieve that.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by mr.wu View Post
    You "presume" mean you are not sure of your answer?
    No it means that is the only way I can make sense of it. If you free a in the function creating it, you can not use it. Though I can not be sure what the author of the tutorial had in mind.

    Look:

    Code:
    char *abc (){
    
            char *a;
            a=malloc(10);
            strcpy(a, "abcd");
            return a;
    
    }
    
    int main(int argc, char **argv)
    {
            char *c = abc();
            puts(c);
            free(c);
            return 0;
    
    }

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    if i don't free it and that function is called millions of time, can i be sure that there is no memory leak?

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You are returning a pointer to the allocated memory, that you ARE freeing, but after you have used it obviously! Try freeing it then returning it and see how that works for you.

    Edit: The last sentence is me being sarcastic, don't free memory before you use it. If you call your function one million times you are responsible for calling free() one million times. When you do it depends on when you are done with it.
    Last edited by Subsonics; 06-24-2010 at 05:55 PM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Think of malloc as getting a dish from the cupbord.
    Think of free as putting the dish in the sink.

    Some place in there, you want to eat off of that dish. So naturally, you would eat off of it after you get it from the cupbord, but before you put it in the sink. Pretty simple.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    That's easy to understand but it's not clear what happens when pointers are passed in the functions. As you see in the FAQ any local pointer allocated must be freed.

    Thank you both for your informative replies.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Everything in C is passed as a copy. A pointer is just a variable, like every other variable, that holds a value. The value a pointer holds represents a spot in memory. When you call free with some pointer, you're telling it: "This spot of memory isn't being used any more."
    Code:
    char *p;
    p = malloc( SOMESIZE );
    ...
    myfunction( p );
    All we're doing here is passing the value that p has, to the 'myfunction'. If you free it inside that function, then you can't attempt to use it anymore outside the function. It's perfectly fine to free inside a function, but you have to remember that you're doing so. You don't free twice, just once. So if you free inside some other function, then you don't free back where your variable was first declared.

    You can use the method where you free the memory in the same block that you allocate it:
    Code:
    char *p;
    ...
    p = malloc( SOMESIZE );
    ...
    dostuffwithp(  p );
    ...
    free( p );
    It makes it clear that way, that this function has taken care of everything it has created. It created it, it gets rid of it. That may not always be practical in your program, but it's not a terrible idea.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    Thanks. That helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. faqs question
    By St0rM-MaN in forum C Programming
    Replies: 7
    Last Post: 06-17-2007, 11:42 AM