Newbie C pointer question

Status
Not open for further replies.

arp

Seniorius Lurkius
30
Well, I'm learning C, and I was reading this tutorial on pointers, and I wrote a small program myself. This is it:
#include <stdio.h>

int *pPointer;

void SomeFunc(void){
int Number = 15;
pPointer = &Number;
}

void main(void){
SomeFunc();

printf("Value of pPointer: %d\n", *pPointer);
}
Compile goes fluently, and it WORKS?? I just don't understand, I'm sorry. Isn't this supposed to fail. (As the tutorial says, the pointer points to a local variable, which is distroyed at the moment of the printf ...
 

lensam69

Ars Scholae Palatinae
746
exactly.

The variable was destroyed, this means that you can't access Number anymore in your program. However, the memory space that it occupied isn't cleared because it's not neccesary, It just gets "labeled" as free. You can also try asigning random values to the pointer, and reading from them, You probably won't get null values.

Don't try to write to these random pointers, you could crash other programs running (including your OS).
 

sittidae

Ars Centurion
325
Subscriptor
Compile goes fluently, and it WORKS?? I just don't understand, I'm sorry. Isn't this supposed to fail. (As the tutorial says, the pointer points to a local variable, which is distroyed at the moment of the printf ...

1. They gave a bad example. The program might or might not print the value you were expecting (depending on how it treats free'd stack space).

2. You are misunderstanding a bit when the variable was destroyed. It is not at "the moment of the printf...", but at the time Number goes out of scope (when SomeFunc terminates).

It all depends on the compiler what will actuall happen. This unpredictability bites a number of C programmers. There have been a lot of times I have debugged someone's C code that looks like:

code:<HR>
Code:
int  func (params)
{
   int init;
....
   if (init == 0)
   {
      do some initialization here
   }
...
}

When they compiled and tested it (if they ever did), it probably worked correctly. They were relying on stack space to be initialized to 0. Port the code to another platform or compiler and it can be very hard to track down.
 

petrov

Ars Tribunus Militum
2,714
Originally posted by lensam69:
Don't try to write to these random pointers, you could crash other programs running (including your OS).

this is only true on OS's with poor memory protection. If you try to write to memory outside of your process in any Unix, you'll get a segmentation fault. NT probably won't crash either, and will give a general protection fault, but I wouldn't be as confident.

--sam
 

petrov

Ars Tribunus Militum
2,714
Originally posted by russ-iha:
if you tried to write to a protected memory area on nt/2k/xp you'd get a protection fault and your program would be terminated.

that's the theory but I've had quicktime blue screen my machine at work, and diablo 2 blue screens my home box, both running 2k. I've never had a kernel panic on 3-4 unix boxen that I've been running for the past 6+ years.

--sam
 

rockeye

Ars Scholae Palatinae
1,206
if you tried to write to a protected memory area on nt/2k/xp you'd get a protection fault and your program would be terminated.

Correct. Any crashing (besides the dumbass program which must be killed) that takes place may be due to unstable drivers. Drivers be necessity have access to kernel data structures. Overwriting those can lead to crashes.
 

Midlands

Ars Praetorian
535
Subscriptor
The end lesson here is that writing C (and many other languages) is very hard, because the fact that it works doesn't mean it's right. A very large number of constructs will work on one platform, but not on another, or will work on one platform for a long time until later you do something that triggers the latent bug, or so on.

In other words, you have to end up programming to the standard, not to what works on your platform -- unless, of course, it's a throwaway program and you don't care.

(In particular, note that these situations are almost always those in which behavior is 'undefined' according to the C standard. This means it could crash your program, work just fine, nuke the OS and format your hard drive, send porn spam to your boss, or miraculously create an AI capable of curing cancer for you. You just don't know. Therein lies the rub.)
 
Status
Not open for further replies.