Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointers to statically allocated objects

Tags:

c++

pointers

I'm trying to understand how pointers to statically allocated objects work and where they can go wrong.

I wrote this code:

int* pinf = NULL;
for (int i = 0; i<1;i++) {
    int inf = 4;
    pinf = &inf;
}

cout<<"inf"<< (*pinf)<<endl;

I was surprised that it worked becasue I thought that inf would dissapear when the program left the block and the pointer would point to something that no longer exists. I expected a segmentation fault when trying to access pinf. At what stage in the program would inf die?

like image 253
Meir Avatar asked Jul 13 '10 12:07

Meir


People also ask

Can pointers be statically allocated?

The statically allocated objects p1 (the pointer) and p2 (the class instance) exist as long as the program runs. It is important to distinguish the pointer p1 containing just an address from the class instance at that address. (That instance will be created at run time by new p() ).

How do pointers help in dynamic allocation?

Dynamically allocated memory must be referred to by pointers. the computer memory which can be accessed by the identifier (the name of the variable). integer, 8, stored. The other is the “value” or address of the memory location.

Do pointers allocate memory?

Pointers (along with memory allocation) allow you to create programs that can use as much memory as they need (and no more). A pointer is a reference to some other piece of data. It is not the data itself.

What is a pointer variable What is static memory allocation and dynamic memory allocation?

When the allocation of memory performs at the compile time, then it is known as static memory. When the memory allocation is done at the execution or run time, then it is called dynamic memory allocation. 2. The memory is allocated at the compile time. The memory is allocated at the runtime.


2 Answers

Your understanding is correct. inf disappears when you leave the scope of the loop, and so accessing *pinf yields undefined behavior. Undefined behavior means the compiler and/or program can do anything, which may be to crash, or in this case may be to simply chug along.

This is because inf is on the stack. Even when it is out of scope pinf still points to a useable memory location on the stack. As far as the runtime is concerned the stack address is fine, and the compiler doesn't bother to insert code to verify that you're not accessing locations beyond the end of the stack. That would be prohibitively expensive in a language designed for speed.

For this reason you must be very careful to avoid undefined behavior. C and C++ are not nice the way Java or C# are where illegal operations pretty much always generate an immediate exception and crash your program. You the programmer have to be vigilant because the compiler will miss all kinds of elementary mistakes you make.

like image 151
John Kugelman Avatar answered Sep 28 '22 07:09

John Kugelman


You use so called Dangling pointer. It will result in undefined behavior by the C++ Standard.

like image 43
Kirill V. Lyadvinsky Avatar answered Sep 28 '22 07:09

Kirill V. Lyadvinsky