Thread: Questions about releasing memory...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    22

    Questions about releasing memory...

    Hi there,

    I have three questions.

    1. What is the difference between using:

    Code:
    const char varchar[] = "this is an example";
    and

    Code:
    const char * varchar = "this is an example";
    In both cases do I need to use the free function to release memory? I mean,

    Code:
    free(varchar);
    2. Do I need to release memory if I define this line?

    Code:
    int array[4]={1,2,3,4};
    then,

    Code:
    free(array);
    3. What if I wanted to release a variable of type int from the memory? For instance,

    Code:
    int varint = 4;
    Thank you!

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You don't need to, and should not free memory in any of these cases.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    This
    Code:
    const char varchar[] = "this is an example";
    allocates space for the length of the string literal (plus one) to the varchar array and then copies the string literal into that space.
    This
    Code:
    const char * varchar = "this is an example";
    only allocates space for a pointer-to-char and sets that pointer to the address of the string literal.

    You only call free() to free memory that you've dynamically allocated (with malloc or calloc, for example). If you define the variables you've shown inside a function, then they are automatically allocated in the stack frame for that function and automatically deallocated when the function ends. If you define them globally (or statically in a function) then they are automatically allocated when the program starts and automatically deallocated when the program ends. (Actually, all memory that a program uses should be automatically deallocated when the program ends, even if it was dynamically allocated.)
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by oogabooga View Post
    This
    Code:
    const char * varchar = "this is an example";
    only allocates space for a pointer-to-char and sets that pointer to the address of the string literal.
    It should be noted here that the string literal is actually allocated as well. The thing about string literals though is that this:
    Code:
    char *p = "hello";
    char *q = "hello";
    These may or may not actually be pointing to the same spot in memory.


    Quzah.
    Last edited by quzah; 04-07-2012 at 10:14 AM.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    Dear Subsonics,


    So this means that I only need to release memory when it was allocated dinamically?


    Thank you for reply.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    Dear oogabooga,

    Thank you very much for reply. Everything is clear now!

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    Dear quzah,

    Thanks for the clarification.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by adarpodracir View Post
    Dear Subsonics,


    So this means that I only need to release memory when it was allocated dinamically?


    Thank you for reply.
    Yes, only when you explicitly ask for a chunk of memory (malloc() etc.) or are using a function that does (strdup() for example).

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You only use free for something that you allocated using malloc, calloc, or realloc.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by iMalc View Post
    You only use free for something that you allocated using malloc, calloc, or realloc.
    No, as Subsonics mentions WRT strdup, it is not unusual for library functions to return pointers which also need to be freed (which is why you should pay attention to API documentation).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by MK27 View Post
    No, as Subsonics mentions WRT strdup, it is not unusual for library functions to return pointers which also need to be freed (which is why you should pay attention to API documentation).
    That is because such library functions, directly or indirectly, allocate memory using malloc(), calloc(), or realloc().

    So iMalc is correct. As is Subsonics, with the caveat that strdup() is also not standard C. Despite the fact that, on systems that support it, it is often placed in the <string.h> header.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Questions on Memory
    By LyTning94 in forum C++ Programming
    Replies: 14
    Last Post: 03-24-2011, 01:29 PM
  2. Replies: 4
    Last Post: 11-10-2009, 06:47 PM
  3. Memory Questions
    By John_L in forum Tech Board
    Replies: 11
    Last Post: 10-07-2008, 06:35 PM
  4. threads not releasing memory?
    By Spark in forum C Programming
    Replies: 3
    Last Post: 06-08-2002, 02:09 PM