What is the definition of "Segmentation Fault" - Where is... (original) (raw)
Answer by kilgroja
Submitted on 3/26/2004
Rating:
Rate this answer:
Ok. I understand. But...what about this:void *memPtr = malloc(25); //causes coredumpBut, if I change the statement to:void *memPtr = malloc(24); //okAnything > 24 in this particular program causes a coredump. My debugger (ladebug) tells me that I got a signal segmentation fault. The system I'm running on is a Compaq Tru64 system with oodles of memory. I can port it to another Tru64 system and the same thing happens.Can anyone give me a clue what might be happening?
Answer by vicky
Submitted on 6/20/2005
Rating: Not yet rated
Rate this answer:
A segmentation fault occurs when your program tries to access memory locations that haven't been allocated for the program's use. Here are some common errors that will cause this problem: scanf("%d", number); In this case, number is integer. scanf() expects you to pass it the address of the variable you want to read an integer into. But, the writer has fogotten to use the `&' before number to give scanf the address of the variable. If the value of number happened to be 3, scanf() would try to access memory location 3, which is not accessible by normal users. The correct way to access the address of number would be to place a `&' (ampersand) before number: scanf("%d", &number); Another common segmentation fault occurs when you try to access an array index which is out of range. Let's say you set up an array of integers: int integers[80]; If, in your program, you try to use an index (the number within the brackets) over 79, you will ``step out of your memory bounds'', which causes a segmentation fault. To correct this, rethink your array bounds or the code that is using the array.
Answer by swarup
Submitted on 7/21/2005
Rating: Not yet rated
Rate this answer:
A segmentation fault occurs when your program tries to access memory locations that haven't been allocated for the program's use. Here are some common errors that will cause this problem:scanf("%d", number);In this case, number is integer. scanf() expects you to pass it the address of the variable you want to read an integer into. But, the writer has fogotten to use the `&' before number to give scanf the address of the variable. If the value of number happened to be 3, scanf() would try to access memory location 3, which is not accessible by normal users. The correct way to access the address of number would be to place a `&' (ampersand) before number:scanf("%d", &number);Another common segmentation fault occurs when you try to access an array index which is out of range. Let's say you set up an array of integers:int integers[80];If, in your program, you try to use an index (the number within the brackets) over 79, you will ``step out of your memory bounds'', which causes a segmentation fault. To correct this, rethink your array bounds or the code that is using the array.
Answer by rcw
Submitted on 12/26/2005
Rating: Not yet rated
Rate this answer:
Why the following will cause segmentation fault ?void calculatedouble(){ double* d1; double* d2; double d;*d1 = 2.3;*d2 = 3.7; //segmentation fault hered = (*d1) * (*d2);cout<< d <<endl;}
Answer by Valdo
Submitted on 11/15/2006
Rating: Not yet rated
Rate this answer:
A very simple c program:hd.ccompiled with gcc -o hd hd.crun: ./hd The idea is print the file contents as hexadecimal. but after print the first 16 bytes, a segmentatio fault occurs.I am use this code before in UNIX and DOS/Windows, and never got this error. What Am I doing wrong?TIAValdo#include <stdio.h>#include <string.h>int main(int argc, char **argv) { FILE *f; unsigned char fs[15]; int i=0; long offset=0; f = fopen(argv[1], "r"); fseek(f, 0, SEEK_SET); while (!feof(f)) { offset =+ fread(fs, 1, 16, f); printf("%010d ", (offset-16)); for (i=0; i < 16; i++) { printf("%02X", fs[i]); printf((i==7)?("-"):(" ")); } printf("\t"); for (i=0; i < 16; i++) { printf("%c", fs[i]); } printf("\r\n"); } return 0;}
Answer by yuggi
Submitted on 3/11/2007
Rating: Not yet rated
Rate this answer:
an example for seg_fault............scanf("%d", x);---------------the routine scanf requires &x or for x to be a pointer you mentioned earlier .if this fails x does point to some a memory and if it is invalid you the the seg_fault.~All pain yes gain ! ~
FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.