Click here to Skip to main content
15,937,779 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
in this program i have segmentation fault error any one point-out where i was mistake in memory allocation in this program.

C#
//stack program
#include<stdio.h>
#include<conio.h>

struct node
{
       int data;
       struct node *next;   //this variable is refer the same structure
}*top=NULL;
int main()
{
//     clrscr();
     insert(34);   //insert node
     insert(20);

     display();
//     getch();
     return 0;
}
     // insert new stack value
insert(int val)
     {
                struct node *temp = (struct node *)malloc(sizeof(struct node));
                temp = top;
                temp->data = val;
                temp->next = top;
                top = temp;
                }
     //display the stack
display()
     {
              struct node *temp = top;
              while(temp)
              {
                         printf("%d",temp->data);
                         temp = temp->next;
                         }
                         }
Posted

In the beginning you initialize a pointer to a node variable called top to null.
Then in your insert method you initialize variable temp ( a pointer to a node ) with the pointer you got from allocating an appropriate sized memory chunk. Directly after that you overwrite that temp variable with the content of variable top (which is still null). After this you try to access elements within the node struct.

This explains your pains.

All you have to do is leave that statement out all together:

C#
insert(int val)
{
    struct node *temp = (struct node *)malloc(sizeof(struct node));
    // temp = top; // This is not needed as it overwrites the previously acquired pointer
    temp->data = val;
    temp->next = top;
    top = temp;
}

Best Regards,

—MRB
 
Share this answer
 
v2
Comments
Albert Holguin 11-Oct-11 11:27am    
+5
Manfred Rudolf Bihy 11-Oct-11 11:30am    
Muchas gracias!
Albert Holguin 11-Oct-11 13:18pm    
:)
Why are you assigning top to temp right after you've just allocated temp? Off the back, you're loading NULL where you actually already had a valid heap memory address.

edit:
Also noticed in your small program you never deallocate the memory. You should probably do that unless you want memory leaks.
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 11-Oct-11 11:24am    
Correct! 5+
Albert Holguin 11-Oct-11 11:27am    
thank you manfred... looks like a few of us answered almost at the same time...
Manfred Rudolf Bihy 11-Oct-11 11:29am    
It was close enough to recognize that! :)
C#
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp = top;


So you allocate this new shiny chunk of memory and store it's address in 'temp'

Then you immediately wipe out the pointer with 'top', which is initialized to NULL.

BOOM
 
Share this answer
 
Comments
Manfred Rudolf Bihy 11-Oct-11 11:24am    
Spot on! 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900