Thread: Memory Allocation and Structs

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    6

    Memory Allocation and Structs

    When I allocate mem for a struct pointer then free it, should I be able to still access data inside it after the call to free()? How can I tell for sure that I'm free'ing it properly?

    Here's a bit of code to illustrate what I'm talking about...

    Code:
    typedef struct {
    
       int some_data;
    
    } LIST;
    
    
    function {
    
    LIST* item = NULL;
    
    
    item = malloc(sizeof(LIST));
    
    item->some_data = 33; 
    
    
    free(item); 
    
    printf("some_data = %d \n", item->some_data);   // should I still be able to access this after the free?
    
    
    }

    Thanks for looking

  2. #2
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Here's your code, but compilable:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct
    {
    	int some_data;
    } LIST;
    
    
    int main(void)
    {
    	LIST* item;
    
    	item = malloc(sizeof(LIST));
    
    	item->some_data = 33; 
    
    	printf("some_data = %d\n", item->some_data);
    	
    	free(item); 
    
    	printf("some_data = %d\n", item->some_data);   // should I still be able to access this after the free?
    
    	return 0;
    }
    Here's my output:
    Code:
    some_data = 33
    some_data = 0

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Accessing memory that has been freed is Undefined behaviour [1]. There is no guarantee that it will work, or that it will NOT work. It depends on the implementation of the compiler and if the block of memory gets reused. For example this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct
    {
    	int some_data;
    } LIST;
    
    
    int main(void)
    {
    	LIST* item;
            LIST* item2;
    
    	item = malloc(sizeof(LIST));
    
    	item->some_data = 33; 
    
    	printf("some_data = %d\n", item->some_data);
    	
    	free(item); 
    
            item2 = malloc(sizeof(*item2));
            item2->some_data = 42;
    
    	printf("some_data = %d\n", item->some_data);   // should I still be able to access this after the free?
    
    	return 0;
    }
    is almost guaranteed to produce the output of:
    Code:
    some_data = 33
    some_data = 42
    I say almost guarnteed, because there is no guarantees as to what happens when you allocate another block of the same size that you just freed - the most common scenario is that the last freed block is used.

    [1] I just wrote that article on cpwiki. If anyone fancies enhancing or extending it [it could get VERY long], feel free.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    Thanks for the quick replies... I understand whats going on now and am no longer paranoid that I'm not freeing mem properly

    JDGATX I guess you got what I think is the desired behaviour, thanks for checkin that out!

    matsp very informative I'll have to check out your article, thanks.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is also why it's custom to set a pointer to NULL after freeing it so you know it's freed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by reversaflex View Post
    JDGATX I guess you got what I think is the desired behaviour, thanks for checkin that out!
    If you do the same on a Visual Studio Debug build, you will get a HUGE integer (possibly huge negative) value back from your freed memory, because the runtime library fills the freed memory with a special value to indicate it's been freed. (I think it's 0xDFDFDFDF - which is minus a few billion or as an integer). It appears that JDGATX's build fills that part of memory with the value zero - whether that applies to ALL of the freed memory or just the first 4 bytes or so is another question.

    But it's undefined as far as the C standard is concerned, and as such, there is absolutely no telling what happens [including a possible crash].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If we want to be correct, it may work and it may not. If all data within the page has been previously freed (or not allocated at all) and when you free the rest of the data inside the page, it will get unloaded from the process and you'll get an access violation (because the process won't have access to the page anymore).
    However, the process may still have data still allocated within said page, which means it will still be able to access and write to the location. So, it's undefined behavior.
    Curious about virtual memory? Have a look at cpwiki's virtual memory article.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    If we want to be correct, it may work and it may not. If all data within the page has been previously freed (or not allocated at all) and when you free the rest of the data inside the page, it will get unloaded from the process and you'll get an access violation (because the process won't have access to the page anymore).
    However, the process may still have data still allocated within said page, which means it will still be able to access and write to the location. So, it's undefined behavior.
    Curious about virtual memory? Have a look at cpwiki's virtual memory article.
    <pedant>As discussed in a previous thread, the malloc/free functions are optimized, so the system will not free the memory back to the OS unless it REALLY thinks there is a huge gain (e.g. you have freed many megabytes of memory in consecutive pages). So even if you free every memory allocation on a particular page, it's unlikely that it leads to an access violation later on</pedant>

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is dynamic memory allocation
    By dbzx in forum C Programming
    Replies: 7
    Last Post: 06-08-2009, 08:11 AM
  2. Structs and mem allocation
    By reversaflex in forum C Programming
    Replies: 2
    Last Post: 07-03-2008, 07:46 AM
  3. Mysterious memory allocation problem
    By TomServo1 in forum C Programming
    Replies: 7
    Last Post: 07-08-2007, 11:29 AM
  4. dynamic memory allocation problem
    By firyace in forum C Programming
    Replies: 4
    Last Post: 05-23-2007, 09:57 PM
  5. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM