Pushing data from a structure into a stack in C

The task of the program is to push all the data 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.

Answers


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 mallocs - 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.


  1. note you do not allocate a space for s
  2. you need to initialize the st to NULL
  3. pls check that snew is not NULL

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.


Need Your Help

Android mediaplayer won't start after pause

java android android-mediaplayer

I'm trying make a simple mediaplayer but the pause button doesn't work. When I click on the pause button it stops but when I click on play again it starts at the start again.

Truncating a double to a float in C

c floating-point double precision floating-accuracy

This a very simple question, but an important one since it affects my whole project tremendously.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.