About pointers

Hey there. I have the following code:

1
2
int *i;
*i=5;


This one works perfectly fine on Code::Blocks. My question is: why it's not necessary to alloc space for the first pointer ?

It's not working fine. You're corrupting the heap. You're writing to some arbitrary memory location that you shouldn't be.

The fact that it appears to work is exactly why heap corruption is so dangerous. Sometimes it's no problem, until 3 months later when you make some other unrelated change. Then when you try to run your program it explodes and the bug is incredibly hard to track down and fix.
This is particular issue for Code::Blocks or it's a general fact ?
Code::Blocks has nothing to do with it.

Heap corruption by it's very nature is just unpredictable.
closed account (S6k9GNh0)
It's a general fact (as in, pertains to a compiler that follows C++ standard. Code::Blocks is not a compiler but one can assume you are using MinGW with Code::Blocks). Please be sure and read what Disch said thoroughly. Here's a few corrected versions of your code for example:

1
2
3
4
int i;
int *p = &i;
*p = 5;
//Be careful though, once the current scope for i is up, p points to an undetermined location. 


1
2
3
int *i = new int;
*i = 5;
delete i;
Last edited on
Thanks for your quick reply. I'm just getting started to learn more about pointers.

You think I should read something too about stack and heap ( to understand how pointers works ) ?
closed account (S6k9GNh0)
Really, a common C++ developer doesn't need to know how new, delete, and stack work as long as they know they syntax to them and what they're used for. However, I find the more I know about how something works, the better I can understand my own code.
Topic archived. No new replies allowed.