D
D
Daniil Demidko2016-02-27 07:41:10
C++ / C#
Daniil Demidko, 2016-02-27 07:41:10

Pointers to memory on the stack?

I know about stack and heap. I know about pointers.
For example char *pArr = new char [5] , this is clear to me, we allocate memory on the heap.
But what is it:
char *cStr = "string";
Where is the memory allocated - on the heap or on the stack?
Why doesn't delete work here?
Why, when outputting, for example,
std :: cout << cStr ;
"string" is output, that is, the value, and not the address of the value, as usual with pointers?
Why when comparing, for example like this:

char *a = "str" ;
char *b = "str" ;
std :: cout << ( a == b ) ; /// true , но как?

Compare value ? These are pointers, right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Armenian Radio, 2016-02-27
@Daniro_San

So. In the assignment, we have "string" involved - this is a constant. It is sent by the compiler to the program's data area - it depends on the platform where exactly. So the cStr pointer itself is on the stack, which points to somewhere where the compiler put your letters.
On the second question (what the hell did you put two questions into one?) - the compiler is not a fool, he took two identical sets of "str" ​​characters, realized that they were the same, and did not produce duplicates - one pointer gets into a and b, because the compiler did not save two identical lines in the program.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question