执行 strcpy 时出现分段错误
Segmentation fault when doing strcpy
我已经开始学习C,并且一直在做一些小程序来学习。现在我正处于内存指针的阶段,我有一个快速的问题。
据我所知,我在下面编写的程序导航到 /proc/sys/net/ipv4/conf/ 目录并打印出那里列出的目录。我的意图是在函数 getSysctlNames() 中分配内存,然后在调用者 main() 中释放它。 getSysctlNames() 还应该 return 它找到的目录名称的数量。我读到过这不是最好的主意(在函数中分配并从调用者处解除分配),但我只是想愚弄和学习而不是遵循一种模式。我的问题是我在第 55 行遇到分段错误,而 运行 strcpy 函数。我不确定我做错了什么。如有任何帮助,我们将不胜感激!
int getSysctlNames(char** namesPointer);
int main(int argc, char **argv) {
char** namesPointer;
int numberOfNames = getSysctlNames(namesPointer);
printf("Number of files: %i\n", numberOfNames);
int i = 0;
for (i = 0; i <= numberOfNames; i++) {
printf("%s\n", namesPointer[i]);
free(namesPointer[i]);
}
free(namesPointer);
return 0;
}
int getSysctlNames(char** namesPointer) {
int numberOfNames = 0;
DIR *directory = opendir("/proc/sys/net/ipv4/conf/");
if (directory) {
struct dirent *directoryEntry;
while ((directoryEntry = readdir(directory)) != NULL) {
if (strcmp(directoryEntry->d_name, ".") != 0 &&
strcmp(directoryEntry->d_name, "..") != 0) {
numberOfNames++;
}
}
closedir(directory);
} else {
return -1;
}
if (numberOfNames == 0) return 0;
namesPointer = (char**) malloc(numberOfNames * sizeof(char*));
if (namesPointer == NULL) {
return -1;
}
directory = opendir("/proc/sys/net/ipv4/conf/");
if (directory) {
struct dirent *directoryEntry;
int i = 0;
while ((directoryEntry = readdir(directory)) != NULL) {
if (strcmp(directoryEntry->d_name, ".") != 0 &&
strcmp(directoryEntry->d_name, "..") != 0) {
namesPointer[i] = malloc((strlen(directoryEntry->d_name) + 1) * sizeof(char));
if (namesPointer[i] = NULL) {
int j = i - 1;
while (j >= 0) {
free(namesPointer[j]);
j--;
}
free(namesPointer);
return 0;
}
printf("%s\n", directoryEntry->d_name);
strcpy(namesPointer[i], directoryEntry->d_name);
printf("Got to 2\n");
i++;
}
}
closedir(directory);
return numberOfNames;
}
free(namesPointer);
return -1;
}
您分配内存,然后用 NULL 覆盖指针值:
namesPointer[i] = malloc((strlen(directoryEntry->d_name) + 1) * sizeof(char));
if (namesPointer[i] = NULL) {...
应该是
if (namesPointer[i] == NULL)
或
if (!namesPointer[i])
我已经开始学习C,并且一直在做一些小程序来学习。现在我正处于内存指针的阶段,我有一个快速的问题。
据我所知,我在下面编写的程序导航到 /proc/sys/net/ipv4/conf/ 目录并打印出那里列出的目录。我的意图是在函数 getSysctlNames() 中分配内存,然后在调用者 main() 中释放它。 getSysctlNames() 还应该 return 它找到的目录名称的数量。我读到过这不是最好的主意(在函数中分配并从调用者处解除分配),但我只是想愚弄和学习而不是遵循一种模式。我的问题是我在第 55 行遇到分段错误,而 运行 strcpy 函数。我不确定我做错了什么。如有任何帮助,我们将不胜感激!
int getSysctlNames(char** namesPointer);
int main(int argc, char **argv) {
char** namesPointer;
int numberOfNames = getSysctlNames(namesPointer);
printf("Number of files: %i\n", numberOfNames);
int i = 0;
for (i = 0; i <= numberOfNames; i++) {
printf("%s\n", namesPointer[i]);
free(namesPointer[i]);
}
free(namesPointer);
return 0;
}
int getSysctlNames(char** namesPointer) {
int numberOfNames = 0;
DIR *directory = opendir("/proc/sys/net/ipv4/conf/");
if (directory) {
struct dirent *directoryEntry;
while ((directoryEntry = readdir(directory)) != NULL) {
if (strcmp(directoryEntry->d_name, ".") != 0 &&
strcmp(directoryEntry->d_name, "..") != 0) {
numberOfNames++;
}
}
closedir(directory);
} else {
return -1;
}
if (numberOfNames == 0) return 0;
namesPointer = (char**) malloc(numberOfNames * sizeof(char*));
if (namesPointer == NULL) {
return -1;
}
directory = opendir("/proc/sys/net/ipv4/conf/");
if (directory) {
struct dirent *directoryEntry;
int i = 0;
while ((directoryEntry = readdir(directory)) != NULL) {
if (strcmp(directoryEntry->d_name, ".") != 0 &&
strcmp(directoryEntry->d_name, "..") != 0) {
namesPointer[i] = malloc((strlen(directoryEntry->d_name) + 1) * sizeof(char));
if (namesPointer[i] = NULL) {
int j = i - 1;
while (j >= 0) {
free(namesPointer[j]);
j--;
}
free(namesPointer);
return 0;
}
printf("%s\n", directoryEntry->d_name);
strcpy(namesPointer[i], directoryEntry->d_name);
printf("Got to 2\n");
i++;
}
}
closedir(directory);
return numberOfNames;
}
free(namesPointer);
return -1;
}
您分配内存,然后用 NULL 覆盖指针值:
namesPointer[i] = malloc((strlen(directoryEntry->d_name) + 1) * sizeof(char));
if (namesPointer[i] = NULL) {...
应该是
if (namesPointer[i] == NULL)
或
if (!namesPointer[i])