Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3439118
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:16:11+00:00 2026-05-18T08:16:11+00:00

The task of the program is to push all the data from a structure

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:16:12+00:00Added an answer on May 18, 2026 at 8:16 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to hide a C# program from the Windows Task Manager?
In my program i have some very long task, which should be interruptable from
I'm considering separating a task from my main program to a new (java) thread.
I've written a task manager program using Java, and made a single UI implementation
I am trying to access tasks from a task manager type program, that will
I have an interesting task: to write a program which captures input from the
I'm working on a task tracking program for my job. It is modified from
I have the task to program one module to store some data and another
I'm new to using the PreferenceActivity. Task: Allow user to choose the program layout
I have a task management program with a Urgency field. Valid values are Int16

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.