Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.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
C 大小为8的写入无效_C_String_Valgrind - Fatal编程技术网

C 大小为8的写入无效

C 大小为8的写入无效,c,string,valgrind,C,String,Valgrind,我开发了一个动态字符串,可以在c编程语言中调整大小,但我读到valgrind报告时出现了一些错误,我试图找出原因,但失败了: 附言:程序正在运行 D字符串c main.c 为分配结构指定了错误的内存量: if((*dstr = malloc(sizeof *dstr)) == NULL) 因为dstr是(struct dstr**),所以sizeof(*dstr)是指针的大小,而不是结构的大小。要解决此问题,您可能希望以以下方式编写: if((*dstr = malloc(sizeof **d

我开发了一个动态字符串,可以在c编程语言中调整大小,但我读到valgrind报告时出现了一些错误,我试图找出原因,但失败了:

附言:程序正在运行

D字符串c main.c
为分配结构指定了错误的内存量:

if((*dstr = malloc(sizeof *dstr)) == NULL)
因为dstr是(struct dstr**),所以sizeof(*dstr)是指针的大小,而不是结构的大小。要解决此问题,您可能希望以以下方式编写:

if((*dstr = malloc(sizeof **dstr)) == NULL)

虽然Chostakovitch的答案是错误的,但事实上应该是
*dstr=malloc(sizeof(**dstr))
。对不起我的答案,我意识到
dstr
不是类型,而是指针本身。我把它拿走了。伊哈罗布是对的。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "error.h"
#include "dstring.h"


int main()
{
    lgw_return rc;
    DynamicString *s;

    if((rc =lgw_init_dstring(&s, "test")) != LGW_SUCCESS) {
        fprintf(stderr, "ERROR : %s", lgw_strerror(rc));
        exit(EXIT_FAILURE);
    }

    lgw_append_dstring(s, "123");
    const char *str = lgw_get_dstring(s);

    printf("value is : %s\n", str);
    lgw_destroy_dstring(&s);
}
if((*dstr = malloc(sizeof *dstr)) == NULL)
if((*dstr = malloc(sizeof **dstr)) == NULL)