Memory Allocation/Freeing Problems

This is a discussion on Memory Allocation/Freeing Problems within the C Programming forums, part of the General Programming Boards category; I am creating a buffer for a simple compiler. Essential all this program does is take chars that are taken ...

  1. #1
    Unregistered
    Guest

    Memory Allocation/Freeing Problems

    I am creating a buffer for a simple compiler. Essential all this program
    does is take chars that are taken from a .txt file and stores them into
    memory. The 'buffer' is a char* and memory is allocated for it using malloc
    and is also reallocated. The problem is is am trying to free the memory
    used by this char* when exiting the program. I am using the following code
    to free the memory:

    free((void *) pBd->pcb_head);
    pBd->pcb_head = NULL;

    After I have done this the characters still reside in memory which messes up
    the program when I try to execute it again. Anyone have any thoughts on
    what I am doing wrong?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    After you call free(), the OS is allowed to reallocate that memory as it sees fit. If it doesn't give it to another program immediatley, yes, your data will still be there, but that's not your concern anymore.

    As you are having problems, I'd suggest you post a snippet of code where the program crashes (or whatever), and someone will have a look for you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Unregistered
    Guest
    the problem is that when I run the buffer again after it has terminated the old data appears in the memory my char* is pointing at. Even if I pass the buffer an empty file. When I print the char* the old data comes out with the new.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you've free()'d and set the pointer to NULL the first time round, then malloc()'d more memory, then yes, at this point the pointer could well be pointing at the same memory location. But, you should never trust memory that you've just malloc()'d to be anything specific. If you want to be sure it's clean, you can use calloc() instead of malloc(), or you can memset() it.

    You shouldn't be doing this kind of thing:
    Code:
    char *ptr;
    ptr = malloc(20);
    printf ("%s", ptr);  /* Bad, we don't know what's at ptr's location */
    Hope this helps. If not, I'd really need to see some of your code to advise you better. If the program is large, just pick some pertinent bits, and we'll go from there.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Unregistered
    Guest

    Thumbs up

    My problem is now solved with using calloc to initially allocate memory for my char* on the heap. For some reason when using this function I don't get the artifacts in the buffer when I run the program the second time. Thx for help!

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    > My problem is now solved with using calloc
    It would be more correct to say your problem has been side-stepped by using calloc

    calloc is more expensive than malloc, and comes with a health warning

    You should really find out where you're doing something like this say
    Code:
    char *p = malloc( 100 );
    strcat( p, "hello" );  // bad code, *p is uninitialised
    Where it would have been much better to do this
    Code:
    char *p = malloc( 100 );
    *p = '\0';  // an empty string
    strcat( p, "hello" );  // good code, *p is initialised
    However, perhaps you really meant to
    Code:
    char *p = malloc( 100 );
    strcpy( p, "hello" );  // good code, *p is initialised by copy
    Now sometimes you do want to clear the whole array, and calloc is the correct thing to do. If you're just after an empty string, then calloc is very expensive compared to just initialising the first byte to 0.

    > Even if I pass the buffer an empty file
    Perhaps you're using while ( !feof() ) in your code

  7. #7
    *
    Guest
    Allow me to state the obvious for our unregistered novice:

    An empty file has no data in it. Zero bytes in length. So nothing goes into the buffer.

    What do you think is in RAM? All pretty little zeroes?! No, RAM is filled with whatever was last in it from whatever previously used that space.

    If you want your memory blocks cleaned out prior to use, _you_ have to do that. Try using a loop to put a 0x00 in every location in your buffer before you use it.

    sheesh

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation/reallocation
    By magda3227 in forum C Programming
    Replies: 10
    Last Post: 07-04-2008, 03:27 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Weird memory problems (MS-VC++)
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2006, 08:01 AM
  4. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 01:15 AM
  5. stl::vector problems (memory?)
    By aker_y3k in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2003, 11:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21