malloc allocates the same space in the memory

This is a discussion on malloc allocates the same space in the memory within the C Programming forums, part of the General Programming Boards category; I use malloc in a while loop to create a linked list and it allocates the same space in each ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52

    malloc allocates the same space in the memory

    I use malloc in a while loop to create a linked list and it allocates the same space in each iteration. What is wrong? Doesn't malloc allocate the space in the heap? Why it allocates the same space each time? How can I solve this problem?

    Thanks in advance.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Show us some code that demonstrates this problem. If you free() what you allocate each time, then it may very well pick the same spot...
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    3
    You need to do something like
    previous_node = current_node;
    current_node = malloc(sizeof(whatever it is));
    ...
    previous_node->next = current_node

    If i understand what you're doing, you're allocating the same space for the same pointer every time in the while. You need to copy that pointer to other, and then allocate a fresh new one, and link the stuff.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52
    Code:
    while(1)
    {
    // . . . 
    // create the new mail which will be added at the end of the linked list
    mailbox sent_mail;
    sent_mail = NULL; // make sure that the malloc will be called
    sent_mail = malloc(sizeof(mail));
    // if malloc fails
    if (sent_mail==NULL){
    printf("You don't have enough memory on your system. This application is going to be killed\n");
    exit(1);
    }
    // the newest mail is the first one
    sent_mail->next = user_in_dbase[i].mail_list;
    user_in_dbase[i].mail_list = sent_mail;	
    // . . . 
    }

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by myle View Post
    Code:
    while(1)
    {
    // . . . 
    // create the new mail which will be added at the end of the linked list
    mailbox sent_mail;
    sent_mail = NULL; // make sure that the malloc will be called
    sent_mail = malloc(sizeof(mail));
    // if malloc fails
    if (sent_mail==NULL){
    printf("You don't have enough memory on your system. This application is going to be killed\n");
    exit(1);
    }
    // the newest mail is the first one
    sent_mail->next = user_in_dbase[i].mail_list;
    user_in_dbase[i].mail_list = sent_mail;	
    // . . . 
    }
    This is quite surely not necessary.

    The code you are showing is not complete, as far as I can tell, and it's very difficult to see anything that is wrong from the snippet you have posted.

    However, I'm wondering what user_in_dbase[i].mail_list is, and how that is related to your linked list? Why are you setting sent_mail->next to user_in_dbase[i].mail_list? next is usually the name of the pointer to the next element in the list.

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

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52
    Quote Originally Posted by matsp
    The code you are showing is not complete, as far as I can tell, and it's very difficult to see anything that is wrong from the snippet you have posted.

    However, I'm wondering what user_in_dbase[i].mail_list is, and how that is related to your linked list?
    It's a pointer to the first element of a linked list. It is defined as mail* .

    Quote Originally Posted by matsp
    Why are you setting sent_mail->next to user_in_dbase[i].mail_list? next is usually the name of the pointer to the next element in the list.
    We want the new mail to be the first mail of the list and then the head of the list to point to this element.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    OK, so why are you saying that "malloc allocates the same space"? The code you have shown so far seems correct, so I wonder what you mean by it. What are the signs that you are getting the same address - are you printing the sent_mail pointer after malloc? What values do you get?

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

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Is mailbox a typedef for a mail* var?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52
    @ matsp: Yes, I was printing the hex value of the send_mail and it was always the same, no matter how many iterations we had done.
    It seems like it allocates the same space each time even if I don't free() the space allocated by the pointer.

    @hk_mp5kpdw: Exactly. It's an alias for mail*.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I find it EXTREMELEY unlikely that a function as frequently used as malloc() would fail in such an apparent way. Are you sure you are printing the value of the pointer, not the address of the pointer?

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

  11. #11
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52
    I check it with the line:
    Code:
    printf("The pointer value is: %x", sent_mail);

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you post the value you get, and also print the address of sent_mail?

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

  13. #13
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52
    The addresses are respectively, in hex:
    Quote Originally Posted by output
    The pointer value is: 804b008
    The address of the pointer is: bfd4fc48
    as far as the sent_mail is concerned.

    I have run the program twice and I got the same results.
    Last edited by myle; 11-19-2007 at 03:23 PM.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Pointer value looks sane, and it's clearly a different range to the stack. It looks like you are running in Linux, so I expect that it's a glibc supplied malloc - if that's broken, we'd all know about it.

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

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Where did you add your printout? You sent me a PM with the code, but since I don't have the type declarations or any of the rest of the code, I can't compile it and test it - not that I would be inclined to try to debug a whole half-functional mail server.

    I can guarantee that if the rest of your code isn't horribly broken, malloc() is allocating memory correctly. Of course, if you overwrite the right bit of memory in the heap (or do a double free, or some other bad thing), you may get a "circular link" in the linked list that malloc()/free() uses to keep track of the memory allocated - and that could of course lead to all sorts of "interesting" effects, including that malloc() itself returns the same value each time.

    By the way, just re-reading the previous post, are you saying that if you run your application twice, doing a single allocation, you get the same value - that is common and normal. If you call malloc several times within the same process, you should get different values.

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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with insert/delete binary search tree
    By Nazgulled in forum C Programming
    Replies: 39
    Last Post: 03-25-2009, 04:24 PM
  2. memory problem, maybe malloc struct...
    By s_siouris in forum C Programming
    Replies: 3
    Last Post: 07-11-2008, 08:34 AM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-13-2001, 09:57 AM

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