Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory usage with pointers

Please look at the below image. After changing the value of p1, now it points to the B2 memory location. What happened to the shaded memory segment? As i know It will remain until code block finished its execution. Can those garbaged memory segments be reused again for program execution?

char *p1 = "String";
char *p2 = "Another";
p1 = p2;

Question title may be misleading. I was unable to find a qood title for this question. enter image description here

like image 676
Deadlock Avatar asked Oct 08 '15 10:10

Deadlock


People also ask

How much memory do pointers use?

Pointers take up the space needed to hold an address, which is 4 bytes on a 32-bit machine and 8 bytes on a 64-bit machine.

Do pointers use more memory?

Passing a pointer to object will only increase memory consumption by the size of the pointer.

Do pointers take up less memory?

A pointer is stored in as many bytes as required to hold an address on the computer. This often makes pointers much smaller than the things they point to. We take advantage of this small size when storing data and when passing parameters to functions.

How can we access memory using pointers?

By assigning a structure to a pointer (for example *MyPointer. Point) it allows to access any memory address in a structured way (with the operator '\'). Pointers allow to move, to read and to write easily in memory.


1 Answers

What happened to the shaded memory segment? As i know It will remain until code block finished its execution.

As per §2.13.5/8 a string literal has static storage duration:

Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration (3.7).

which means that, as per §3.7.1/1, they last for the duration of the program:

All variables which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration. The storage for these entities shall last for the duration of the program (3.6.2, 3.6.3).

As a side note, you should really assign string literals to char const* or char const[], not to char*. The reason why it mostly work in compilers is for backward compatibility reasons and in C++11 it has been deprecated.


Can those garbaged memory segments be reused again for program execution?

According to §2.13.5/16 it's unspecified whether they are reused or not:

Evaluating a string-literal results in a string literal object with static storage duration, initialized from the given characters as specified above. Whether all string literals are distinct (that is, are stored in nonoverlapping objects) and whether successive evaluations of a string-literal yield the same or a different object is unspecified. [ Note: The effect of attempting to modify a string literal is undefined. — end note ]

like image 51
Shoe Avatar answered Oct 24 '22 05:10

Shoe