Gamestudio Links
Zorro Links
Newest Posts
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (degenerate_762, AbrahamR, AndrewAMD, ozgur), 667 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
append to array #112879
02/20/07 17:34
02/20/07 17:34
Joined: Jul 2006
Posts: 783
London, UK
sheefo Offline OP
User
sheefo  Offline OP
User

Joined: Jul 2006
Posts: 783
London, UK
This is a general programming question; I think it is much suited here.

Is there a way to append an object/pointer to an array, without keeping a counter? Like in the Ruby Programming Language you can use "<<" to append an object to the first unoccupied space in an array.

Re: append to array [Re: sheefo] #112880
02/20/07 17:55
02/20/07 17:55
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
There are lots of standard solutions for that problem.

For an array, you could use either a counter, or a function that checks for the next empty space in the array. This is however a bad solution.

However the usual way is not an array, but a linked list. While an array has a fixed size, a linked list can have any size and you can easily append or remove nodes without the need to look for an empty space. Predefined objects like PANELs, BMAPs etc. are stored in linked lists.

Re: append to array [Re: jcl] #112881
02/20/07 18:27
02/20/07 18:27
Joined: Jul 2006
Posts: 783
London, UK
sheefo Offline OP
User
sheefo  Offline OP
User

Joined: Jul 2006
Posts: 783
London, UK
FixxeR helped me with Linked List, so now I use them for handling my custom structs for Graphics and stuff.

It is unpracticle using Linked Lists instead of an array. Never mind, I will use a counter instead It not that important anyway

BTW, I had to define my own Graphic Linked List for PANELs because I noticed there was no 'ent_next' for panels. Perhaps you can implement it...?

Re: append to array [Re: sheefo] #112882
02/20/07 19:38
02/20/07 19:38
Joined: Nov 2003
Posts: 108
Oklahoma, USA
FixxeR Offline
Member
FixxeR  Offline
Member

Joined: Nov 2003
Posts: 108
Oklahoma, USA
Hopefully I can be of some help, again. There is a way to accomplish what you want, and it's actually not all that complex. But you have to be extremely careful in what you do, or else memory leaks and other problems will start popping up like the devil.

Code:

int main()
{
int *iData;
int i;

iData = (int *) malloc(sizeof(int) * 2);

iData[0] = 100;
iData[1] = 200;

i = iData[0];

if (i == 100)
MessageBox(NULL, "Success; i == 100", "Huzzah", MB_OK);

i = iData[1];
if (i == 200)
MessageBox(NULL, "Success; i == 200", "Huzzah", MB_OK);

return 0;
}



So, effectively, all you need to do (in this example) to expand your array is to iData = (int *)malloc(sizeof(iData) + sizeof(int)). This is a usable solution, but realize there are risks involved. If you don't have solid memory management, you run the risk of severe memory leakage causing strange problems in unrelated systems.

Hope this helps,
FixxeR


John Dies at the End - One of the best fictional stories online. Join the fight for Internet Freedom
Re: append to array [Re: FixxeR] #112883
02/20/07 22:04
02/20/07 22:04
Joined: Nov 2005
Posts: 94
Texas
3
3Dski Offline
Junior Member
3Dski  Offline
Junior Member
3

Joined: Nov 2005
Posts: 94
Texas
FixxeR, I don't see where "resizing" or "expanding" an array comes into play in the above. The above is only for the initial dynamic allocation of an array of N size.

I personally just tried link list construction via malloc(), with sizeof(), as well as using free() to clean things up. The manual doesn't really speak of these specificly, from my search, but VERY glad to see they're available.

This leads to the question, "Is realloc() available?" Even if it is, one still usually doesn't only increase an array by 1, but rather by some cushion and must keep track of free space to knowing when another realloc() is necessary.

Re: append to array [Re: 3Dski] #112884
02/21/07 00:11
02/21/07 00:11
Joined: Sep 2003
Posts: 9,859
F
FBL Offline
Senior Expert
FBL  Offline
Senior Expert
F

Joined: Sep 2003
Posts: 9,859
I'm currently working on some struct stuff which allows to append() and also use array indices afterwards.
It still has a lot of flaws, but the basic idea is working. More when I'm finished.

