Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Arrays in DarkGDK (C++)

Author
Message
James McGlocken
16
Years of Service
User Offline
Joined: 29th Jan 2008
Location: In The Beautiful Mountains of Vermont
Posted: 10th Apr 2008 04:40
To declare an array I thought I would do something like this:

int MyArray[2][2];

I THINK that this will work, but then to use variables to replace those numbers would that work?

MyArray[MyVar][YourVar];

Does that seem right? Is there anything I should keep in mind when using Arrays in C++ other than this?

Basic is just so much easier, but I really want to get this down. ;/ I wish there was some better documentation on the basics of C++, as the documentation for Dark Basic and Dark Basic Pro was just wonderful...

PHP Programming, Music Composition
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 10th Apr 2008 06:13
Essentially you're right. You can do...

MyArray [1][1] = 32;

just remember that the arrays are indexed starting at 0.

To set the values without having to individually access them you can do

int MyArray [2][2] = {{32, 48},{64, -1}}; // hike

You can also do

int MyArray [2][2] = {32, 48, 64, -1);

which would work the same but isn't as neat and may be prone to errors in larger arrays.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
James McGlocken
16
Years of Service
User Offline
Joined: 29th Jan 2008
Location: In The Beautiful Mountains of Vermont
Posted: 10th Apr 2008 08:00
One of the things that I have to have working but couldn't get working yet is the following code:



Last time I tried this it didn't work, does this code look right?

PHP Programming, Music Composition
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 10th Apr 2008 08:05
You can't use 5 as an index with the dimensions you've given. You're telling the compiler that you want a two dimensional array with five elements in each direction. Your first index is 0, not 1, so in order to index five elements you can only use the numbers 0, 1, 2, 3 and 4.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Codger
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 12th Apr 2008 21:42
Thank you Lilith I had forgotten that in c or c++ the arrays are dimensioned from 0 to n-1 rather than 0 to n in basic. Maybe now i can get my little project working properly

System
PIV 2.8 MZ 512 Mem
FX 5600 256 mem
James McGlocken
16
Years of Service
User Offline
Joined: 29th Jan 2008
Location: In The Beautiful Mountains of Vermont
Posted: 14th Apr 2008 20:02
I still can't get arrays to create themselves using variables istead of [5][5] (for example).

Here is the code for the very beginning of my program. I'm trying to translate it from Dark Basic Pro into DarkGDK:



PHP Programming, Music Composition
James McGlocken
16
Years of Service
User Offline
Joined: 29th Jan 2008
Location: In The Beautiful Mountains of Vermont
Posted: 14th Apr 2008 20:05
The following code worked fine in Dark Basic Pro:

DIM AMc(create_totalmaxObj,12)


yet in DarkGDK

int AMc[create_maxobj][12];

I can't get it to work.

PHP Programming, Music Composition
Pixel Perfect
17
Years of Service
User Offline
Joined: 21st Feb 2007
Location: UK
Posted: 14th Apr 2008 20:30 Edited at: 14th Apr 2008 23:57
Support for dynamic arrays in C++ is extremely limited. As a result most people do the following:

Use vectors from the STL (Standard Template Library)

Use specifically designed Dynamic Array classes. Below is an example posted by IanM a while back based on using vectors, although I have never used it:




Use pointers to simulate dynamic arrays. Below is a code example I have used successfully:



Note: the individual elements like most variables in C++ are not pre initialised. If this is important to you then you will need to initialise each one first in a loop.

Hope this helps

[EDIT] Changed STD to STL

No matter how good your code is, someone will improve on it
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 14th Apr 2008 20:38 Edited at: 14th Apr 2008 20:40
Quote: "
I still can't get arrays to create themselves using variables istead of [5][5] (for example)."


Using variables to create arrays is a totally different beast. That's when you have to use the new keyword to allocate space. But even there there are some limitations. Unfortunately I'm a bit rusty on this. Essentially you need to have at least the most significant array dimension as a known constant. Assuming we're working with integers you'd do something like this.

int *mydoublearray;
int size = 10;
mydoublearray = new int [size][5];

which would assign mydoublearray to point to and behave as if it were an array

int [10][5];

ten rows of five integers across.

