Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当我在malloc内存后将ptr传递给函数时,如果我没有';t malloc,很有趣?_C - Fatal编程技术网

当我在malloc内存后将ptr传递给函数时,如果我没有';t malloc,很有趣?

当我在malloc内存后将ptr传递给函数时,如果我没有';t malloc,很有趣?,c,C,这是一个双回路链路。关于我的问题,没有发布dev_num、blk_num和status typedef struct buf_node { unsigned dev_num; unsigned blk_num; unsigned status:2; struct buf_node *ptr_prev_free_q; struct buf_node *ptr_next_f

这是一个双回路链路。关于我的问题,没有发布dev_num、blk_num和status

   typedef struct buf_node {
    unsigned            dev_num;
    unsigned            blk_num;
    unsigned            status:2;
    struct buf_node     *ptr_prev_free_q;
    struct buf_node     *ptr_next_free_q;
    } stc_buf_header;
这是一个缓冲区节点,包含一个标识buf_头和一个数据字段

 typedef struct {
    stc_buf_header      *buf_header;
    stc_mm              *data_area;
    } stc_buffer;
这是一个双循环链接,用于保存等待使用的空闲缓冲区节点(如UNIX)

现在我定义了一个函数来初始化双循环链接

int init(stc_buffer *lk_header, unsigned dev_num, unsigned blk_num){

printf("buf_header\t%p\n", lk_header->buf_header);
printf("lk_header\t%p\n", lk_header);

printf("dev_num\t%d\n", lk_header->buf_header->dev_num);
printf("blk_num\t%d\n", lk_header->buf_header->blk_num);

lk_header->buf_header->dev_num = dev_num;
lk_header->buf_header->blk_num = blk_num;
lk_header->buf_header->status = 0x0;
lk_header->buf_header->ptr_next_free_q = lk_header->buf_header;
lk_header->buf_header->ptr_prev_free_q = lk_header->buf_header;
lk_header->data_area->data = -1; // means that this node is header of the          link
}
当我遇到这个错误时,printf()语句用于调试

好,现在,如果我在main()中这样写

当它运行到printf()语句处时,将引发一个Segment Fualt错误

但是,如果我在main()中这样写


一切都好。为什么??,这让我感到困惑…(-o-)#

首先,使用
%p
打印地址,而不是
%d

您的问题是
malloc
只分配内存,而不初始化它。而您
malloc
调用只初始化了
struct stc_free_lk
对象的内存,它不会为作为指针的成员分配内存,您还需要为它们分配内存

所以
lk_header->buf_header
没有指向任何地方,您不能取消引用它,这是未定义的行为。 你看到的是一个典型的未定义行为

正如我所说,在访问结构成员之前,必须初始化结构成员。 您可以使用
calloc
而不是
malloc
,因为
calloc
设置分配的 内存为0,这有助于初始化结构中的指针

stc_free_lk *link = calloc(1, sizeof *link);
if(link == NULL)
{
    fprintf(stderr, "Not enough memory\n");
    return 1;
}

link->header = calloc(1, sizeof *link->header);
if(link->header == NULL)
{
    fprintf(stderr, "Not enough memory\n");
    free(link);
    return 1;
}

init(link->lk_header, (unsigned)0, (unsigned)0);
...
然后在
init
中,您还应该为
lk\u头->buf\u头分配内存
lk\u头->数据区域
等等

我建议您编写一个函数,将所有内存分配给所有 指针,这样您就不必在不同的位置分配内存。那个 使代码很难遵循,也很难发现bug。我还要写一个
destroy

一个函数,它可以释放所有分配的内存,再次将所有内存放在一个位置。

首先,使用
%p
打印地址,而不是
%d

您的问题是
malloc
只分配内存,而不初始化它。而您
malloc
调用只初始化了
struct stc_free_lk
对象的内存,它不会为作为指针的成员分配内存,您还需要为它们分配内存

所以
lk_header->buf_header
没有指向任何地方,您不能取消引用它,这是未定义的行为。 你看到的是一个典型的未定义行为

正如我所说,在访问结构成员之前,必须初始化结构成员。 您可以使用
calloc
而不是
malloc
,因为
calloc
设置分配的 内存为0,这有助于初始化结构中的指针

stc_free_lk *link = calloc(1, sizeof *link);
if(link == NULL)
{
    fprintf(stderr, "Not enough memory\n");
    return 1;
}

link->header = calloc(1, sizeof *link->header);
if(link->header == NULL)
{
    fprintf(stderr, "Not enough memory\n");
    free(link);
    return 1;
}

init(link->lk_header, (unsigned)0, (unsigned)0);
...
然后在
init
中,您还应该为
lk\u头->buf\u头分配内存
lk\u头->数据区域
等等

我建议您编写一个函数,将所有内存分配给所有 指针,这样您就不必在不同的位置分配内存。那个 使代码很难遵循,也很难发现bug。我还要写一个
destroy

函数,该函数释放所有分配的内存,再次全部放在一个位置。

即使在第二个实例中,它实际上也不正常,只是因为堆栈的缘故才看起来正常。试着用-O2编译第二个实例,看看它是否崩溃(可能会崩溃)。实际上,即使在第二个实例中,它也不正常,只是因为堆栈的缘故才看起来正常。尝试用-O2编译第二个实例,看看它是否崩溃(可能会崩溃)。ACK,我正在尝试如果我仍然使用malloc,但指出刚才分配给NULL的内容如何?@W.Cameron抱歉,我不理解你的问题。ACK,我正在尝试如果我仍然使用malloc,但指出刚刚分配给NULL的内容如何?@W.Cameron抱歉,我不理解你的问题。
int main(){
    stc_free_lk link;
    init(link.lk_header, (unsigned)0, (unsigned)0);

    return 0;
}
stc_free_lk *link = calloc(1, sizeof *link);
if(link == NULL)
{
    fprintf(stderr, "Not enough memory\n");
    return 1;
}

link->header = calloc(1, sizeof *link->header);
if(link->header == NULL)
{
    fprintf(stderr, "Not enough memory\n");
    free(link);
    return 1;
}

init(link->lk_header, (unsigned)0, (unsigned)0);
...