Computer Science Canada Just wondering about an array |
Author: | Cancer Sol [ Sun Mar 24, 2013 5:46 pm ] | ||||
Post subject: | Just wondering about an array | ||||
Okay so... I was just looking at a tic tac toe game online at cppforschool.com, the array at the start that's like this:
I'm just wondering. Why is there another 'o' there? I tried compiling the code without it actually, and it was messed up. Why is it like that though? The full code is like this:
That graph actually looks pretty good, I might use it for mine too |
Author: | Raknarg [ Sun Mar 24, 2013 5:54 pm ] |
Post subject: | RE:Just wondering about an array |
I'm assuming it's because he'd rather work with array values 1 to 9 rather than 0 to 8 |
Author: | Zren [ Sun Mar 24, 2013 6:09 pm ] |
Post subject: | RE:Just wondering about an array |
Looks like it's just used for padding the array to start from an index of 1 so they can easily get user input in a range more comfortable for a user. Which is a horrible reason for this practice as you can just do cin >> input; choice = input - 1; and shift all the indexes down by one. Depending on this hack will also prevent you from moving to multi-dimensional arrays. Quote: I tried compiling the code without it actually, and it was messed up. What was the error? Edit: Damn it I'm slow. |
Author: | Cancer Sol [ Sun Mar 24, 2013 7:42 pm ] |
Post subject: | Re: Just wondering about an array |
I thought that's only if the arrays were integers, but I guess I just learnt something new xD Thanks guys. @Zren It wasn't an error, it was just that the board's numbers were all over the place Quote: Depending on this hack will also prevent you from moving to multi-dimensional arrays. What do you mean by that? Doing what the sample tic tac toe code showed won't let me use multi-dimensional arrays properly? Edit: Wouldn't that just add an extra variable that's not needed? Or is it supposed to do something like make the code easier to read? |
Author: | Insectoid [ Sun Mar 24, 2013 7:53 pm ] |
Post subject: | RE:Just wondering about an array |
It would add an entire array that isn't needed. Not really a world-ending issue but it's really inefficient. |
Author: | Zren [ Sun Mar 24, 2013 7:57 pm ] |
Post subject: | Re: Just wondering about an array |
Cancer Sol @ Sun Mar 24, 2013 7:42 pm wrote: I thought that's only if the arrays were integers, but I guess I just learnt something new xD
Quote: Depending on this hack will also prevent you from moving to multi-dimensional arrays. What do you mean by that? Doing what the sample tic tac toe code showed won't let me use multi-dimensional arrays properly? Erm. Now that I think about it, you can still use it. The padding would just use up a lot more memory (than just one extra unit). In a single-dimensional array. You waste just the first index. In a two dimensional (2d) array, you waste the entire first row and column. In the above example 10x10 array, you're wasting the 19 (x 4 Bytes if using a typical integer data type) units of memory at both the column at index 0 and the row at index 0. That's almost a 1/5th of extra memory usage for this hack. Edit, not again. ;_; |
Author: | Raknarg [ Sun Mar 24, 2013 8:02 pm ] |
Post subject: | RE:Just wondering about an array |
I'm guessing it only makes a difference if you use a lot of arrays then |
Author: | DemonWasp [ Mon Mar 25, 2013 1:20 am ] | ||
Post subject: | RE:Just wondering about an array | ||
Given as the language in question is C++, you can also abuse pointers:
That said, this is some pretty dodgy-looking C++ code (both my snippet and the OP snippet). I would not recommend that example as something to learn from. Edit: correcting my own code snippet, I hope. I haven't actually tried compiling it because Windows. |
Author: | Cancer Sol [ Mon Mar 25, 2013 7:19 pm ] | ||
Post subject: | Re: RE:Just wondering about an array | ||
DemonWasp @ 3/25/2013, 1:20 am wrote: Given as the language in question is C++, you can also abuse pointers:
That said, this is some pretty dodgy-looking C++ code (both my snippet and the OP snippet). I would not recommend that example as something to learn from. Edit: correcting my own code snippet, I hope. I haven't actually tried compiling it because Windows. I didn't learn pointers yet I just learnt how to do structures, but I still have to practice that. It's a lot better than the one I made before though (Mine was really confusing, even to me) @Zren what do you mean by "hack" though? And btw, is it when I use more arrays, or do it wrong with multi-dimensional arrays, it will affect the amount of memory the computer will require to use? Sorry about my grammar and everything though, lol. |
Author: | Insectoid [ Mon Mar 25, 2013 8:20 pm ] |
Post subject: | RE:Just wondering about an array |
Pointers are a big deal in C++. Anyway, a lot of the time, 'hacking' refers to things that work, but that you really shouldn't do because of potential side-effects and...well, it's like a band-aid for your code. As for arrays, well, using lots of arrays takes up ram equal to the sum of the sizes of the arrays. A multi-dimensional array takes up the product of the size of each dimension. |
Author: | Cancer Sol [ Tue Mar 26, 2013 7:44 am ] | ||||
Post subject: | Re: RE:Just wondering about an array | ||||
Insectoid @ 3/25/2013, 8:20 pm wrote: Pointers are a big deal in C++.
Anyway, a lot of the time, 'hacking' refers to things that work, but that you really shouldn't do because of potential side-effects and...well, it's like a band-aid for your code. As for arrays, well, using lots of arrays takes up ram equal to the sum of the sizes of the arrays. A multi-dimensional array takes up the product of the size of each dimension.
How much bytes of RAM would that take up compared to
Let's just say the cpu is the same speed for testing both of those Not sure if the cpu speed will affect how much RAM it will use though, I'm not that good with computers :/ |
Author: | DemonWasp [ Tue Mar 26, 2013 12:59 pm ] |
Post subject: | RE:Just wondering about an array |
Well first, '10' is not a character, and neither of those declarations is valid: you have 10 entries given in an array of size [9] and 11 entries given for an array of size [10]. Bluntly, an array of nine characters will take 9 bytes, and an array of ten characters will take 10 bytes. Realistically, neither of those matters at all because even tiny embedded computers have megabytes to gigabytes of RAM now. CPU speed does not affect RAM size. CPU speed and RAM speed are "linked" in some architectures because of how the memory access bus is implemented (this includes AMD and Intel CPUs, among many others). The question of how much RAM is actually consumed turns out to be pretty complicated, too. I'm not sure about the exact C++ semantics, but I believe that allocations of the form type name[size] = {...} at the global scope are allocated on the stack, which has different implications for memory consumption than allocating on the heap (which are way too complicated to get into because they depend on operating system, CPU type, etc). In short, an allocation of 10 bytes on the stack may consume 10 bytes (array itself) plus 0 or 4 or 8 bytes for the pointer (depending on whether that value can be 'baked' into the instructions issued and pointer size). On the heap, it might consume 10 bytes (array) plus 0, 4, or 8 (per pointer you use to refer to it) plus a small "header" used by malloc/free or new/delete for record-keeping, plus it may "consume" some memory by forcing subsequent allocations to use a different memory block, etc. The interactions are very complicated. |
Author: | Cancer Sol [ Tue Mar 26, 2013 4:11 pm ] |
Post subject: | Re: RE:Just wondering about an array |
DemonWasp @ 3/26/2013, 12:59 pm wrote: Well first, '10' is not a character, and neither of those declarations is valid: you have 10 entries given in an array of size [9] and 11 entries given for an array of size [10].
Bluntly, an array of nine characters will take 9 bytes, and an array of ten characters will take 10 bytes. Realistically, neither of those matters at all because even tiny embedded computers have megabytes to gigabytes of RAM now. CPU speed does not affect RAM size. CPU speed and RAM speed are "linked" in some architectures because of how the memory access bus is implemented (this includes AMD and Intel CPUs, among many others). The question of how much RAM is actually consumed turns out to be pretty complicated, too. I'm not sure about the exact C++ semantics, but I believe that allocations of the form type name[size] = {...} at the global scope are allocated on the stack, which has different implications for memory consumption than allocating on the heap (which are way too complicated to get into because they depend on operating system, CPU type, etc). In short, an allocation of 10 bytes on the stack may consume 10 bytes (array itself) plus 0 or 4 or 8 bytes for the pointer (depending on whether that value can be 'baked' into the instructions issued and pointer size). On the heap, it might consume 10 bytes (array) plus 0, 4, or 8 (per pointer you use to refer to it) plus a small "header" used by malloc/free or new/delete for record-keeping, plus it may "consume" some memory by forcing subsequent allocations to use a different memory block, etc. The interactions are very complicated. Oh whoops, I forgot I would have to put it as a string if I wanted to use 10 right? I still don't really get what the example needs to be changed though :/ |