Re: append to array [Re: FBL] #112885
02/21/07 04:23
02/21/07 04:23
Joined: Nov 2005
Posts: 94
Texas
3
3Dski Offline
Junior Member
3Dski  Offline
Junior Member
3

Joined: Nov 2005
Posts: 94
Texas
I was first thinking of that kind of strategy, so interested in what you come up with.

It is a tricky problem, only because you do have to always know the lengths of all involved, and remember to free() all allocated memory. It is particularly tricky, since realloc() is apparently not available to us in LiteC. Though it may not be "perfect" here is one solution I came up with:

Code:

#include <acknex.h>
#include <default.c>

var gVal;

int *array1;
int *array2;
int *array3;

PANEL* selection1 = {
pos_x = 0; pos_y = 0;
digits(10,10,1,*,1,gVal);
flags = VISIBLE;
}


///////////////////////////////////////////////////////////
// Allocate for an int array with capacity. Sets a sentinal
// value of -999 so we can later test for the size of the array.
// (see length() below)
///////////////////////////////////////////////////////////
int* createIntArray(int* a, int capacity)
{
a = malloc(sizeof(int)*(capacity+1));
a[capacity] = -999;
return a;
}

///////////////////////////////////////////////////////////
// Counts number of array positions in array. Tests agains
// -999 to determine the end of the array.
///////////////////////////////////////////////////////////
int length(int *a)
{
int i = 0;
while(a[i] != -999)
{
++i;
}
return i;
}

///////////////////////////////////////////////////////////
// Appends contents of a2 to a1, resulting in a3. a1 and a2
// are not altered.
//
// NOTE: If you were to attempt to make a1 the new allocated
// address, then you would no longer be able to free the
// memory originally allocated to it.
///////////////////////////////////////////////////////////
int* appendIntArray(int* a1, int* a2, int* a3)
{
int i,j;
int k = 0;
int l1 = length(a1);
int l2 = length(a2);

a3 = createIntArray(a3, (l1+l2) );
for(i = 0;i < l1;i++)
{
a3[i] = a1[i];
}
for(j = i;j < l1+l2;j++)
{
a3[j] = a2[k];
k++;
}

return a3;
}

function main()
{
screen_color.blue = 150;

array1 = createIntArray(array1, 3);
array1[0] = 1;
array1[1] = 1;
array1[2] = 3;

array2 = createIntArray(array2, 3);
array2[0] = 4;
array2[1] = 5;
array2[2] = 6;

array3 = appendIntArray(array1, array2, array3);


gVal = array3[5];

// !!! make sure to clear all allocated memory !!!
free(array1);
free(array2);
free(array3);
}



I use a sentinal value of -999 to mark the end of my int arrays, and my own length() function that uses this sentinal to count the valid storage locations.

EDIT: Just found out that realloc() could be made available by using the DLL it is contained in... http://manual.conitec.net/litec_c.htm This would allow for better array appending.


Last edited by 3Dski; 02/21/07 05:19.
Re: append to array [Re: 3Dski] #112886
02/21/07 14:28
02/21/07 14:28
Joined: Nov 2003
Posts: 108
Oklahoma, USA
FixxeR Offline
Member
FixxeR  Offline
Member

Joined: Nov 2003
Posts: 108
Oklahoma, USA
You realize you just basically said what I said, 3Dski, =P. I just didn't include all the code.

FixxeR


John Dies at the End - One of the best fictional stories online. Join the fight for Internet Freedom
Re: append to array [Re: FixxeR] #112887
02/21/07 14:38
02/21/07 14:38
Joined: Jul 2006
Posts: 783
London, UK
sheefo Offline OP
User
sheefo  Offline OP
User

Joined: Jul 2006
Posts: 783
London, UK
Don't worry, you both have been really helpful.

Thank you. Now I have tonnes of useful code Dynamic Arrays, and Appending Arrays...

Re: append to array [Re: FixxeR] #112888
02/21/07 16:26
02/21/07 16:26
Joined: Nov 2005
Posts: 94
Texas
3
3Dski Offline
Junior Member
3Dski  Offline
Junior Member
3

Joined: Nov 2005
Posts: 94
Texas
Actually, your contribution contribution was very helpful to me. I've been working with Java professionally, and other web technologies for a good while now, so have forgotten some of C techniques. A sincere thanks : )

Page 1 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1