Thread: book record function

  1. #1
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36

    Post book record function

    how does write a function that creates a new node for a linked list of book records.

    Suppose the function name is:

    Code:
    struct bookRecord* createNode(char* title, char isbn[16], int year);
    The function performs the following actions.
    Creates a new node of type struct bookRecord.
    Assigns the parameter title to the field bookTitle of the new node.
    Assigns the parameter isbn to the field isbn of the new node.
    Assigns the parameter year to the field year of the new node.
    Make this node the end node.

  2. #2
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Red face For a Start

    Code:
       struct bookRecord *temp;
       strcpy(temp->bookTitle,title);
       strcpy(temp->isbn,isbn);
       temp->year = year;
    
       tailNode->next = temp;
       temp->next = NULL;
       tailNode = temp;
    Be sure to add the appropriate headers and also, why would you want to return the node? Next, I assume that you are maintaining a tailNode (which keeps your coding to a minimum).
    Help everyone you can

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Be sure to add the appropriate headers and also, why would you want to return the node? Next, I assume that you are maintaining a tailNode (which keeps your coding to a minimum).
    Why wouldn't you want to return a node?
    Code:
    Node *ptr = NULL, *theList = NULL;
    
    prt = newNode( somedata );
    if( ptr )
    {
        prt->next =theList;
        theList = ptr;
    }
    Basicly, you're creating a malloc type function that fills in one or more elements of the node itself, and returns the new node. It's quite common to do it this way. The main reason for doing this, rather than the way you do it, is in the event you want to have two or more lists. Using globals and a function that automaticly links for you, you can't do this. However, if you simply have a function that creates a node and populates its members with data, you can then stick this on whatever list you want.

    Granted, you could pass a pointer to the list as a function member, and have it automaticly add it to it. But some times it's quite handy to simply have a function that allocates a node and optionally populates its data members.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Just a caution,

    in the case of your style Quzah, the most important pit-fall to be avoided is that the temporary (new) node should not be declared within the function or else its memory will be freed as soon as you return back from that function.
    For the benifit of those who are confused...
    Code:
    NODE *AddNewNode(int a,int b)
    {
       NODE *temp;
       temp = (NODE *)malloc(sizeof(NODE));
       temp->firstElement = a;
       temp->secondElement = b;
       return temp;
    }
    This code is very dangerous because, Temp is created when the function is executed and that memory is freed when you exit out of the function. So, from the main function (if you are calling this from main), you are never sure that the pointer will point to valid data after this function is called.

    -Cheers
    Harsha
    Help everyone you can

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by vsriharsha
    Just a caution,

    in the case of your style Quzah, the most important pit-fall to be avoided is that the temporary (new) node should not be declared within the function or else its memory will be freed as soon as you return back from that function.
    Thank you Mr. Mis-information. Just how the hell do you think malloc works, if not by returning a variable defined in that function? In other words, no functions could create a new dynamic variable, you'd have to declare them all in main, or in a non-terminating scope of a function below them, otherwise, by your logic, it would automaticly free itself.

    That's wrong. Also what you're saying, is that there's never any point in calling free, because it will automaticly do it when the scope ends. This is also entirely wrong.

    In short, you're wrong. How else would you ever dynamicly create variables? Yes, automatic, non-static, non-pointer variables are freed when a scope block ends.

    In your example, this is what happens:
    1) A pointer called "temp" is declared.
    2) malloc is called, storing the address of the newly allocated memory in "temp".
    3) The function returns the value stored in "temp".

    Seriously, how on earth do you think function returns work? If you return a value from a function, the value doesn't "vanish" simply because what you return does. Yes, the actual instance of the pointer vanishes. However, you're returning the value stored in the pointer, because all pointers are is variables that store addresses. Thus, just like every other variable, a value is returned.

    No, there is absolutely nothing wrong with doing that. Yes, it works fine. No, you're wrong.
    [/rant]

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Well Well Well,

    that poses a serious question about my understanding of scopes and memory allocations and function returns... Let me put down what I understood (until I read your post).
    1. When ever a program enters a function, a new stack-frame is created for that function and any non-static variable is allocated memory in that stack frame. Any dynamic memory allocation is done in the Heap. Static variables are anyway stored in the BSS section of the program-executable.
    2. When you return back from a function, the created stack-frame is deleted (freed for use at a later point by a later function) and any information in that stack frame becomes Garbage.
    3. In my example, when you return temp, you are returning the value (of a memory location) allocated in the heap. So, here you mean to say that its only the stack part that is destroyed and not the variables created in Heap? (Im not sure about it, but will surely study it now).
    4. I never said that the value that is returned (address contained by the pointer) is vanished. All I said was that the memory location pointed to by the Pointer (temp in my case) might not be appropriate in the context (again, if heap is not cleared when a program returns, then yes, I've learnt a new thing).

    Cheers,
    Harsha.
    Help everyone you can

  7. #7
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Ok... Plead Guilty...

    You were so correct Qazah.. Variables allocated in Heap (using Dynamic allocation) are not freed automatically after the function finishes its execution. In case of stack, the stack pointer is moved back by the number of bytes allocated, but this is not the case with heap and you have to explicitly call FREE. WEll, that explains many things. And, yes you can create dynamic memory within functions and still pass the address of that memory to the calling function without any loss of info.

    Thanks for making me Mr. Informed

    Cheers,
    Harsha.
    Help everyone you can

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM