Pushing data from a structure into a stack in C
The task of the program is to push all the dat开发者_运维技巧a from a structure into a stack, using memcpy
.
Upon execution, it successfully enters the data into the structure, but reaches a segmentation fault when it comes to the push()
function.
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <mem.h>
typedef struct STD {
char ime [50];
int fn;
float usp;
} STD;
typedef struct STACK {
STD *s;
STACK *next;
} STACK;
int push (void *a, int siz, STACK **sst) {
STACK *snew;
snew = (STACK *) malloc (siz + 1);
memcpy (snew->s, a, siz);
snew -> next = *sst;
*sst = snew;
}
int main () {
STACK *st;
STD ss;
printf ("Vyvedi ime");
gets (ss.ime);
ss.ime[49] = 0;
printf ("Vyvedi fn");
scanf ("%d", &ss.fn);
printf ("Vyvedi usp");
scanf ("%f", &ss.usp);
push (&ss, sizeof(ss) , &st);
system ("pause"); }
Don't know if it matters, I use DevC as a compiler.
This code is wrong:
STACK *snew;
snew = (STACK *) malloc (siz + 1);
memcpy (snew->s, a, siz);
snew->s
is not initialized when you memcpy a
into it. I would expect to see two malloc
s - one for STACK*
and another for STD*
, which you would then use to seed snew->s
before copying stuff into it.
STACK *snew;
snew = (STACK *) malloc (sizeof(STACK));
snew->s = (STD*) malloc(sizeof(STD));
memcpy (snew->s, a, siz);
Alternatively you could use a single malloc
, and point snew->s
to the appropriate offset within it (after you've left space for the STACK struct
).
STACK *snew;
snew = (STACK *) malloc (sizeof(STACK) + siz + 1);
snew->s = (char*)snew + sizeof(STACK);
memcpy (snew->s, a, siz);
The siz
parameter on your push
function seems superfluous, since you are always passing in a struct STD
.
- note you do not allocate a space for
s
- you need to initialize the
st
toNULL
- pls check that
snew
is notNULL
i.e.
int push (void *a, int siz, STACK **sst) {
STACK *snew (STACK *) malloc (siz + 1);
snew->s = (STD *) mallos (sizeof(STD)); // <-----------
memcpy (snew->s, a, siz);
snew -> next = *sst;
*sst = snew;
}
And it looks like there are other issues there, start using meaningful names, not ss
, st
..
Here's what you do, Dalamar:
1. If you have a debugger, and you know how to use it, then step through the push() function to see where the segmentation fault occurs.
2. Otherwise, put a printf statement between every line in push():
printf ("1\n") ;
...
printf ("2\n") ;
...
This will also tell you where the segmentation fault occurs.
If you're still stuck, then get back to us with the new info.
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include "linkedlist.h"
int main(int argc, char *argv[])
{
LinkedList myList;
Node *node ;
int choice = 0 , index=0 ;
string agentName, caseDesc ;
int caseNo;
do
{
cout<< "\n Enter 1 Add a new node to the end \n";
cout<< " Enter 2 Add a new node to the beginning \n";
cout<< " Enter 3 Print out the entire list \n";
cout<< " Enter 4 Remove a node from the list \n";
cout<< " Enter 5 Quit the program \n";
cout<< " Enter your choice : ";
cin>> choice;
switch(choice){
case 1:
// Insert appropriate code here ....
break;
case 2:
// Insert appropriate code here ....
break;
case 3:
// Insert appropriate code here ....
break;
case 4:
// Insert appropriate code here ....
break;
case 5:
exit(1);
break;
default :
cout<<"\n Invalid Option, Please try again .....\n";
break;
}
}while (true);
system("PAUSE");
return 0;
精彩评论