Thread: program that segment faults..

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    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....

  2. #2
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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