segmentation fault and memory fault

This is a discussion on segmentation fault and memory fault within the C Programming forums, part of the General Programming Boards category; hello all, could someone explain to why segmentation fault and memore fault would occur. what is the difference between the ...

  1. #1
    Unregistered
    Guest

    Question segmentation fault and memory fault

    hello all,
    could someone explain to why segmentation fault and memore fault would occur.

    what is the difference between the two any way?


    tnx

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Segmentation faults are when you usually screw up with pointers.
    A memory fault is when you screw up and forget your significant other's birthday, your anniversary, etc, and they kill you for it.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    Try to do the following and you will know the answer.

    1.) Try to free an already freed memory.

    2.) alloc () "n" (n= some number) of memory, get your pointer to point to outside the boundary of n and then try free' ing it.

    3.) alloc some memory inside a function called from, lets say main() and don't delete it. Try to access the memory from main now.


    Anoop.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > 3.) alloc some memory inside a function called from, lets say
    > main() and don't delete it. Try to access the memory from main
    > now.

    To access it, you would have to know where it was. If you knew
    where it was, you'd have its addressed. If you had its address,
    then you can make a pointer to it.

    Since this is 'alloc'ed memory (malloc/realloc/calloc), any function
    that has a pointer to its memory address, can access it without
    any problems.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    >> 3.) alloc some memory inside a function called from, lets say
    >> main() and don't delete it. Try to access the memory from >main
    >> now.

    >To access it, you would have to know where it was. If you knew
    >where it was, you'd have its addressed. If you had its address,
    >then you can make a pointer to it.

    >Since this is 'alloc'ed memory (malloc/realloc/calloc), any function
    >that has a pointer to its memory address, can access it without
    >any problems.

    My bad.

    3.) should have been

    void main ()
    {
    ...
    ...

    int *ptr;

    func(ptr);
    ....

    //try using ptr now.
    }

    func (ptr)
    {
    int i = 2;
    ....
    ptr = &i;
    ....
    }

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    3.) should have been

    void main ()
    {
    I don't know, something about that little snippet makes me cringe. Let's try it again, but correctly this time.
    Code:
    3.) should have been
    
    int main ( void ) 
    {
    For some odd reson people trust your answers more if you actually know what you're doing and can show it through your code. Using void main is a dead giveaway.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    Okay let me be absolutely ANSI correct.
    It should be

    int main()

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Okay let me be absolutely ANSI correct.
    It should be

    int main()
    No, it shouldn't. If you were absolutely ANSI correct then you would write code exactly as the standard tells you to. The ANSI/ISO standard EXPLICITY states that when main has no arguments it is declared as

    int main ( void )

    Which is close to yours, but not exactly, which is why you're wrong. And I'm going to go away and come back when I'm in a better mood, before everyone starts to hate me.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    Looks like today is not my day


    int main(void)
    int main(void)
    int main(void)
    int main(void)
    int main(void)
    int main(void)
    int main(void)
    int main(void)
    int main(void)
    int main(void)

    Thanks,
    Anoop.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Looks like today is not my day


    int main(void)
    int main(void)
    int main(void)
    int main(void)
    int main(void)
    int main(void)
    int main(void)
    int main(void)
    int main(void)
    int main(void)
    Unfortunately there is a problem...

    There can only be ONE MAIN FUNCTION! HAHAHA... Oh, I slay me.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Hehe, good one

    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    Today is a new day and boy am I feeling fresh?

    >>There can only be ONE MAIN FUNCTION! HAHAHA... Oh, I slay me.

    Well not exactly. there can be only one main function not MAIN


    hahhahahaa

  13. #13
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    I have never heard of a "memore fault". On unix a segfault means that you have dereferanced an address that is invalid or that you don't have rights to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 AM
  2. Segmentation Fault Problem: Urgent Help
    By bodydrop in forum C Programming
    Replies: 3
    Last Post: 05-05-2006, 08:02 PM
  3. Help on Segmentation Fault!!!
    By SweeLeen in forum Linux Programming
    Replies: 4
    Last Post: 02-19-2006, 10:33 AM
  4. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21