2 registered members (Akow, tomaslolo),
1,536
guests, and 12
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
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
OP
User
|
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
Member
|
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
|
|
|
Re: append to array
[Re: FBL]
#112885
02/21/07 04:23
02/21/07 04:23
|
Joined: Nov 2005
Posts: 94 Texas
3Dski
Junior Member
|
Junior Member
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.
|
|
|
|