-1
int main (void)   /*case :1 */
{
  int *ptr;
  *ptr = 4;
   return 0;
}

It is giving segmentation fault. in this case ptr is initialized with 0 address. I checked with gdb tool

int main (void)  /* case2 */
{ 
  int *ptr;
  *ptr = 4;
  printf ("%d", ptr);
  return 0;
}

it is not giving segmentation fault. output = 4;

int main (void)  /* case3 */
{ 
  int *ptr;
  ptr++;
 *ptr = 4;
  printf ("%d", ptr);
  return 0;
}

again it is giving segmentation fault. in case 2 and case 3 ptr value is "0xb76f7000" I have checked maps file of the process this address belong to library file. cat /proc/10489/maps

08048000-08049000 r-xp 00000000 08:06 788694     /home/durga/app
08049000-0804a000 r--p 00000000 08:06 788694     /home/durga/app
0804a000-0804b000 rw-p 00001000 08:06 788694     /home/durga/app
b754c000-b754d000 rw-p 00000000 00:00 0 
b754d000-b76f5000 r-xp 00000000 08:01 132823     /lib/i386-linux-gnu/libc-2.19.so
b76f5000-b76f7000 r--p 001a8000 08:01 132823     /lib/i386-linux-gnu/libc-2.19.so
b76f7000-b76f8000 rw-p 001aa000 08:01 132823     /lib/i386-linux-gnu/libc-2.19.so
b76f8000-b76fb000 rw-p 00000000 00:00 0 
b7711000-b7715000 rw-p 00000000 00:00 0 
b7715000-b7717000 r--p 00000000 00:00 0          [vvar]
b7717000-b7719000 r-xp 00000000 00:00 0          [vdso]
b7719000-b7739000 r-xp 00000000 08:01 132826     /lib/i386-linux-gnu/ld-2.19.so
b7739000-b773a000 r--p 0001f000 08:01 132826     /lib/i386-linux-gnu/ld-2.19.so
b773a000-b773b000 rw-p 00020000 08:01 132826     /lib/i386-linux-gnu/ld-2.19.so
bfb59000-bfb7a000 rw-p 00000000 00:00 0          [stack]
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • _in this case ptr is initialized with 0 address. i checked with gdb tool_ It is just indeterminate the value assigned to local pointer.. – LPs Jan 12 '17 at 09:26
  • every time it is zero only with out using library functions in my code i checked with gdb and objdump tools – Gopala krishna Jan 12 '17 at 09:34
  • 2
    @Gopalakrishna Because that's what GDB is doing for you, stop trying to define undefined behaviour. – Hatted Rooster Jan 12 '17 at 09:35
  • For Reference: http://stackoverflow.com/documentation/c/364/undefined-behavior/1472/use-of-an-uninitialized-variable#t=201701120935331590943 – alk Jan 12 '17 at 09:37
  • 1
    @Gopalakrishna "Undefined behavior" has a very specific meaning in C. It's not about "in my view" because your view here is irrelevant. "Undefined behavior" is a very specific important term to understand when programming in C. Look it up. – Art Jan 12 '17 at 09:41
  • Why do you do this? If you need a variable that stores 4: declare a variable not a pointer. In some cases, there is the need to convert a variable to a pointer, because an API function accepts only void* parameters and you(me) is to lazy to malloc() free() etc just for a single number. in this case no deferenciate but just convert void * p= (void *)4; To get it back : int d= (int) p; No deferenciate. The address =0x04 is not a valid address. – jurhas Jan 12 '17 at 09:41
  • You need to understand you can only write to memory that is yours. – Paul Ogilvie Jan 12 '17 at 09:44
  • _ptr_ _is_ _initialized_ _with_ _0_ _address_ is not true. It has random content which is pretty much the opposit of being initialized. – Gerhardh Jan 12 '17 at 09:45

4 Answers4

3

In all cases you're trying to write through an uninitialized pointer ( with *ptr = 4;), this is undefined behaviour, the behaviour of the program afterwards does not matter, you can never rely on consistent output. Whether it seems to "work" or not is irrelevant, no use in trying to define undefined behaviour.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
1

The one liner: Dereferencing an uninitialized pointer (which points to an invalid address) invokes undefined behavior.

To elaborate, ptr, in your case, is an automatic local variable. Unless initialized explicitly, it contains indeterministic value. In other words, the contents of the pointer variable is indeterminate. It may seem to point to some valid memory location, but that memory location need not to be valid from your program context.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

ptr is a pointer and you are deferencing it without making it point to some valid memory location, which will lead to undefined behavior.

int *ptr = malloc(sizeof(int));

Now ptr is pointing to some valid memory location to which you can write.

Gopi
  • 19,784
  • 4
  • 24
  • 36
  • why it is allowing to change the first 4 bytes of library – Gopala krishna Jan 12 '17 at 09:28
  • @Gopalakrishna Understand what is wrong in your code. When you have undefined behavior anything might happen and finding an explanation for it doesn't make sense. `ptr` is a pointer and you are trying to dereference it and it is uninitialized – Gopi Jan 12 '17 at 09:30
  • @Gopalakrishna - it could change the first four bytes of anywhere you have write access to. – cdarke Jan 12 '17 at 09:31
0

int *ptr only declares a pointer to an int, it does not declare an int to point at - that's for you to do.

Automatic variables in C (and C++) are uninitialised (see Non-static variable initialization) . The actual value of an uninitialised variable, like your int *ptr, is indeterminate. It will probably be consistent until you change some code or change the compiler, but that is not certain and can never be relied on.

So why doesn't C initialise pointers? Performance. In a high-level language things like initialisation are done for you, or there is a special "uninitialised" value (like None in python and undef in Perl and Ruby), but you pay the performance cost.

In C you, the programmer, are expected to keep track of everything, including what every pointer is pointing at.

Community
  • 1
  • 1
cdarke
  • 42,728
  • 8
  • 80
  • 84
  • `Variables in C (and C++) are uninitialised.`..sorry, but that's an overstatement. Thick of static storage. – Sourav Ghosh Jan 12 '17 at 09:44
  • @SouravGhosh Yeh, OK. Automatic variable are uninitialised. I'm guessing that this OP won't know the difference so its safer to use the overstatement. Personally I explicitly initialise off-stack data too, but that's just me. – cdarke Jan 12 '17 at 09:46