Question:
Why am I getting a segmentation fault?
Sean
2012-09-29 17:16:31 UTC
I have localized the error to this while loop:
******************************************
int dcount = 0;
int i = nameLength+1;
int count =0;
char *result;
while(dcount < 3){
//Does not enter loop. Segmentation fault error generated in where clause.
...
}
*******************************************
Any idea why I keep getting this error?
"Segmentation fault (core dumped)"
Four answers:
?
2012-09-29 18:01:38 UTC
nameLength ?? in your code it is not defined.



are you trying to do some access to your result??



it is not initialized. So any access like result[1] or result[0], will give you segmentation error due to memory violation.



if you are using gcc .you can use gdb for debugging. But you need to compile with symbols. This is how you do it.

gcc -g filename.c -o filename.o





gdb filename.o



followed by



run

will tell you where the segmentation fault is













char *getSalt(int nameLength, char *hashTemp){

char *result = (char*)malloc(sizeof(char)*20);

int i, c;

i = nameLength+1;

c = 0;



//printf("Starting index: %d\n",(nameLength+1));

while(c < 3){

if(hashTemp[i] == '$'){

c++;

}

result[c] = hashTemp[i];

c++;

i++;

}

result[c] = '\0';

printf("Ending index: %d\n",i);

return result;

}





you havent initialized *result
green meklar
2012-09-30 23:52:04 UTC
You declared result, but never gave it a value. Then you try to work with data in the memory it's pointing to. What memory does it point to? You have no idea. You never told it. It still just has a garbage value, pointing to some arbitrary location in memory that the program probably isn't allowed to access. It is not surprising that you get a segmentation fault when you do this; that is exactly what we would expect to happen.
Anas Imtiaz
2012-09-30 02:24:02 UTC
You have not assigned an array to the pointer *result but you are trying to access an index that does not exist.

I am guessing same goes with *hashTemp. You might not have passed a pointer that points to an array as an argument.
obierne
2016-12-26 14:20:12 UTC
comprise considerable(){ char *str; // it relatively is the priority str is a pointer to char -- it does no longer factor everywhere you may desire to provide it someplace to point it relatively is: str is a variable that could carry the commencing addresss of a string of characters you have no longer given it any fee yet so it ought to factor everywhere! printf("enter the string: "); scanf("%s",str); printf("npercentsn",&str[a million]); } #comprise #comprise int considerable(void){ // when you consider that considerable() returns an int say so char *str; str=malloc(one hundred); // make it factor someplace printf("enter the string: "); scanf("%s",str); printf("npercentsn",&str[a million]); // it relatively is okay // &str[a million] is the commencing handle of the 2nd letter which you entered via scanf() return 0; } // 'reason considerable() returns an int return one


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...