Skip to main content
GameDev.net gamedev.net
🔒 Locked

? about arrays

Started by joey201 Jun 25, 2012 at 8:21 PM 8 replies 2.2k views
Original Post
joey201
joey201
Hello I'm following this tutorial about arrays and I understand how they are declared and the several ways to initialize them, what confuses me is when I can't do
iaArray1 = iaArray2;
but instead
int iaArray1[3] = {1,2,3};
int iaArray2[3];
int iIndex = 0;
while (iIndex < 0) {
iaArray2[iIndex] = iaArray1[iIndex];
}

could I have done this?:
int iaArray1[3] = {1,2,3};
int iaArray2[3];
int iIndex = iaArray1;
int iaArray2 = iIndex;
}

What I understand is the first one is not possible the second one iaArray1 equals iaArray2 as long as iIndex is less than 0. In the third one (the one I made up) its saying iIndex equals iaArray1 and iaArray2 equals iIndex.
Anyway if anyone could develop a bit more I will greatly appreciate it.
Thanks in advance.
BCullis
BCullis
Just for general information background: an array is a contiguous block of memory that, through its type declaration, knows how to partition the memory it spans when a read-request or write-request is made. The variable name you give an array (like "iaArray1") refers to the head of the array (a pointer to the start of the contiguous memory block), and the index operator ( the [#] part) says "start at the head, and jump # appropriately-sized** chunks of memory before you read or write the value of that memory".

More specifically: your while loop doesn't do anything. You set iIndex to 0, and your while condition is "iIndex < 0", so it will never run. What you probably need to look at is for-loops. I'm assuming you want to copy the contents of iaArray1 into iaArray2? That would take something like this:

int iaArray1[3] = {1,2,3};
int iaArray2[3];
for(int iIndex = 0; iIndex < 3; i++)
{
iaArray2[iIndex] = iaArray1[iIndex];
}


**Technically, this is " # * sizeof(type)" where in your example type is "int"
Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)
frob
frob
Assuming C++...

You need to keep in mind how arrays work. Arrays are a continuous chunk of memory, and the variable name is basically a pointer to the beginning of that memory.

For your first line: iaArray[3] = {1, 2, 3}, the values are stored as data in the executable. They are assumed to be constant (although only some compilers will warn about it). The variable iaArray effectively addresses that chunk of memory.

The second line iaArray2[3] allocates a separate chunk of memory. That chunk of memory automatically exists when you hit the line. When you attempt to do iaArray2=iaArray1, think about what you are doing. You have a chunk of memory, and you want to ... what exactly? Do you want to copy the memory as raw data? Do you want to point to that memory? Do you want to destroy whatever you have in your memory and make copies of the objects held in the other block of memory? None of that really makes sense under the hood. You already have an array of objects that are already created.

If you want to point to the chunk of memory, use an integer pointer.
If you want to copy raw data, use memcopy.
If you want to make duplicates of the data, try std::copy()
SabreMan
SabreMan

Hello I'm following this tutorial about arrays and I understand how they are declared and the several ways to initialize them, what confuses me is when I can't do
...

Arrays in C++ are a degenerate data structure which you should avoid, at least if working code is a priority. You might consider finding a tutorial which teaches a more modern approach to C++ (Accelerated C++ for instance).
SiCrane
SiCrane
Incidentally, the formal way to say this is that arrays are not first class types in C++ (or C for that matter).
Matt-D
Matt-D
You should consider using containers:
- for fixed-size arrays, use std::array: http://en.cppreferen...container/array
- for dynamic (resizable) arrays, use std::vector: http://en.cppreferen...ontainer/vector

For example: http://ideone.com/OzBtb

With an optimizing compiler there should be absolutely no performance difference between C-style fixed-size array, say "int Array[5]" and, say, "std::array Array" -- none, as in: they compile to an identical machine code.

With containers you can do some pretty cool stuff -- e.g., if you want to implement linear algebra expressions such as "int i = (A + B)[2];" that evaluates to "int i = A[2] + B[2];" in an efficient way (not touching the elements you don't need for the result), you can use Expression Templates or Boost.Proto: http://cpp-next.com/...n-optimization/ (although this is no longer strictly "for beginners", save the link for the future, some day it may come in handy).
antiHUMANDesigns
antiHUMANDesigns
You can access the elements of an array is a second way aswell.

[source lang="cpp"]int array[10]; // create the array

int *p = array; // create a pointer of the same type and assign it to the start of the array.[/source]

Now, using pointer arithmetics, you can access the elements by adding a number to the pointer, such as "p+1", which is the same thing as array[1]. "p" by itself would be the same thing as array[0]. This actually what arrays do, but I think the compiler keeps a note on how many elements the array was created with, to help you with not going out of bounds.
Also, you need to "delete" arrays that have been created with "new" by typing "delete[]".

Pointer ariothmetics is not just an alternative way of traversing an array, but is something more or less needed, so it's good to know it.

I'm a bit rusty on programming atm, so don't bash me too hard if I wrote something stupid or made some silly mistake.

EDIT: oh, and using an array normally (array[0]) dereferences automatically, but if you use "p", it's not dereferenced, so you need to write "*p" to get the actual value of the element, instead of the address to it. Just wanted to point that out, even if I felt it was obvious.
joey201
joey201
Thanks all for the replies. I'm following these online tutorials and while they have given me an insight on the language I think I may get a book to get a more professional approach and not have this type of confusions on the journey. Which ones do you reccomend I'm inclining towards Accelerated C++ SabreMan suggested.
frob
frob

Which ones do you reccomend I'm inclining towards Accelerated C++ SabreMan suggested.

That is a very good book to start with.
Matt-D
Matt-D

[quote name='joey201' timestamp='1340758320' post='4953193']
Which ones do you reccomend I'm inclining towards Accelerated C++ SabreMan suggested.

That is a very good book to start with.
[/quote]

Agreed!
OP, if you have programmed before, "Accelerated C++" is the one I would recommend as well.
See also: http://www.gamedev.net/topic/627067-litterature-recommendation-for-programminggame-development/page__p__4953369#entry4953369

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.