Simple C++ questions

Started by
8 comments, last by Kobo 12 years, 3 months ago
Hello, I've started to learn C++ and I want to make this topic to ask simple questions while reading and uderstaning this language.

The forst question is about multidimensional arrays. I learned how to create and acces multidimensional arrays but i dont understand the mathematics. What actually happens when I declare int x[20[20] ? It makes 20*20 integers by multiplying the indicesand place them in a single vector and then acces every vector by the indices ? How the multidimensional vector will look in the memory ? How an element gets accesed from the memory ?

Advertisement
Hi.

In fact arrays are pointers (note: this doesn't mean pointers are arrays), when you write somthing like int x [20][20] you allocate into stack a mono-dimensional array of 20 pointers and each pointer points to another array of 20 int.

try to have a look here:http://www.cplusplus...utorial/arrays/

and here a old uni pdf slide (note mine) that shows how to access in different ways to bi-dimensioanl array members:http://www.mediafire...t3ckbonucicox76


How and where they are allocated into stak depends from the compiler and the OS... Note that pointers store addres value (this means they need to be long at least as the address bitness of the OS)  and an int store a integer value (that is usually 32 bit long, but note that C and C++ don't fix like java or other language the bitness and the range of fundamental types, e.g. try to look at "long" type into windows and linux...)  

In fact arrays are pointers (note: this doesn't mean pointers are arrays), when you write somthing like int x [20][20] you allocate into stack a mono-dimensional array of 20 pointers and each pointer points to another array of 20 int.

No, that's not right. Arrays are not pointers. Arrays have an implicit conversion to a pointer to their first element, but that is not the same as being a pointer. A multidimensional array of int[20][20] will in memory have the structure 20x20 or 400 contiguous ints. Read your own link.
I thought arrays were supposed to be "guaranteed" (and I use the term lightly much as with everything else :D) to be continuous in memory. So creating an array of 20 pointers to point to different arrays would violate that rule.

Yo dawg, don't even trip.


[quote name='DarkRadeon' timestamp='1324824275' post='4897275']
In fact arrays are pointers (note: this doesn't mean pointers are arrays), when you write somthing like int x [20][20] you allocate into stack a mono-dimensional array of 20 pointers and each pointer points to another array of 20 int.

No, that's not right. Arrays are not pointers. Arrays have an implicit conversion to a pointer to their first element, but that is not the same as being a pointer. A multidimensional array of int[20][20] will in memory have the structure 20x20 or 400 contiguous ints. Read your own link.
[/quote]
mmm... my bad

anyway isn't unsafe thinking a maxtrix as a contiguous memory pice? I saw a linux distribution where multidimensional arrays allocation was not contiguos, I don't remember which version, I only remember it was like 2 yeas ago..

edit: double negation in English language is an affermation or a stronger negation? Don't remember that... damned Italian and damned latin... my language is full of ambiguities..


I thought arrays were supposed to be "guaranteed" (and I use the term lightly much as with everything else :D) to be continuous in memory. So creating an array of 20 pointers to point to different arrays would violate that rule.
That's right, if there's no continuous space to allocate the array there'll be a problem!
PS: in most OSs, at least...

[quote name='SiCrane' timestamp='1324826008' post='4897280']
[quote name='DarkRadeon' timestamp='1324824275' post='4897275']
In fact arrays are pointers (note: this doesn't mean pointers are arrays), when you write somthing like int x [20][20] you allocate into stack a mono-dimensional array of 20 pointers and each pointer points to another array of 20 int.

No, that's not right. Arrays are not pointers. Arrays have an implicit conversion to a pointer to their first element, but that is not the same as being a pointer. A multidimensional array of int[20][20] will in memory have the structure 20x20 or 400 contiguous ints. Read your own link.
[/quote]
mmm... my bad

anyway isn't unsafe thinking a maxtrix as a contiguous memory pice? I saw a linux distribution where multidimensional arrays allocation was not contiguos, I don't remember which version, I only remember it was like 2 yeas ago..

edit: double negation in English language is an affermation or a stronger negation? Don't remember that... damned Italian and damned latin... my language is full of ambiguities..


[/quote]

double negative in English is a confirmation :D It's like math.

Yo dawg, don't even trip.


anyway isn't unsafe thinking a maxtrix as a contiguous memory pice? I saw a linux distribution where multidimensional arrays allocation was not contiguos, I don't remember which version, I only remember it was like 2 yeas ago..

That wouldn't be legal C++. A int[20][20] is a contiguous array of 20 contiguous arrays of 20 ints. See section 8.3.4 of the standard.

[quote name='DarkRadeon' timestamp='1324828585' post='4897287']
anyway isn't unsafe thinking a maxtrix as a contiguous memory pice? I saw a linux distribution where multidimensional arrays allocation was not contiguos, I don't remember which version, I only remember it was like 2 yeas ago..

That wouldn't be legal C++. A int[20][20] is a contiguous array of 20 contiguous arrays of 20 ints. See section 8.3.4 of the standard.
[/quote]
ok, thank you for clarification



double negative in English is a confirmation :D It's like math.


thank you too

So in English is duplex negatio affirmat...in Italian there is no strict rules (but in most cases a double negation is a stronger negation), most of the meaning is determined from the situation and the tone of the speaker and same for the answer.. all this always confuses me, especially when I write in another language .-.





[quote name='boogyman19946' timestamp='1324827893' post='4897286']
I thought arrays were supposed to be "guaranteed" (and I use the term lightly much as with everything else biggrin.gif) to be continuous in memory. So creating an array of 20 pointers to point to different arrays would violate that rule.
That's right, if there's no continuous space to allocate the array there'll be a problem!
PS: in most OSs, at least...
[/quote]

Arrays are contiguous within the context of the address space of the process. It is the operating system's problem outside of that context. This means that you can use pointer arithmetic to access members of an array.

This topic is closed to new replies.

Advertisement