🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C++ Workshop - Arrays & Strings (Ch. 13)

Started by
35 comments, last by alphastickmania 15 years, 1 month ago
Just wanted to say Hello and thank you guys for this workshop. I just started last week so I'm playing catch up. I in chapter 6 right now... I am moving so quickly I don't think it is all sticking but it is so much better then learning on my own. Also thanks to all of you that are asking questions, they are some of the same ones I have had.

will
Advertisement
I have a question regarding multidimensional array and its equivalent single array representation.

As mentioned in the book, we can declare SQUARE board[8][8] as SQUARE Board[64].

Apart from being more intuitive in the multidimensional array, is it true that a multidimensional array performs slower than a single dimensional array.

If so, should we use the one-dimensional representation for multidimensional array if performance is an issue?
Quote: Original post by schwa
I have a question regarding multidimensional array and its equivalent single array representation.

As mentioned in the book, we can declare SQUARE board[8][8] as SQUARE Board[64].

Apart from being more intuitive in the multidimensional array, is it true that a multidimensional array performs slower than a single dimensional array.

If so, should we use the one-dimensional representation for multidimensional array if performance is an issue?


The answer depends on the size of your cache. The compiler flattens the multi-dimensional array to a single array in memory. This means that its a long section of memory 8*8 long. If you iterate through the array [row][col] or [col][row] it will traverse the memory differently. One will jump 8 at a time while another will increment 1 at a time. If your cache can handle loading in the size of the entire array into cache either traversals will be just as quick.

It seems on my compiler the compiler will lay out the array by [row][col](y,x) so if you increment one by x or col each time then you'll only be moving forward one in memory.

Looking at my dissasembly in debug shows that to access an array in [r][c] format takes two pointer dereferences but with optimizations on its one look up.

So no I can't see how it would be any faster to use a single array yourself. Unless you write some obscure code and the compiler doesn't know how to optimize it.



AFAIK, the "ordering" of a multi-dimensional array's memory layout is well-defined, specified by the standard, such that the first dimension takes the "big" stride and the last dimension iterates sequentially through memory. As long as your nested loops are in the right order, there is no performance difference; it really is just syntactic sugar. And even if you had nested loops in the wrong order, in *some* circumstances an optimizing compiler can fix it for you. (The actual restructuring is normally straightforward; the hard part is checking if it's "safe". And yes, I took an optimizing-compilers course in university :) )
Was a quiz posted for this chapter?
Not yet. I'm behind schedule on my quizzes due to my 2 month old baby. =) I'll go back and post some retroactively, but very few were answering the quizzes anyways.
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
A two month old baby would definally take a lot of time. I been putting all the information together in a packet. It has help give me a much more complete understanding of C++.
Quote: Original post by jwalsh
Not yet. I'm behind schedule on my quizzes due to my 2 month old baby. =) I'll go back and post some retroactively, but very few were answering the quizzes anyways.


I would love to see some quiz questions for the past chapters though. I don't need tons of questions for each chapter, but just a few, to test if I understood the chapter would be nice.
Greetings All!

It's once again QUIZ TIME!!! That's right, listed below are a set of quiz questions to help you test your knowledge and understanding of the material contained in chapters 13.

In addition to the questions and exercises below, make sure that as you're reading the book you enter the examples into your compiler, build the program, and run the executable. I know this is a time consuming process, but the repeat use of keywords, syntax, and semantics will help ingrain the information into your long-term memory. My advice is to create a simple "driver" project with a function main. As you read, enter the examples into function main, test it, and then erase it for use again in the next example.

PLEASE DO NOT POST THE ANSWERS TO THESE QUESTIONS OR EXERCISES. If you are unable to answer these questions, please ask for assistance, but DO NOT POST THE ANSWERS. Any question which is not marked with [Extra Credit] can be answered by reading your textbook. Questions which are marked [Extra Credit] either have been answered in the thread previously, or can be answered by doing a bit of research.

I will create an answer thread for these questions immediately, so that people will have a chance to get the answers more quickly.

Chapter 13 Quiz

1. What is an array?
2. How do you access an element in an array?
3. Are offsets (indexes) numbered starting from 0 or from 1?
4. What happens if you write to an index that is outside the range of your array?
5. What are fence post errors?
6. How do you initialize arrays with simple data types?
7. If you initialize an array with values, are you required to specify the size? Should you?
8. When using an array of objects, which constructor gets called when the array is created?
9. Is it possible to have multi-dimensional arrays?
10. Is there a maximum number of dimensions which are supported by C++?
11. In C++, what is another way of thinking about an array name, as it pertains to pointers?
12. Is it legal to use array names as replacements for the construct in question? How about vice-versa?
13. What happens if you allocate a dynamic array of memory and don’t delete it, including the [] operator?
14. Can you set the size of an array created on the stack at run-time?
15. Can you set the size of an array created on the heap at run-time?
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
How do I pass an array of strings into a function?

example of what i tried:

#include <string>....int main(){  string myStringArray[] = {"hello", "whatsup", "howdy"};  doMyFunc(myString);}void doMyFunc(string theArray){  cout << endl << theArray[0]; // please print the first full string,                                // not the first character of tfist string  cout << endl << theArray[1]; // please print the second full string,   cout << endl << theArray[2]; // please print the third full string, }


ive tried this and other varieties, which generate a compiler error:
"conversion from `std::string*' to non-scalar type `std::string' requested"
(depending which variety i try, the `std::string*' could also be `std::string**' etc.)

This topic is closed to new replies.

Advertisement