
-----------------------------------
Cancer Sol
Sun Mar 24, 2013 5:46 pm

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:

char square
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:

#include 
using namespace std;

char square
That graph actually looks pretty good, I might use it for mine too :P

-----------------------------------
Raknarg
Sun Mar 24, 2013 5:54 pm

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

-----------------------------------
Zren
Sun Mar 24, 2013 6:09 pm

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.


I tried compiling the code without it actually, and it was messed up.


What was the error?

Edit: Damn it I'm slow.

-----------------------------------
Cancer Sol
Sun Mar 24, 2013 7:42 pm

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 :P


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?

-----------------------------------
Insectoid
Sun Mar 24, 2013 7:53 pm

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.

-----------------------------------
Zren
Sun Mar 24, 2013 7:57 pm

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

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.

http://www.codeproject.com/KB/cs/Arrays-dontumindit/array5.gif

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. ;_;

-----------------------------------
Raknarg
Sun Mar 24, 2013 8:02 pm

RE:Just wondering about an array
-----------------------------------
I'm guessing it only makes a difference if you use a lot of arrays then

-----------------------------------
DemonWasp
Mon Mar 25, 2013 1:20 am

RE:Just wondering about an array
-----------------------------------
Given as the language in question is C++, you can also abuse pointers:

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.

-----------------------------------
Cancer Sol
Mon Mar 25, 2013 7:19 pm

Re: RE:Just wondering about an array
-----------------------------------
Given as the language in question is C++, you can also abuse pointers:

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 :P
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 :P (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.

-----------------------------------
Insectoid
Mon Mar 25, 2013 8:20 pm

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.

-----------------------------------
Cancer Sol
Tue Mar 26, 2013 7:44 am

Re: 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.

[code]
char square[9] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
[/code]
How much bytes of RAM would that take up compared to 
[code]
char square[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','10'};
[/code]
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 :/

-----------------------------------
DemonWasp
Tue Mar 26, 2013 12:59 pm

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 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 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.

-----------------------------------
Cancer Sol
Tue Mar 26, 2013 4:11 pm

Re: 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 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 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 :P
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 :/
