tags:

views:

449

answers:

6
int *i;
*i=123;
+27  A: 

Yes. There is no allocated memory to place the value 123.

Like initializing a variable like int is not guaranteed to be 0, an int* isn't guaranteed to be a safe address.

It also could potentially lead to data corruption.

Daniel A. White
it is not guaranteed to cause a segfault though. It could by happenstance point to a writable location.
Evan Teran
+30  A: 

It can, but there again it could do anything - it exhibits what the C Standard calls "undefined behaviour".

anon
It could even erase your hard drive. Not bloody likely, but it could.
Adam Rosenfield
Not as unlikely as it might seem. I always used to take the floppies out of my old Z80 CP/M box before I ran some newly assembled code to prevent exactly this happening!
anon
It could erase your hard drive as far as the C language is concerned. These days a process doing undefined behavior is still subject to the operating system's security rules, so chances are slim. Still, if you've used `sudo` recently...
Jason Orendorff
Unless you have a user account just to sandbox your C compiler, your user privileges are probably enough to delete anything you care about on your hard drive, even if it can't be fully erased.
Steve Jessop
Remember, the compiler is allowed to generate anything at all for undefined behavior, so starting a game of pong is allowed!
Tim Williscroft
@Tim: Or even starting nethack! http://www.feross.org/gcc-ownage/
Eric Perko
+1  A: 

int *i; This allocates a memory for pointer but this variable is not initialized so it's value is totally random. Then you dereference it so you have a random address and perform a write to this place in the memory but 1) you don't know where the place is 2) this place probably is not yours. You can fix it with initialization like int * i = ( int* ) malloc( sizeof(int) )

Gaim
C does not have the `new` operator.
Daniel A. White
You are right, my fault. There is function malloc. I'll fix it.
Gaim
The cast from `void*` to `int*` is unnecessary in C and bad practice. `int *i = malloc(sizeof(int));` is perfectly sufficient.
Chinmay Kanchi
+3  A: 

Rough odds on a 32-bit operating system:

  • Odds to trigger a hardware exception: 99.9995%
  • Odds to corrupt program state: 0.0005%
  • Odds to accidentally hit the right memory location: 1 in 2 billion

Estimates only to demonstrate orders of magnitude. Relevant only for a Release build. If it does corrupt program state then the odds it will do so consistently climb to close to 100%. A good compiler makes the hardware exception odds 100% in the Debug build. Get a good compiler.

Hans Passant
Yes, i will be initialized to a random value. But who said anything about a linear distribution?
Martinho Fernandes
I think you mean a "uniform" distribution. But if that code was executed just after calling a function that left a pointer in that stack location, then odds are actually very good that it points to valid memory.
Adam Rosenfield
@Adam: yes, uniform (what was I thinking?)
Martinho Fernandes
Post tweaked. Thanks.
Hans Passant
+2  A: 

Yes, the code you posted can cause a segmentation fault. You've got a pointer which is uninitialized (doesn't point to any known location) and you then use it to store something. Where does the 'something' go? Good question, to which there is no consistent answer.

You need to initialize that pointer. For example:

int target = 0;
int *i = ⌖ 

printf("target=%d\n", target);
*i=123; 
printf("target=%d\n", target);

I hope this helps.

Bob Jarvis
+4  A: 

Yes    

DigitalRoss
+1. Good answer. :-)
JesperE