program that segment faults..

This is a discussion on program that segment faults.. within the C Programming forums, part of the General Programming Boards category; Hi, Code: main() { int *p=1 ,j=1; printf("val of *p:%d\n",*p); // good example for segment fault printf("val of p:%x\n",p); printf("val ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    139

    program that segment faults..

    Hi,

    Code:
    main()
    {
    	int *p=1,j=1;
    	printf("val of *p:%d\n",*p);  //good example for segment fault
    	printf("val of p:%x\n",p);
    	printf("val of &p:%x\n",&p);
    	p = &j;                                  //Introduction of this nullifies seg fault
    	*p = 2;                                 // statement 3
    	printf("val of p:%x\n",p);
    	printf("Val of p:%d\n",*p);
    	return 0;
    
    }

    My questions:

    1. What is the difference b/w *p=1 in definition and *p=2 in statement 3.
    2. Shouldn't both the statements mean one and same? (*p=1 and *p=2);
    3. Why is the statement int *p= 1 has p=1 and it is different when it comes to statment *p =3.

    Thanks in advance

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    A pointer is an address.

    int *p = 1; Should segfault immediately because the pointer is not pointing anywhere. (i.e. p has some invalid memory address by default just like a variable has some random value when not initialized).

    On the other hand:

    int j =1;
    int *p = &j;
    *p = 2;

    Is correct, between p points to the memory location where the value of j resides. (which is a valid memory address)

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    * in definition means you are defining a pointer and when you use = when you define something you initialize it (the pointer not what it points to).
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    Quote Originally Posted by claudiu View Post
    A pointer is an address.

    int *p = 1; Should segfault immediately because the pointer is not pointing anywhere. (i.e. p has some invalid memory address by default just like a variable has some random value when not initialized).

    On the other hand:

    int j =1;
    int *p = &j;
    *p = 2;

    Is correct, between p points to the memory location where the value of j resides. (which is a valid memory address)
    Okay as per my question 2, doubt is

    now u have declared *p =2;
    then shouldn't this also mean p=2......Now correct me if i am wrong

    my question was why when i define
    int *p =1; it takes p =1

    and while it not the same other way round *p=2;

    Shouldn't it mean that val pointed by p is 2 in both the cases....

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    int *p = 1;
    
    printf("val of *p:%d\n",*p);  //good example for segment fault
    This creates a pointer 'p' and set the address it holds to 1. 1 isn't very likely a valid address for this pointer to point to, you should never just arbitrarily pick an address to assign to a pointer. Dereferencing the pointer later in the printf attempts to access the value stored at that address which like I just said most likely isn't going to be an address you are meant to access. This causes the segfault, an invalid address you are attempting to read from.



    Code:
    int j;
    
    ...
    
    p = &j;
    *p = 2;
    The above points p to j. That is, the address stored in p is the same as the address of the variable j. Since p now has a valid address, later attempts to dereference the pointer are fine. The *p = 2 line sets the value at where p points to, which is 'j', to 2. So the value now stored in j is 2.




    You're confusion seems to stem from the syntax or the following lines:
    Code:
    int *p = 1;
    *p = 2;
    These two statements are two completely different things though they look quite similar. They are drastically different in intent from the compiler's standpoint. The first creates the pointer and assigns an address to the pointer, the second has nothing to do with assigning an address but does assign a value at the address currently pointed to by p.
    Last edited by hk_mp5kpdw; 04-22-2010 at 10:06 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by sanddune008 View Post
    Okay as per my question 2, doubt is

    now u have declared *p =2;
    then shouldn't this also mean p=2......Now correct me if i am wrong

    my question was why when i define
    int *p =1; it takes p =1

    and while it not the same other way round *p=2;

    Shouldn't it mean that val pointed by p is 2 in both the cases....
    Sorry I was slightly wrong in my explanation of point 1) -- Lack of sleep

    hk_mp5kpdw, explained things better.

    Indeed in case 1) int *p = 1; this is setting the address pointed to by p to 1, which is probably an invalid memory location. After you try to dereference the pointer you will get a segfault.

    I think your misunderstanding stems from the use of the star(*) operator in two circumstances:

    1) int *p; This is telling the compiler here is a pointer p to type int. If you initialize *p on the same line you don't initialize the value contained at the memory location pointed to by p like I wrongly said , but rather the address where p points.

    2) A subsequent use of the star(*) operator behind the pointer name refers to the value not the address.

    *p = 5; Set the value at address p to 5. However, if p does not point to a valid memory location either dynamically allocated or statically allocated the program will segfault.

    I apologize if I confused you even further the previous post.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    Thanks all for your time......

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    Code:
    main()
    {
    	char *s = "Hello World";
    	
            printf("Add of s:%x\n",s); 
    	
           *s = 'H';                  //Segment fault
    
    	return 0;
    
    }
    Meanwhile why does this segment faults?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because s points to non-writable memory (well, non-writable to you and/or your program). Trying to write to non-writable memory is a Bad Thing.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by HowardL View Post
    You have assigned 'H' to *s.
    'H' evaluates to ascii 0x48.
    If you try to access the data stored at address 0x48 you get slapped!

    You can only access memory that has been allocated to your variables as of the time of your access attempt.
    Ignore this post. And see tabstop's.

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    Quote Originally Posted by tabstop View Post
    Because s points to non-writable memory (well, non-writable to you and/or your program). Trying to write to non-writable memory is a Bad Thing.
    Does this mean that with following definition

    char *s = "Hello World";

    1. what do you mean by non-writable memory? Does this mean there is
    no way you can change the content of *s?

    2. When all will the data written to the non-writable memory.


    Thanks

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char *s <-- this, means to declare a pointer to char named s
    = "Hello World"; <-- this means to make that pointer point to a string literal (read: cannot change the contents of said string literal).

    You can make s point at something else.
    You should never try to change the values inside the string literal to be something else.


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

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    The whole problem is that this is kinda wrong:
    Code:
    char *s = "Hello World";
    And should be:
    Code:
    const char *s = "Hello World";
    Of course, you can still change what s points to, as quzah said. But if the pointer is dereferenced, the resulting type is of a "const char" and can't be changed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-14-2007, 11:34 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  3. Looking for feedback on program segment
    By avron in forum C++ Programming
    Replies: 4
    Last Post: 05-07-2007, 05:38 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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