If you need something more variable than that you just allocate the number of ints into a single array and have a function that calculates the position within the one dimensional array based on a passed row and column.

Keep in mind you'd need to delete the allocated space when you're finished with it.

delete mydoublearray [];

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 14th Apr 2008 20:42 Edited at: 14th Apr 2008 20:43
Quote: "The following code worked fine in Dark Basic Pro:

DIM AMc(create_totalmaxObj,12)


yet in DarkGDK

int AMc[create_maxobj][12];

I can't get it to work."



int *AMc = new int [create_maxobj][12];
if (AMc == NULL) {
// not enough memory available to allocate space
}


When you're finished with the array you need to do

delete AMc [];

Be sure you do this while the AMc pointer is still in scope.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
James McGlocken
16
Years of Service
User Offline
Joined: 29th Jan 2008
Location: In The Beautiful Mountains of Vermont
Posted: 15th Apr 2008 22:30


This exact code above does not compile in Visual C++ 2008 Express. It comes up with

Quote: "error C2440: 'initializing' : cannot convert from 'int (*)[12]' to 'int *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast"


PHP Programming, Music Composition
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 15th Apr 2008 22:42
Try it this way:

int (*AMc)[12] = new int [create_maxobj][12];

I made an oversight.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
James McGlocken
16
Years of Service
User Offline
Joined: 29th Jan 2008
Location: In The Beautiful Mountains of Vermont
Posted: 17th Apr 2008 22:45


Unfortunately the code is not working yet. ( again thanks for the help so far )

Above is just the full programs code.

PHP Programming, Music Composition
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 17th Apr 2008 23:06
It compiled fine for me though, of course, the program doesn't do anything other than setup. Are you receiving an error at compile time? If so, what is it?

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 21st Apr 2008 19:19
Quote: "int MyArray [2][2] = {{32, 48},{64, -1}}; // hike
"
LOL

bobba
16
Years of Service
User Offline
Joined: 10th Apr 2008
Location:
Posted: 28th Apr 2008 16:22
easy m8 hear is the code u need:


int **MyArray;
int IndexValue = 2;
int IndexValue2 = 6;


MyArray= new int* [IndexValue ];
for(int i=0;i<IndexValue;i++)
{
*(MyArray+i)=new int[IndexValue2 ];
}

MyArray[1][5] = 15;

Some advansed stuff about arrays.
it is posible to increase them in size as required, as well as make jaged arrays to not have wasted space. the:
*(MyArray+i)=new int[IndexValue2 ];
line can be declered just before it is used and can be differing sizes. Single elements can be deleted without deleting the whole array, and if you want to expanded it then only the base must be deleted, a new base of greater size is declered then pointed to the old elements.
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 28th Apr 2008 17:58
Bobba, I think I might like your method. Is that better than dynamically allocating a CHUNK of elements... say 100, then once used, making a new bigger CHUNK... say 200, and copying the first 100 items to new BIGGER chunk? I don't know. Here is the reasoning behind me using the chunked method:

The reasoning behind this approach is that by using bigger chunks of memory at a time, when you release that RAM, you are releasing a "Contigous Chunk" versus individual elements which can fragment your ram quite a bit over time. The benefit of this is an operating system thing, in that you heighten the chances that in subsequent memory allocations of the same size (or smaller) those "released" contiguous chunks will be available and more likelt to be reused versus allocating a NEW area in your ram because there wasn't' any free contiguous blocks available.

Memory manager Programs often "Speed up software" by merely doing the equivalent of a disk defragmenter in RAM and they work pretty good. Especially on a machine that's been on and busy a few days because the ram gets all fragmented over time.

The chunked method, makes it so you are always allocated a predetermined "chunk size" in your program EVERY TIME is allocates an array, amking (over the life of your program...in theory) plenty of "same sized" blocks for the OS to choose from when your program repetively allocates and deallocates "Like-sized" blocks of ram.

I understand that this might not be the place for this deep a discussion an, and that your code sample and description is probably perfect, simple, and plausible for the poster, however you demonstrated some definate KNOW HOW and I wanted to run this by you and throw it out there and hopefully hear your thoughts.

Login to post a reply

Server time is: 2024-09-29 19:22:14
Your offset time is: 2024-09-29 19:22:14