[英]Seg fault when printing string array line, works fine when printed character by character
I have this code: 我有以下代码:
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");
}*/
What I get when running it is a seg fault when trying to print. 我在运行时得到的是尝试打印时出现的段错误。
If I comment out qsort
and the printing for
, and comment in the print data array part, I get, as expected, all my entries in the order I inserted them in. 如果我注释掉
qsort
并打印for
注释,并在打印数据数组部分中注释, qsort
预期的顺序,我将按插入顺序获得所有条目。
If I leave qsort
out, but keep the for loop as my printing method, I still get a seg fault. 如果我不使用
qsort
,而将for循环保留为我的打印方法,则仍然会出现段错误。
1.The strings in the data array come from a file so they re probably not null terminated. 1.数据数组中的字符串来自文件,因此它们可能不会以null结尾。 However, I am hesitant to add a null byte cause it must not be there when I write the sorted array back in the file.
但是,我不愿意添加一个空字节,因为当我将排序后的数组写回到文件中时,它一定不能存在 。
Please tell me to further explain anything I haven't done a good job explaining. 请告诉我进一步解释所有我做得不好的事情。 Thanks.
谢谢。
Edit: Entire code cause it turns out I can't locate the problem 编辑:整个代码导致原来我找不到问题
#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);
} }
I'm going to guess that MAX_SIZE
is the maximum size of a string. 我猜想
MAX_SIZE
是字符串的最大大小。
But it should be sizeof(char*)
. 但是它应该是
sizeof(char*)
。
The function qsort
expects a parameter that indicates the size of each element. qsort
函数需要一个指示每个元素大小的参数。 The size of each element in a data type char**
is the size of char*
. 数据类型
char**
中每个元素的大小为char*
的大小。
Here's an example, similar to your program, that's useful for quick testing. 这是一个类似于您的程序的示例,对快速测试很有用。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char** argv) {
int all_names = argc;
char** data = argv;
qsort(data, all_names, sizeof(char*), strcmp);
for(int i=0; i<all_names; ++i)
{
printf("%s\n", data[i]);
}
return 0;
}
Regarding your updated code, after this line: 关于您的更新代码,在此行之后:
read(fd,data[i],(end-start));
You should null-terminate your string. 您应该以空值终止字符串。
data[i][end - start] = 0;
I say this with the assumption that end - start
does not account for a null-terminating character and that you're not storing null-terminated characters in the file you're reading from. 我这样说的前提是
end - start
不代表以零结尾的字符,并且您不在要读取的文件中存储以null结束的字符。
Regarding your additional update, if you don't want to write the null-terminator to the file, then just find the length of the string using strlen
before writing it to the file. 关于其他更新,如果您不想将null终止符写入文件,则只需在将字符串写入文件之前使用
strlen
查找字符串的长度。 Your other option would be to write a wrapper for qsort
that looks something like this. 您的另一个选择是为
qsort
编写一个包装,看起来像这样。
int cmp_string_block(const void* a, const void* b) {
return memcmp(a, b, MAX_SIZE);
}
And then pass that function to qsort
instead of strcmp
. 然后将该函数传递给
qsort
而不是strcmp
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.