Thread: Question about dynamic memory allocation

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    46

    Question about dynamic memory allocation

    Hi guys , i have a question about din. memory alloc. and i want to ask it with an example.

    Code:
    /* here is my struct */
    struct node
    {
          int data;
          struct node *next;
    }
    /* here is my simple making node function*/
    
    struct node *makenode(int value)
    {
            struct node *np;
            np=malloc(sizeof(struct node));
            np->data=value;
            np->next=NULL;
    }
    What if i did not write the np=malloc(sizeof(struct node)); line, Does the code work properly ? When i need to take a place from memory using with pointer and when i dont need ? Thank you for your answers.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by dayanike View Post
    Code:
            struct node *np;
            np=malloc(sizeof(struct node));
            np->data=value;
            np->next=NULL;
    }
    What if i did not write the np=malloc(sizeof(struct node)); line, Does the code work properly ?
    No - in this case, np would be uninitialized and could point anywhere, so np->data could be located anywhere in your address space. So you don't know what you might be trying to modify.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    58
    Quote Originally Posted by dayanike View Post
    When i need to take a place from memory using with pointer and when i dont need ?
    Basically if you want to use the contents of what that pointer points to you will always need to have allocated space.

    If you want to iterate through a linked list, you could do:

    Code:
    int find (struct node * root, int value){
       struct node * t; /* you don't need to allocate memory here */
       
       t = root; /* t will point to an already allocated space in memory */
    
       while (t != NULL) {
          if (t->data == value)
             return 1;
          
          t = t->next;
       }
    
    return 0;
    }
    Pointers are variables which store memory addresses. When you declare them they are storing a random address whose contents are not known. So you initialize them by allocating space or making them point to an already allocated space.

    You can also assign them to NULL which is an initialization, but in this case the pointer is still pointing to something now known.

    Edit: There's this small tutorial which covers very nice features of pointers, give it a look.
    Last edited by koplersky; 12-11-2012 at 08:42 AM. Reason: added link

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Memory Allocation Question
    By somniferium in forum C Programming
    Replies: 6
    Last Post: 10-12-2012, 07:51 PM
  2. Dynamic Memory Allocation
    By slash.hack in forum C Programming
    Replies: 9
    Last Post: 09-30-2011, 04:31 AM
  3. A question related to dynamic memory allocation
    By spiit231 in forum C Programming
    Replies: 2
    Last Post: 03-11-2008, 12:25 AM
  4. Dynamic Memory Allocation Question
    By Piknosh in forum C++ Programming
    Replies: 1
    Last Post: 04-14-2004, 01:55 PM
  5. dynamic memory allocation
    By inquisitive in forum C++ Programming
    Replies: 5
    Last Post: 03-13-2004, 02:07 AM

Tags for this Thread