gpt4 book ai didi

从函数返回字符串数组时无法访问地址 0x0 处的内存

转载 作者:太空宇宙 更新时间:2023-11-04 03:02:10 25 4
gpt4 key购买 nike

我将指向 char* 数组的指针发送给函数 (args)。在该函数中,我将数组中的前两个位置的值设置为 malloc 字符串。当我返回到数组本身被 malloc 的原始调用函数时,数组的最后一个位置给我一个“无法访问地址 0x0 处的内存”。我在 malloc/realloc/或存储值中做错了什么吗?

调用函数:

int bufspace = 0; /* bytes in table */

...

args = emalloc(BUFSIZ); /* initialize array */
bufspace = BUFSIZ; //size=8192
spots = BUFSIZ / sizeof(char *);

while (*cp != '\0') //While not at the end
cp = read_segment(cp, &len, &indollarsign, &argnum, start, &args, &prev_char_escape);
start = cp;
len = 0;

if (argnum + 1 >= spots) {
args = erealloc(args, bufspace + BUFSIZ);
bufspace += BUFSIZ;
spots += (BUFSIZ / sizeof(char *));
}
}

调用的函数(read_segment)- 首先调用存储在位置 0,然后在位置 1:

*args[*argnum] = newstr(start, *len); //Generate the string through malloc

在这一行,我在这些位置有字符串:*args[0] 和 *args[1]

但是一旦我返回调用函数args[0] 有一个字符串,但 args[1] 显示“无法访问地址 0x0 处的内存”

最佳答案

*args[*argnum] 取消引用 args[*argnum] 处的指针。我想你的意思是

(*args)[*argnum] = newstr(start, *len);

关于从函数返回字符串数组时无法访问地址 0x0 处的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10262133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com