A data structure problem.

124 views
Skip to first unread message

shailesh

unread,
Mar 7, 2010, 4:12:05 PM3/7/10
to GLOSS, SASTRA
struct node *nPtr, *sPtr; /* pointers for a linked list. */
for (nPtr=sPtr; nPtr; nPtr=nPtr->next)
{
free(nPtr);
}
The sample code above releases memory from a linked list. Which of the
choices below accurately describes how it will
work?
Choice 1
It will work correctly since the for loop covers the entire list.
Choice 2
It may fail since each node "nPtr" is freed before its next address
can be accessed.
Choice 3
In the for loop, the assignment "nPtr=nPtr->next" should be changed to
"nPtr=nPtr.next".
Choice 4
This is invalid syntax for freeing memory.
Choice 5
The loop will never end.

I have a confusion b/w choice 1 and 2.

Iyswarya Narayanan

unread,
Mar 7, 2010, 10:37:28 PM3/7/10
to gloss-...@googlegroups.com
choice 2 , as the memory is freed, before accessing its successor. 

NISHANT BULCHANDANI

unread,
Mar 8, 2010, 10:47:29 PM3/8/10
to gloss-...@googlegroups.com
yep,...choice 2 :)

On Mon, Mar 8, 2010 at 9:07 AM, Iyswarya Narayanan <iyswa...@gmail.com> wrote:
choice 2 , as the memory is freed, before accessing its successor. 

--
"There is no mistery about success.. Do you know even one successful man who didn't tell you about it!! "
 
To post to this group, send email to gloss-...@googlegroups.com
To unsubscribe from this group, send email to gloss-sastra...@googlegroups.com
For more options, visit this group at http://groups.google.co.in/group/gloss-sastra?hl=en

ravi babu

unread,
Mar 11, 2010, 12:12:16 PM3/11/10
to gloss-...@googlegroups.com


choice 4,,.....
human knowledge belongs to the world..

Bhargav kanipakam

unread,
Mar 12, 2010, 9:55:32 AM3/12/10
to gloss-...@googlegroups.com
the answer may be choice 2 i think....

kumara vardhan

unread,
Mar 13, 2010, 4:38:48 AM3/13/10
to gloss-...@googlegroups.com
Choice is 2
 
Reply all
Reply to author
Forward
0 new messages