不要用指针指向局部变量
#include <stdio.h>
int main() {
int *p = NULL;
int i = 1;
switch(1)
{
case 1:
{
p = &i;
printf("Inside switch - Address of i: %p, Value of p: %p\n", (void*)&i, (void*)p);
}
break;
default:
break;
}
printf("After switch - Address pointed by p: %p, Value at p: %d\n", (void*)p, *p);
return 0;
}
I'll explain what's happening in this code and why it's problematic.
This code has a serious issue - it's attempting to access a local variable after it goes out of scope, which leads to undefined behavior. Here's the problem:
- Inside the switch-case block,
iis declared as a local variable - The pointer
pstores the address ofi - When the case block ends,
igoes out of scope and is destroyed - The program then tries to dereference
p, which now points to invalid memory
Here's the corrected version:
#include <stdio.h>
int main() {
int *p = NULL;
int i = 1; // Moved the variable declaration outside the switch
switch(1)
{
case 1:
{
p = &i;
printf("Inside switch - Address of i: %p, Value of p: %p\n", (void*)&i, (void*)p);
}
break;
default:
break;
}
printf("After switch - Address pointed by p: %p, Value at p: %d\n", (void*)p, *p);
return 0;
}
Key changes made:
- Moved the variable
ioutside the switch block so it remains valid throughout the program's execution - Added more descriptive printf messages
- Added proper type casting to void* when printing pointers to avoid compiler warnings
Now the code is safe because:
- The variable
iexists for the entire duration of the main function - The pointer
ppoints to a valid memory location even after the switch block - We can safely dereference
pin the final printf statement
This version will produce consistent, defined behavior instead of potentially crashing or producing unexpected results.
It happended to work? and why
Let me explain why the original code appeared to "work" despite being technically incorrect:
- What you observed:
0061FF18 0061FF18 // First line: address of i and value of p inside the switch
0061FF18 1 // Second line: address in p and value at p after the switch
- Why it "worked":
- When you saw it working, this was actually just lucky behavior. It's undefined behavior in C.
- The memory location (stack frame) where
iwas previously stored hadn't been overwritten yet by another function call - The compiler didn't immediately reuse that stack space
- The value
1was still sitting in that memory location
- Why it's still wrong:
- According to C language rules, accessing a local variable after it goes out of scope is undefined behavior
- The same code might:
- Crash on a different compiler
- Give different results with optimization flags
- Work today but fail tomorrow
- Fail on a different operating system
- Work in debug mode but fail in release mode
- Real-world analogy:
Imagine you're renting a hotel room:
- When you check out, you no longer have valid access to that room
- Maybe your old key still works because they haven't reset it yet
- Maybe your belongings are still there because they haven't cleaned yet
- But you can't rely on any of this - it's not guaranteed behavior
- Best Practice:
- Never rely on undefined behavior, even if it appears to work
- Always ensure variables are in scope when you access them
- The corrected version with
ideclared outside the switch block is the proper way to write this code
Remember: Just because code appears to work doesn't mean it's correct. Code that invokes undefined behavior might work today but could fail in unexpected ways in different circumstances.

浙公网安备 33010602011771号