Thread: general pointer & malloc question

  1. #1
    Registered User
    Join Date
    May 2002

    general pointer & malloc question

    i was wondering...

    if a pointer holds the address to the item it points to, then why do you have to malloc a pointer when you create a new node on a linked list? you're not storing the new node inside the pointer... just storing the address to the node, am i correct? can someone please clarify this for me? thanks!


  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    You can either point a pointer to already allocated memory or allocate an unnamed block of memory yourself and assign it to the pointer, this is what linked structures do most often since it keeps things simple.

    >you're not storing the new node inside the pointer... just storing
    >the address to the node, am i correct
    Not always, sometimes you create a new node and then assign the data to it with variables or from a file. Also, if you simply assign the address of a struct instance to a pointer of a node you run the risk of that instance going out of scope. If it isn't dynamically allocated then you can add it to the list and when you return from a function your pointer now points to garbage. If you allocate the memory yourself then you are sure that it will be yours until you need to free it.

    All of this is done because it makes the code shorter and easier to follow, as well as more solid against bugs and other such nasties.

    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2002
    yes pointers only hold address in them or we can say the value
    held by a pointer is address to some memory location.
    why do we malloc?
    when ever you declare a pointer suppose
    int *num;
    now when you run your programm
    num will be assigned some memory location say 10.
    now this 10 is given the name num.
    looking at the above statement it would be safe to say
    that num is having the address 10;
    now in 10 whatever will be the value would be considered address why because you have made num a pointer.and yes
    there will be some garbage value here inside 10.
    let us suppose it is 20.
    so now num is having address 10 and in that address is stored 20.if num would not have been a pointer than this 20 would have
    been a normal 20.but since it is a pointer this 20 refers to the memory location numbered 20.and you did not place 20 inside 10
    system itself did it.if earlier to int *num;you had made another pointer int *lum; then conditions would have been same there too
    say its address is 30 and inside 30 is it is possible that it is again 20.
    now both the pointers are pointing to the same location
    if one pointer tries to change the value in 20.wherever other pointer is would recieve its effect avoid this
    malloc is used so it is made sure that both pointers is holding the address of some new location.
    to summarize:-
    mallocing is done to tell the pointers where to point and it is checked by the system that the new memory location is not being used by anything else.
    i think this would do for explaning your problem;

    if i am wrong please dont mind.
    correct me
    i am simply a student i myself need the help of your esteemed self

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  2. Rookie pointer problem: 2 dimensions, malloc
    By GatesAntichrist in forum C Programming
    Replies: 11
    Last Post: 12-20-2005, 12:35 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. struct problem and general C question
    By techrolla in forum C Programming
    Replies: 8
    Last Post: 01-09-2004, 01:37 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM