在C中将数据从结构推入堆栈
该程序的任务是使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这段代码是错误的:
当你
memcpy a
进入其中时,snew->s
没有初始化。我希望看到两个malloc
- 一个用于STACK*
,另一个用于STD*
,然后您将用它来种子snew->s
在将内容复制到其中之前。或者,您可以使用单个
malloc
,并将snew->s
指向其中的适当偏移量(在为STACK struct< 留出空间之后) /代码>)。
push
函数上的siz
参数似乎是多余的,因为您总是传入struct STD
。This code is wrong:
snew->s
is not initialized when youmemcpy a
into it. I would expect to see twomalloc
s - one forSTACK*
and another forSTD*
, which you would then use to seedsnew->s
before copying stuff into it.Alternatively you could use a single
malloc
, and pointsnew->s
to the appropriate offset within it (after you've left space for theSTACK struct
).The
siz
parameter on yourpush
function seems superfluous, since you are always passing in astruct STD
.s
分配空间,st
初始化为NULL
snew
是否为notNULL
即
看起来还有其他问题,开始使用有意义的名称,而不是
ss
、st
..s
st
toNULL
snew
is notNULL
i.e.
And it looks like there are other issues there, start using meaningful names, not
ss
,st
..这就是你要做的,达拉马:
1. 如果您有调试器,并且知道如何使用它,则单步执行 Push() 函数以查看分段错误发生的位置。
2. 否则,在push()的每一行之间放置一个printf语句:
这也会告诉你哪里发生了分段错误。
如果您仍然遇到困难,请向我们反馈新信息。
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():
This will also tell you where the segmentation fault occurs.
If you're still stuck, then get back to us with the new info.