[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


...definition of "Segmentation Fault" - Where is...

<< Back to: comp.unix.aix Frequently Asked Questions (Part 5 of 5)

Question by SLayne
Submitted on 7/10/2003
Related FAQ: comp.unix.aix Frequently Asked Questions (Part 5 of 5)
Rating: Rate this question: Vote
What is the definition of "Segmentation Fault" - Where is this defined?


Answer by David
Submitted on 7/16/2003
Rating:  Rate this answer: Vote
Segmentation Fault occurred when running c program that is reading or writing non-exist segment(physical memory space). Example, declare an array of int x[5] and you try putting or reading 6th item(s) that the cpu system to crash.

 

Answer by Dragon Dave
Submitted on 8/25/2003
Rating:  Rate this answer: Vote
It can be a sporadic occurance; for example, in the program below, if you type 'Dragon' it works quite happily. It's overwriting some part of the memory with something it shouldn't be. But it doesn't segfault until you get to something like 'Dragon Dave is overwriting your memory!'. This is a buffer overrun, and that's *bad*, and a very, very large hole in your programs security.

#include "stdio.h"
int main()
{
/* allocate 5 bytes for the string */
   char name[5];
/* enter a string - possibly much longer */
   gets(name);
/* display the string */
   printf("Hello, %s.",name);
}

 

Answer by kon
Submitted on 10/22/2003
Rating:  Rate this answer: Vote
segmentation faults may also occur in case of hardware errors, f.e. hard-disk or Memory failure. to find out, try running memtest86 and/or any hard-disk diagnostic tools from the manufacturer

 

Answer by prashant
Submitted on 11/4/2003
Rating:  Rate this answer: Vote
segmentation fault is a type of error which occurs when u try to access a non existant physical memory address. Sometimes during execution of a C program u freed  memory and then within the scope of the same program try again to use the same memory, then also the seg. fault occurs, this is due to the fact that untill the completion of the program the memory utilised is not returned to the operating system.

 

Answer by Liang Yu
Submitted on 12/11/2003
Rating:  Rate this answer: Vote
This is often caused by improper usage of pointers in the source code, dereferencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

 

Answer by Ryan
Submitted on 1/18/2004
Rating:  Rate this answer: Vote
Segmentation fault is a bad error.

 

Answer by ashwin
Submitted on 2/14/2004
Rating:  Rate this answer: Vote
if u allot some memory for array in the beginning of the programme and if u try to store the more numbers in the array and if it requires more memory that that has been allocated then it will result in segmentation fault  

 

Answer by kilgroja
Submitted on 3/26/2004
Rating:  Rate this answer: Vote
Ok. I understand. But...what about this:
void *memPtr = malloc(25);  //causes coredump

But, if I change the statement to:
void *memPtr = malloc(24);  //ok

Anything > 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 logicTRANCE
Submitted on 4/17/2004
Rating:  Rate this answer: Vote
did you check on other machines? other than Compaq Tru64....
if it runs properly on other machines, report compaq call centre or equivalent...it must be a bug....

also, if you are using C++, try using
void *memPtr = new(24);

i've never tried it..but i think it should work...
goodluck!
Happy Programming!

 

Answer by Nothix
Submitted on 4/26/2004
Rating:  Rate this answer: Vote
Thanks for the pointer Tip!

 

Answer by bt
Submitted on 5/4/2004
Rating:  Rate this answer: Vote
get as point please

 

Answer by anand
Submitted on 6/19/2004
Rating:  Rate this answer: Vote
This is often caused by improper usage of pointers in the source code, dereferencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

 

Answer by dsff
Submitted on 10/29/2004
Rating: Not yet rated Rate this answer: Vote
address

 

Answer by An
Submitted on 2/13/2005
Rating: Not yet rated Rate this answer: Vote
//n is a pointer to an array of nodes elements
//heapArr is a pointer in the heap class to the array where
//the heap nodes are stored
bool heap::buildHeap(HeapNode *n, const int nodes)
{

  //delete previous heap if one exists                                                                                                        
  if(curSize != 0)
  {
    delete [] heapArr;
    heapArr = NULL;
    curSize = 0;
    maxSize = 0;
  }
                                                                
  heapArr = new HeapNode[nodes]; //segmentation Fault
    if (heapArr != NULL)
  {
    maxSize = nodes;
    for (int a = 1; a <= maxSize; a++)
    {
      cout << "begin copy" <<endl;
      heapArr[a] = n[a];
      curSize = a;
      percDown(a);
    }
    return true;
  }
  else
    maxSize = 0;
  return false;
}

 

Answer by vicky
Submitted on 6/20/2005
Rating: Not yet rated Rate this answer: Vote
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: Vote
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 harik
Submitted on 8/18/2005
Rating: Not yet rated Rate this answer: Vote
Pointer answer is a good one

 

Answer by RenjithLal
Submitted on 9/18/2005
Rating: Not yet rated Rate this answer: Vote
A segmentation fault occurs when your program tries to access memory locations that haven't been allocated for the program's use.

 

Answer by rcw
Submitted on 12/26/2005
Rating: Not yet rated Rate this answer: Vote
Why the following will cause segmentation fault ?

void calculatedouble()
{
  double* d1;
  double* d2;
  double d;

*d1 = 2.3;
*d2 = 3.7; //segmentation fault here

d = (*d1) * (*d2);
cout<< d <<endl;
}

 

Answer by Nilesh bhosle
Submitted on 1/2/2006
Rating: Not yet rated Rate this answer: Vote
segmentation fault occurs due to 2/3 reasons.it may occur due to long array size,due to use of pointers in u r code.

 

Answer by siva reddy
Submitted on 1/23/2006
Rating: Not yet rated Rate this answer: Vote
if u allot some memory for array in the beginning of the programmer and if u try to store the more numbers in the array and if it requires more memory that that has been allocated then it will result in segmentation fault  

 

Answer by nerdy_nerd
Submitted on 2/1/2006
Rating: Not yet rated Rate this answer: Vote
seg fault can occur when something goes wrong with the program that is using something which is not exist or faulty
I would say that these kinds of error should be avoid at the begning of the program,otherwise it will cost a lot in the future. for example if you are writing a program for a big companies, then its too late to chase those seg fault errors. When writing the program,  bear in mind that when you start always be consistent and focus on what you do. thats always a good practice. Hope this would give you some kind of thought in writing a well matured programs. If you have further doubts let me know.

NerDy_NeRd

 

Answer by Prem
Submitted on 2/6/2006
Rating: Not yet rated Rate this answer: Vote
To solve this problem use gdb
run ur code as
gcc -ggdb -o obj file.c
gdb r some_argu line no

or use
gdb bt go through that part of code and come out of this deadly problem

 

Answer by bulla
Submitted on 2/7/2006
Rating: Not yet rated Rate this answer: Vote
This is often caused by improper usage of pointers in the source code, dereferencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

 

Answer by dude
Submitted on 3/12/2006
Rating: Not yet rated Rate this answer: Vote
sssuck on ma balls all of u

 

Answer by shrikant
Submitted on 5/29/2006
Rating: Not yet rated Rate this answer: Vote
segmentation fault is one tye of error occuerd during running the program its nothing but referencing illegal or invalid memory area.

 

Answer by Nitin
Submitted on 6/20/2006
Rating: Not yet rated Rate this answer: Vote
I am also getting same problem during malloc,what could be the solution for that?

 

Answer by bipin
Submitted on 8/2/2006
Rating: Not yet rated Rate this answer: Vote
well thhnx a lot 4 te ans on pointers

 

Answer by Valdo
Submitted on 11/15/2006
Rating: Not yet rated Rate this answer: Vote
A very simple c program:
hd.c
compiled with gcc -o hd hd.c
run: ./hd <somefile>

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?

TIA

Valdo

#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 kumar
Submitted on 11/25/2006
Rating: Not yet rated Rate this answer: Vote
when we call a main function with in main,
and in main we initialised two variables.
this makes infinite loop.while running these variables get stored in different locations on memory. when there is no memory avialable to store these variables after some running time this gives segmentation fault.  

try one example.

























































































 

Answer by santosh
Submitted on 12/27/2006
Rating: Not yet rated Rate this answer: Vote
#include<stdio.h>
int main()
{
char *p = "srikanth";
   p[3]='u';
printf("%s",p);
}
// here also u get seg fault
// can anyone give me the reason

 

Answer by pintu
Submitted on 2/22/2007
Rating: Not yet rated Rate this answer: Vote
error is due to the address

 

Answer by Devagi_n
Submitted on 3/8/2007
Rating: Not yet rated Rate this answer: Vote
Segmentation fault occurs when a program attempts to access the memory location that is not allowed to access.

 

Answer by yuggi
Submitted on 3/11/2007
Rating: Not yet rated Rate this answer: Vote
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 ! ~

 

Answer by ituns
Submitted on 4/15/2007
Rating: Not yet rated Rate this answer: Vote
there are many soloution.
If u care,pm to me.
tuongtuong@mac.com

 

Answer by Apprentice
Submitted on 5/9/2007
Rating: Not yet rated Rate this answer: Vote
Hello!

I have a "Segmentation fault" when i run xchat. Anybody can help me?

Appreciate

 

Answer by harha b
Submitted on 5/14/2007
Rating: Not yet rated Rate this answer: Vote
any variable which is trying to acess memroy has which has been not allocated or trying to acess the freed memory will result in segmentation fault.

 

Answer by jhetfield18
Submitted on 5/14/2007
Rating: Not yet rated Rate this answer: Vote
Let's say I have an Array[6][6] and its integer.If we try to overwrite some of its content with a character '*' and then try to print the array on the screen but with the '*' wherever we've put it.Is it possible to get a segfault?

 

Answer by Panshul
Submitted on 6/5/2007
Rating: Not yet rated Rate this answer: Vote
segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way it is not allowed to (eg, attempts to write a read-only location).

 

Answer by heavymetal
Submitted on 7/1/2007
Rating: Not yet rated Rate this answer: Vote
I fixed this problem by declaring pointers as arrays

 

Answer by gopikrishna
Submitted on 7/6/2007
Rating: Not yet rated Rate this answer: Vote
segmntation fault occurs when we attempt to acess  other memory area.
   ex:
     chart *s[10]="0123456789";
      *s='h';


this shows segmentation fault

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

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.

 

<< Back to: comp.unix.aix Frequently Asked Questions (Part 5 of 5)


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.