Thread: things about realese memory for characters array and array of char array

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

    Angry things about realese memory for characters array and array of char array

    im stuck in the memory problem

    1,
    Code:
    char string[10];
    while (fgets(string, 10, stdin) != NULL)
    { 
    //printf string
    
    // printf string[7]
    
    } 
    then, string will equal to every line from user stdin,
    suppose:
    i tried to stdin two times, first time, i input "hello ha\n" first, then print string => "hello ha"
    then, if i input "hello\n", the print out string is "hello", this is fine, but if i print string[7], the output is 'o'.. is this because the original memory didn't released, so the index 7 still have the previous value?
    but if this is why, then why the output if print string is "hello"?

    2, talking about memory, is there any formal way to release previous array memory, for example, if im dealing with char matix[0][0] an array of characters array, how to clean all the value i assigned to matix before, then i can assign new value to it again

    ~~~~!!!!!!~!~!~!~!~!~!~! looking forward some replys

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Since the memory is statically allocated it will only be deallocated after that execution block is complete (i.e function) . If you allocated memory dynamically you could control when to free it. See malloc() and free().
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Yes when you statically allocate the memoery the size of it fixed. You're trying to reuse the allocated memory rather than reallocating exvery time. The reason why you didn't see the 'o' when you printed was that printf will print string until the '\0' char. In thisn case the NULL char would have been in str[6]. There fore str[7] was filled with 'o' froom your previous input.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  2. Replies: 7
    Last Post: 05-11-2008, 10:57 AM
  3. finding length of characters in an assigned char array ??
    By charlatanksk in forum C Programming
    Replies: 6
    Last Post: 05-04-2008, 02:34 PM
  4. dynamic memory allocation char array
    By waxydock in forum C Programming
    Replies: 2
    Last Post: 05-12-2007, 07:05 AM
  5. allocating memory for char* array
    By creeping_death in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2003, 04:49 AM

Tags for this Thread