C - Segmentation Fault when allocating space for strings?

Up vote 0 down vote favorite share g+ share fb share tw.

This code must naturally sort an array of strings and it gives a segfault. Unfortunately, I don't know what input it gives segfault on, but I suppose that segfault occurs when allocating memory for strings. Maximum string length is 9000000.

Here it is: #include #include #include void strsort(char **string, int left, int right) { char *comp, *temp; int i, j, p; while(left 0) j--; if(i > j) break; temp = stringi; stringi = stringj; stringj = temp; if(j == p) p = i; i++; j--; } stringp = stringj; stringj = comp; j--; if((j - left) = 'a') stringilen -= offset; else stringilen += offset; } stringi = (char *)realloc(stringi, len + 1); stringilen = '\0'; } strsort(string, 0, n - 1); for(i = 0; I = 'a') printf("%c", stringij - offset); else printf("%c", stringij + offset); ++j; } printf("\n"); } return 0; } c homework segmentation-fault link|improve this question asked Nov 27 '11 at 19:18user106821511.

13 It would be a good time to look into learning how to use a debugger so you can replay your tests and see what input is causing it to die. – birryree Nov 27 '11 at 19:20 Can you run this in a debugger and create a stack trace? An immediate error I see is that you never check whether the allocations actually worked, you have to ALWAYS check the return value of malloc as malloc will return NULL in out of memory situations.

– johannes Nov 27 '11 at 19:23 You are right, but I already have spent some time doing this and failed to find such input. It's a good idea to try again, though. – user1068215 Nov 27 '11 at 19:24 1 My wild guess also is that strsort is being called recursively and you get a stack overflow.

But a debugger will tell. (or going through the logic in detail) – johannes Nov 27 '11 at 19:26 1 @johannes +1 for using stack overflow in your answer :) tee hee – Ahmed Masud Nov 27 '11 at 22:46.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions