在C中将数据从结构推入堆栈

发布于 2024-10-05 07:08:06 字数 835 浏览 3 评论 0原文

该程序的任务是使用memcpy 将结构中的所有数据推送到堆栈中。 执行后,它成功地将数据输入到结构中,但在涉及 push() 函数时出现分段错误。

代码如下:

#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");      }

不知道是否重要,我使用 DevC 作为编译器。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

宣告ˉ结束 2024-10-12 07:08:06

这段代码是错误的:

STACK *snew;
snew = (STACK *) malloc (siz + 1);
memcpy (snew->s, a, siz); 

当你memcpy a进入其中时,snew->s没有初始化。我希望看到两个 malloc - 一个用于 STACK*,另一个用于 STD*,然后您将用它来种子 snew->s 在将内容复制到其中之前。

STACK *snew;
snew = (STACK *) malloc (sizeof(STACK));
snew->s = (STD*) malloc(sizeof(STD));
memcpy (snew->s, a, siz);

或者,您可以使用单个 malloc,并将 snew->s 指向其中的适当偏移量(在为 STACK struct< 留出空间之后) /代码>)。

STACK *snew;
snew = (STACK *) malloc (sizeof(STACK) + siz + 1);
snew->s = (char*)snew + sizeof(STACK);
memcpy (snew->s, a, siz);

push 函数上的 siz 参数似乎是多余的,因为您总是传入 struct STD

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.

如果没结果 2024-10-12 07:08:06
  1. 请注意,您没有为 s 分配空间,
  2. 您需要将 st 初始化为 NULL
  3. 请检查 snew 是否为not NULL

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;
 }

看起来还有其他问题,开始使用有意义的名称,而不是 ssst..

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

时光与爱终年不遇 2024-10-12 07:08:06

这就是你要做的,达拉马:
1. 如果您有调试器,并且知道如何使用它,则单步执行 Push() 函数以查看分段错误发生的位置。
2. 否则,在push()的每一行之间放置一个printf语句:

printf ("1\n") ;  
...  
printf ("2\n") ;  
...  

这也会告诉你哪里发生了分段错误。
如果您仍然遇到困难,请向我们反馈新信息。

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.

阪姬 2024-10-12 07:08:06
#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;
#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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文