【发布时间】:2020-03-23 03:40:46
【问题描述】:
我有这个代码:
char **data;
int start = 0;
data = malloc(all_names * sizeof(char*));
fd=open(argv[1],O_RDWR|O_CREAT,S_IRWXU);
for(i=0; i<all_names; i++){
data[i] = malloc((MAX_SIZE+1)*sizeof(char));
int end = atoi(positions[i]);
lseek(fd,0,start);
read(fd,data[i],(end-start));
data[i][end - start] = 0; //line edited in after answer
start = end;
}
qsort(data, all_names, sizeof(char*), strcmp);
for(int i=0; i<all_names; ++i)
{
printf("%s\n", data[i]);
}
/*//print data array
start = 0;
for(i=0; i<all_names; i++){
int end = atoi(positions[i]);
for(j=0;j<(end-start) ;j++){
printf("%c",data[i][j]);
}
printf("\n");
}*/
运行它时我得到的是尝试打印时出现的段错误。
如果我注释掉qsort 和打印for,并在打印数据数组 部分中进行注释,我可以按我插入的顺序获得所有条目。
如果我离开 qsort,但保留 for 循环作为我的打印方法,我仍然会遇到 seg 错误。
1.数据数组中的字符串来自文件,因此它们可能不是以空值结尾的。但是,我犹豫是否要添加一个空字节,因为当我将排序后的数组写回到文件中时,它一定不存在。
- **positions 数组包含每个条目的起点,除了第一个。例如。如果我插入“alpha one”,然后插入“beta 2”,那么 position[0] = 9。
请告诉我进一步解释我没有做好解释的任何事情。谢谢。
编辑:整个代码导致我找不到问题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#define MAX_SIZE 50
int updateCounter(int pcounter, char *str){
int m,charcount = 0;
for(m=0; str[m]; m++) {
charcount ++;
}
//charcount--;
printf("chars: %d \n", charcount);
pcounter = pcounter + charcount;
printf("pcounter = %d \n", pcounter);
return pcounter;
}
int main(int argc, char *argv[]){
int option,i,j;
FILE *fptr;
char *name;
int dcounter,pcounter = 0;
int fd;
char **positions,**data;
int all_names=0; //keeps track of how many names are currently stored
int start = 0; //first byte of word to read in data.bin
char *filename = argv[2];
name=(char*)malloc((MAX_SIZE+1)*sizeof(char));
do{
printf("MENU: \n 1.Insert \n 2.Delete \n 3.Search \n 4.Display \n");
printf("Please choose 1-4\n");
scanf("%d", &option);
while(getchar() != '\n');
//Insert
if(option==1){
printf("Insert name: ");
fgets(name,MAX_SIZE,stdin);
name[strcspn(name,"\n")]=0;
fd=open(argv[1],O_RDWR|O_CREAT|O_APPEND,S_IRWXU);
write(fd,name,strlen(name));
pcounter = updateCounter(pcounter, name);
char passpos[5];
sprintf(passpos,"%d",pcounter); //int to string
fd=open(argv[2],O_RDWR|O_CREAT|O_APPEND,S_IRWXU);
write(fd,passpos,3);
write(fd," ",1);
all_names++;
printf("all names: %d\n",all_names);
positions = malloc(all_names * sizeof(char*));
//create pos array
fd=open(argv[2],O_RDWR|O_CREAT,S_IRWXU);
for(i=0; i<all_names; i++){
positions[i] = malloc((MAX_SIZE+1)*sizeof(char));
for(j=0; ;j++){
read(fd,&positions[i][j],1);
if (positions[i][j] == ' ') {
break;
}
}
}
//print pos array
for(i=0; i<all_names; i++){
printf("%s\n", positions[i]);
}
//create data array
data = malloc(all_names * sizeof(char*));
fd=open(argv[1],O_RDWR|O_CREAT,S_IRWXU);
for(i=0; i<all_names; i++){
data[i] = malloc((MAX_SIZE+1)*sizeof(char));
int end = atoi(positions[i]);
lseek(fd,0,start);
read(fd,data[i],(end-start));
data[i][end - start] = 0;
start = end;
}
qsort(data, all_names, sizeof(char*), strcmp);
for(int i=0; i<all_names; ++i)
{
printf("%s\n", data[i]);
}
/*//print data array
start = 0;
for(i=0; i<all_names; i++){
int end = atoi(positions[i]);
for(j=0;j<(end-start) ;j++){
printf("%c",data[i][j]);
}
printf("\n");
}*/
}
}while(1);
}
【问题讨论】:
-
请显示minimal reproducible example、问题以及重现问题所需的输入。
-
与问题无关,但是如果你之后读取
start和end并且可以得出正确的长度,为什么你分配MAX_SIZE+1字节? -
关于:
data = malloc(all_names * sizeof(char*));1) 强烈建议改为调用calloc(),以便之后很容易释放()内存。 (注意:发布的代码未能释放分配的内存,导致大量内存泄漏。 -
关于:
char *filename = argv[2];用户的预期输入是什么? 2) 在没有首先检查argc以确保用户实际输入预期数量的命令行参数的情况下,永远不要访问超过argv[0] -
OT:为了便于阅读和理解:1) 请始终缩进代码。在每个左大括号 '{' 后缩进。在每个右大括号 '}' 之前取消缩进。建议每个缩进级别为 4 个空格
标签: c string segmentation-fault printf dynamic-arrays