从数组读取时出现分段错误
Segmentation fault upon reading from an array
我写了一个小程序来说明我 运行 遇到的问题。该程序应将 "buff[200]" 的内容复制到数组 "output" 的第一个位置。执行复制后,我多次读取该值以查看它何时消失,因为一旦我尝试访问 driverFunc 范围之外的数据就会出现分段错误。我知道我正在创建一个包含 6 个位置的数组,但只将数据添加到第一个位置,这最终将在一个循环内填充输出数组的其余部分。我对我的用例也有要求,我需要能够扩展这个数组的大小。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 1035
int driverFunc(char ** output, int * sizeOfOutput) {
int rows = 5;
char buff[200] = "hello world";
output = malloc(rows * sizeof(char *)); //malloc row space
//malloc column space
for (int i = 0; i < rows; i ++) {
output[i] = malloc(BUFFER_SIZE * sizeof(char));
}
//copy contents of buff into first position of output
strncpy(output[0], buff, BUFFER_SIZE-1);
printf("Output 1: %s\n", output[0]); //verify that it's there
//resize the array
output = realloc(output, (rows+1) * sizeof(char *));
//allocate space for the new entry
output[rows] = malloc(BUFFER_SIZE * sizeof(char));
*sizeOfOutput = rows;
//verify that it's still there
printf("Output 2: %s\n", output[0]);
return 0;
}
int main() {
char ** outputs;
int sizeOfOutput;
driverFunc(outputs, &sizeOfOutput);
//verify that we can do useful things with our output
printf("Reported size: %d\n", sizeOfOutput);
printf("Captured output: %s\n", outputs[0]); //segfault
}
预期输出:
Output 1: hello world
Output 2: hello world
Reported size: 5
Captured output: hello world
收到输出:
Output 1: hello world
Output 2: hello world
Reported size: 5
Segmentation fault (core dumped)
您正在将 outputs
作为值传递给 driverFunc
:
driverFunc(outputs, &sizeOfOutput);
它的值将被传递给函数而不是 returned。因此,当您在以下位置使用它时:
printf("Captured output: %s\n", outputs[0]);
outputs
仍未初始化。
您需要将其作为参考传递(并相应地更改 driverFunc
):
driverFunc(&outputs, &sizeOfOutput);
或者只是return它:
outputs = driverFunc(&sizeOfOutput);
如果您要更改在 main
中声明的指针 outputs
的值
char ** outputs;
在一个函数中,该函数应该除了间接通过指针的引用指针。
因此函数的声明至少要像
int driverFunc(char *** output, int * sizeOfOutput);
并称赞
driverFunc( &outputs, &sizeOfOutput);
使用函数strncpy
strncpy(output[0], buff, BUFFER_SIZE-1);
没有多大意义。使用strcpy
更简单
strcpy( output[0], buff );
如果重新分配失败
output = realloc(output, (rows+1) * sizeof(char *));
指针output
之前的值将丢失。所以需要使用一个中间变量重新分配内存,并检查调用后的值是否等于NULL。
变量sizeOfOutput
应该设置为
*sizeOfOutput = rows + 1;
在 main 中,您应该释放函数中所有分配的内存。
我写了一个小程序来说明我 运行 遇到的问题。该程序应将 "buff[200]" 的内容复制到数组 "output" 的第一个位置。执行复制后,我多次读取该值以查看它何时消失,因为一旦我尝试访问 driverFunc 范围之外的数据就会出现分段错误。我知道我正在创建一个包含 6 个位置的数组,但只将数据添加到第一个位置,这最终将在一个循环内填充输出数组的其余部分。我对我的用例也有要求,我需要能够扩展这个数组的大小。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 1035
int driverFunc(char ** output, int * sizeOfOutput) {
int rows = 5;
char buff[200] = "hello world";
output = malloc(rows * sizeof(char *)); //malloc row space
//malloc column space
for (int i = 0; i < rows; i ++) {
output[i] = malloc(BUFFER_SIZE * sizeof(char));
}
//copy contents of buff into first position of output
strncpy(output[0], buff, BUFFER_SIZE-1);
printf("Output 1: %s\n", output[0]); //verify that it's there
//resize the array
output = realloc(output, (rows+1) * sizeof(char *));
//allocate space for the new entry
output[rows] = malloc(BUFFER_SIZE * sizeof(char));
*sizeOfOutput = rows;
//verify that it's still there
printf("Output 2: %s\n", output[0]);
return 0;
}
int main() {
char ** outputs;
int sizeOfOutput;
driverFunc(outputs, &sizeOfOutput);
//verify that we can do useful things with our output
printf("Reported size: %d\n", sizeOfOutput);
printf("Captured output: %s\n", outputs[0]); //segfault
}
预期输出:
Output 1: hello world
Output 2: hello world
Reported size: 5
Captured output: hello world
收到输出:
Output 1: hello world
Output 2: hello world
Reported size: 5
Segmentation fault (core dumped)
您正在将 outputs
作为值传递给 driverFunc
:
driverFunc(outputs, &sizeOfOutput);
它的值将被传递给函数而不是 returned。因此,当您在以下位置使用它时:
printf("Captured output: %s\n", outputs[0]);
outputs
仍未初始化。
您需要将其作为参考传递(并相应地更改 driverFunc
):
driverFunc(&outputs, &sizeOfOutput);
或者只是return它:
outputs = driverFunc(&sizeOfOutput);
如果您要更改在 main
中声明的指针outputs
的值
char ** outputs;
在一个函数中,该函数应该除了间接通过指针的引用指针。
因此函数的声明至少要像
int driverFunc(char *** output, int * sizeOfOutput);
并称赞
driverFunc( &outputs, &sizeOfOutput);
使用函数strncpy
strncpy(output[0], buff, BUFFER_SIZE-1);
没有多大意义。使用strcpy
strcpy( output[0], buff );
如果重新分配失败
output = realloc(output, (rows+1) * sizeof(char *));
指针output
之前的值将丢失。所以需要使用一个中间变量重新分配内存,并检查调用后的值是否等于NULL。
变量sizeOfOutput
应该设置为
*sizeOfOutput = rows + 1;
在 main 中,您应该释放函数中所有分配的内存。