Thread: Error when freeing memory (allocated with realloc)

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But WHERE did the "number" variable get produced, as in: what does it actually point to? Something allocated with malloc(), or something else? Show us the code that calls add_number().

    You do realize that you are not writing your "number" content into the memory allocated in the second code-snippet I quoted, but rather just overwriting the pointer itself.

    If you EVER free something that wasn't allocated by malloc/realloc, then you will get undefined behaviour - such as what you are seeing right now?


    --
    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.

  2. #17
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by svdrjeug View Post
    The c->numbers[0..2] are all allocated in this piece of code
    Code:
    numbers = malloc(sizeof(char**));
    	for(i=0; i < 3; i++) {
    		numbers[i] = malloc(sizeof(char*));
    		for(j=0; j < 1; j++)
    			numbers[i][j] = malloc(15*sizeof(char));
    	}
    I think this piece is correct.
    No that's not correct. True you allocate the space for 3 pointers but you store them where there is only spce for 1 pointer ( numbers is only the size of a single pointer ).
    Kurt

  3. #18
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by svdrjeug View Post
    I have tried to make some changes as suggested, but they didn't solve the problem, so I've put my original code back.
    Big mistake.

    Let me rephrase my previous comments:
    You should not use char*** numbers;
    You are not getting the memory allocation right anyway, so switch to something that is easier to get correct such as char *numbers[3];
    Yes that means you have to change a lot of other code as well. You need to do this to make a working program.

    Although this line is wrong for other reasons as well, it must be a strcpy:
    Code:
    		c->numbers[0][c->numbers_size[0]-1] = number;
    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"

  4. #19
    Registered User
    Join Date
    Jan 2008
    Posts
    7
    Quote Originally Posted by ZuK View Post
    No that's not correct. True you allocate the space for 3 pointers but you store them where there is only spce for 1 pointer ( numbers is only the size of a single pointer ).
    Kurt
    Yeah, you were right! I changed to
    Code:
    numbers = malloc(3*sizeof(char**));
    and it works perfect!

    Thank you everybody for the quick replies and tips. I will change my implementation now, as suggested by you.

    Steven

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Dynamically allocated memory
    By ^xor in forum Linux Programming
    Replies: 9
    Last Post: 06-28-2005, 11:42 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Allcoating memory and not freeing it
    By lambs4 in forum C Programming
    Replies: 1
    Last Post: 09-05-2001, 11:32 AM