[英]Segmentation fault when trying to memset() a structure pointer to 0
I just want to set 0 to a pointer to a structure like this: 我只想将0设置为指向这样的结构的指针:
#include <stdio.h>
#include <string.h>
struct apple
{
int f;
int d;
size_t tree;
};
struct apple *app;
int main(void)
{
memset(&app,0,sizeof(app));
app->tree = 1;
app->d = 4;
app->f = 2;
printf("%d %d %lu\n",app->d, app->f, app->tree);
}
It compiles fine with gcc -o foo foo.c
but when I run I get Segmentaion fault. 使用
gcc -o foo foo.c
正常编译,但是运行时出现Segmentaion错误。 i tried memset(app, 0 , sizeof(app));
我尝试了
memset(app, 0 , sizeof(app));
but it complains, I tried memset(*app, 0,sizeof(app));
但它抱怨,我尝试了
memset(*app, 0,sizeof(app));
also complains. 也抱怨。 This one compiles fine but I still get seg fault
memset((void *) &app, 0, sizeof(app));
这个编译很好,但我仍然遇到段错误
memset((void *) &app, 0, sizeof(app));
What should i do to set this pointer to struct 0 ? 我应该怎么做才能将此指针设置为结构0 ? If I have just a simple struct like this :
struct apple app;
如果我只是一个简单的结构是这样的:
struct apple app;
(without pointer) it should go like memset(&app, 0, sizeof(app));
(没有指针),它应该类似于
memset(&app, 0, sizeof(app));
Because it's a pointer and memset has the first parameter a void*
, why isn't working like passing just a simple variable without the "&" thing? 因为它是一个指针,并且memset的第一个参数为
void*
,所以为什么不像只传递一个简单的变量而没有“&”一样工作呢?
This: 这个:
memset(&app,0,sizeof(app));
Overwrites app
(a pointer) with zeroes. 用零覆盖
app
(指针)。 We're off to a bad start. 我们开局不好。
Then we try and reference app
(which is now NULL
): 然后,我们尝试引用
app
(现在为NULL
):
app->tree = 1;
And things have gotten worse. 而且情况变得更糟了。
You want to allocate something for app
to point to: 您要分配一些东西给
app
指向:
app = malloc(sizeof(struct apple));
memset(app, 0, sizeof(struct apple));
or 要么
app = calloc(1, sizeof(struct apple)); // calloc zeroes things out for you
You are passing &app
as a parameter to memset
here: 您在此处将
&app
作为参数传递给memset
:
memset(&app,0,sizeof(app));
This means you're setting the value of that pointer to 0
. 这意味着您正在将该指针的值设置为
0
。 You then proceed to de-reference it. 然后,您可以取消引用它。
app->tree = 1;
It is extremely unlikely that the address 0
points to a place you can write to (in fact, it is possible, but not given, that this address represent the null pointer.) This is enough to cause a segmentation violation. 地址
0
极不可能指向您可以写入的位置(实际上,有可能但没有给出该地址表示空指针的信息。)这足以引起分段冲突。
You need to decide what you mean with "to set 0 to a pointer to a structure". 您需要确定“将0设置为指向结构的指针”的含义。 If you want the contents of the pointee set to zero, then you can use
memset
thus, after making it point to some allocated memory: 如果要将pointee的内容设置为零,则可以在指向某个分配的内存后使用
memset
:
app = malloc(sizeof(*app)); // allocate space for an apple object
memset(app, 0, sizeof(*app)); // app, not &app!!!
Note that you can allocate and zero initialize the allocated memory using calloc
. 注意,您可以使用
calloc
分配和零初始化分配的内存。
app = calloc(1, sizeof(*app));
If you really want to set the value of the pointer to 0, then perhaps you mean setting it to the null pointer, which you can achieve like this: 如果您确实希望将指针的值设置为0,则可能是将其设置为空指针,可以这样实现:
app = NULL;
But now you can't de-reference the pointer. 但是现在您不能取消引用指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.