Похожие чаты

So I tried this: int arr[3];

arr[0] = 1;
arr[2] = 2;

std::cout << *(arr + 1) << std::endl;

This is the output i got:
677446592

Since data in array are contiguous, why didn't I get 2?

9 ответов

12 просмотров

your data is contiguos but the [1] element has no value set

Because you didn't set second element of an array

🖖- Автор вопроса
🖖
oh so space for it reserved?

its like if you try to do a print for int a; without setting the data

🖖- Автор вопроса
Kirill 🚜
Yes

...so what am I shifting when I do "arr[1] =2", that makes the insertion time O(n)?

🖖
...so what am I shifting when I do "arr[1] =2", th...

you are not inserting anything, you are setting the value of an element

🖖- Автор вопроса

This is undefined behavior, there is called stack redzone. It may contain garbage value from previous function. You are printing this garbage value.

Похожие вопросы

Карта сайта