const variable syntax

Pages: 123
He means contiguous memory.

ok, i got it https://stackoverflow.com/a/11753024

https://www.cplusplus.com/forum/beginner/279350/#msg1207252

when applied to pointers doesn't imply const!
Instead it means that pointer is constant expression

so, it's just like marking?
yea in brief...
every time you allocate and release dynamic memory, it costs.
every time you have a page fault, it costs.
the compiler may be smart enough at times to reduce it, but if you take it as coded, every dimension costs.

examples?
int foo[10][20]; //this is a solid block of memory that is 200 * sizeof int bytes. It costs one push on the program stack.

int ** foo;
foo = new int*[10]; //costs to allocate
for(int i = 0; i < 10; i++)
foo[i] = new int[20]; //costs 10 times to allocate.
each row of foo (the first index) can be in a different page of memory (not likely, but it can happen if the computer is very active).
releasing it with a loop + outer release costs 11 more times.

if you need to work with pointers (sizes are unknown, total size too big for stack, or want some functionality like splitting off a row) it can all be done very efficiently, but it is a fair bit of extra work to dodge the aggravations above (eg you can allocate 200 ints one time and manually carve up the dimensions in the loop).

for solid block of memory:
foo[2][3][4][5] for a solid block is just a multiplication statement and can be done at compile time.
for scattered memory, the same thing requires getting each address in turn and jumping to an offset that leads to the next one like the kids game where a gift box has a clue to the next box with a clue to... next next... and finally the real present is hidden somewhere at the end of the chain...

these are basic concepts that illustrate some of the problems. Again, you can force code around them and make the problems mostly go away, but that is piling up a lot of code and pointer heavy code is difficult to debug and modify and all correctly. The takeaway, then, is that going for deep dimensions is a high risk activity (as is just working with pointers at all; a simple textbook linked list suffers the page fault problem in spades)

Last edited on
so, it's just like marking?

What does this mean?
What does this mean?

like extern keyword; it doesn't do anything, just to mention the purpose of the following code after the keyword...
it doesn't do anything, just to mention the purpose of the following code after the keyword...

Isn't that true for every keyword, for every piece of syntax?
constexpr implies top-level const.

For example (again)
constexpr int* px = &x;
The type of p is int* const, constant pointer to int.
The object p cannot be modified to to point to another integer.
The pointed-to type is int. This type is not a constant type. Therefore x can be modified through *px.

I'm not sure where your confusion is, which makes it hard to give a decent explanation.
You could be confused about pointers specifically, about C++'s object model, or about the type system.
Last edited on
Can constexpr int* px = &x; ever be legal? Is taking the address of an object a compile-time operation?
Last edited on
Sure, see [expr.const]/11
https://eel.is/c++draft/expr.const#11

Complete example:
1
2
int x; 
int main() { constexpr int* px = &x; }


The key here is that x has static storage duration. Which should make some intuitive sense - because the storage x occupies is known by the linker.
Last edited on
Thanks, I didn't think about static storage.
Registered users can post here. Sign in or register to post.
Pages: 123