Jump to content
  • Advertisement
Sign in to follow this  
godzilla_123

Simple C++ questions

This topic is 2541 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 ?

Share this post


Link to post
Share on other sites
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...)  

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

[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..

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites

[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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

[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 .-.




Share this post


Link to post
Share on other sites

[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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!