c - Program using read() entering into an infinite loop -
1oid readbinary(char *infile,hxmap* assetmap) { int fd; size_t bytes_read, bytes_expected = 100000000*sizeof(char); char *data; if ((fd = open(infile,o_rdonly)) < 0) err(ex_noinput, "%s", infile); if ((data = malloc(bytes_expected)) == null) err(ex_oserr, "data malloc"); bytes_read = read(fd, data, bytes_expected); if (bytes_read != bytes_expected) printf("read %d of %d bytes %d\n", \ bytes_read, bytes_expected,ex_dataerr); /* ... operate on data ... */ printf("\n"); int i=0; int counter=0; char ch=data[0]; char message[512]; message* newmessage; while(i!=bytes_read) { while(ch!='\n') { message[counter]=ch; i++; counter++; ch =data[i]; } message[counter]='\n'; message[counter+1]='\0'; //--------------------------------------------------- newmessage = (message*)parser(message); messageprocess(newmessage,assetmap); //-------------------------------------------------- //printf("idnum %e\n",newmessage->idnum); free(newmessage); i++; counter=0; ch =data[i]; } free(data); }
here, have allocated 100mb of data malloc, , passed file big enough(not 500mb) size of 926kb about. when pass small files, reads , exits charm, when pass big enough file, program executes till point after hangs. suspect either entered infinite loop, or there memory leak.
edit better understanding stripped away unnecessary function calls, , checked happens, when given large file input. have attached modified code
void readbinary(char *infile,hxmap* assetmap) { int fd; size_t bytes_read, bytes_expected = 500000000*sizeof(char); char *data; if ((fd = open(infile,o_rdonly)) < 0) err(ex_noinput, "%s", infile); if ((data = malloc(bytes_expected)) == null) err(ex_oserr, "data malloc"); bytes_read = read(fd, data, bytes_expected); if (bytes_read != bytes_expected) printf("read %d of %d bytes %d\n", \ bytes_read, bytes_expected,ex_dataerr); /* ... operate on data ... */ printf("\n"); int i=0; int counter=0; char ch=data[0]; char message[512]; while(i<=bytes_read) { while(ch!='\n') { message[counter]=ch; i++; counter++; ch =data[i]; } message[counter]='\n'; message[counter+1]='\0'; i++; printf("idnum \n"); counter=0; ch =data[i]; } free(data); }
what looks is, prints whole lot of idnum
's , poof segmentation fault
i think interesting behaviour, , me looks there problem memory
further edit changed i!=bytes_read
gives no segmentation fault. when check i<=bytes_read
blows past limits in innerloop.(courtesy gdb)
try following loop. basically, refactors implementation there 1 place i
incremented. having 2 places what's causing trouble.
#include <stdio.h> #include <string.h> int main() { const char* data = "first line\nsecond line\nthird line"; unsigned int bytes_read = strlen(data); unsigned int = 0; unsigned int counter = 0; char message[512]; while (i < bytes_read) { message[counter] = data[i]; ++counter; if (data[i] == '\n') { message[counter] = '\0'; printf("%s", message); counter = 0; } ++i; } // if data didn't end newline if (counter) { message[counter] = '\0'; printf("%s\n", message); } return 0; }
or, take "don't reinvent wheel" approach , use standard strtok
call:
#include <stdio.h> #include <string.h> int main() { char data[] = "first line\nsecond line\nthird line"; char* message = strtok(data, "\n"); while (message) { printf("%s\n", message); message = strtok(null, "\n"); } return 0; }
Comments
Post a Comment