append to array

Posted By: sheefo

append to array - 02/20/07 17:34

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.
Posted By: jcl

Re: append to array - 02/20/07 17:55

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.
Posted By: sheefo

Re: append to array - 02/20/07 18:27

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...?
Posted By: FixxeR

Re: append to array - 02/20/07 19:38

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
Posted By: 3Dski

Re: append to array - 02/20/07 22:04

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.
Posted By: FBL

Re: append to array - 02/21/07 00:11

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.
Posted By: 3Dski

Re: append to array - 02/21/07 04:23

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.

Posted By: FixxeR

Re: append to array - 02/21/07 14:28

You realize you just basically said what I said, 3Dski, =P. I just didn't include all the code.

FixxeR
Posted By: sheefo

Re: append to array - 02/21/07 14:38

Don't worry, you both have been really helpful.

Thank you. Now I have tonnes of useful code Dynamic Arrays, and Appending Arrays...
Posted By: 3Dski

Re: append to array - 02/21/07 16:26

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 : )
Posted By: FBL

Re: append to array - 02/22/07 20:25

As promised....
http://www.coniserver.net/ubbthreads/showflat.php/Cat/0/Number/731246/an/0/page/0#Post731246

Is this what you were searching for?
© 2024 lite-C Forums