Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
138 user(s) are online (132 user(s) are browsing Forums)

Members: 0
Guests: 138

more...

Support us!

Headlines

 
  Register To Post  

GCC, resize dynamically an array. Possible ?
Quite a regular
Quite a regular


See User information
Hi All,

I've checked my entire C book concerning arrays and I did find nothing related to resizing an array.

Is it possible to directly change the size of an array inside a program ?

(post that kind of questions will probably makes me be considered as a noob ... it's a risk ... but progress nevers happen without risks ;) )

Kindest Regards,
Freddix

All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, resize dynamically an array. Possible ?
Just can't stay away
Just can't stay away


See User information
@freddix

The only way to resize a memory allocation is to allocate a new piece of memory and copy the data over.

// returns smaller value of x and y
#define MIN(x,y) (((x) < (y)) ? (x) : (y))

void *Realloc(void *oldmemint oldsizeint newsize) {
  
void *newmem;
  
newmem AllocVec(newsize0);
  if (
newmem) {
    
CopyMem(oldmemnewmemMIN(oldsizenewsize));
  }
  
FreeVec(oldmem);
  return 
newmem;
}

Go to top
Re: GCC, resize dynamically an array. Possible ?
Quite a regular
Quite a regular


See User information
@salass00
Thank you :)

So, I can use this function for Array resizing then ?

All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, resize dynamically an array. Possible ?
Just can't stay away
Just can't stay away


See User information
@freddix

You can if you allocate your array from the free store.

int *array;
// allocate array of 50 int values
array = AllocVec(sizeof(int)*500);

// do something with array here

// resize to 100 elements
array = Realloc(array, sizeof(int)*50sizeof(int)*100);


Note that currently it doesn't clear the memory. If you want it to be cleared (set to all zeros) you should change the 2nd argument of AllocVec to MEMF_CLEAR.

Go to top
Re: GCC, resize dynamically an array. Possible ?
Quite a regular
Quite a regular


See User information
@salass00
Thank you for these clean explanations :)
Now I have all informations to continue my project (until the next question :p)

All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, resize dynamically an array. Possible ?
Supreme Council
Supreme Council


See User information
While arrays are handy for fixed amounts of data, if you find yourself with variable amounts, may I suggest you move to linked lists.

Resizing arrays is extremely hard on memory (typically fragmenting it more and more (although the situation on OS4 is better than previous versions)) and is generally kludgy and very error prone.

A linked list allows you to dynamically add "nodes" to the list, remove them, sort them etc. Check out the "Exec Lists" section of the "RKRM Devices" if you have a copy, or at least check the AddTail(), AddHead(), Remove(), Insert() ,Enqueue(), GetHead(), GetSucc() and GetPrev() function autodocs.

If you dont need the overhead of a full list, a MinList might be more suitable. Again the function autodocs should point you in the right direction, and certainly the RKRMs.

The RKRMs are available in AmigaGuide format on the developer CD2.1 available from a good Amiga shop near you :)

There should be plenty of examples out there too, as code intended for 2.04 is still valid when it comes to linked lists.

Simon

Go to top
Re: GCC, resize dynamically an array. Possible ?
Quite a regular
Quite a regular


See User information
@Rigo:
I already own the Developer CD :p
In fact, my need is to handle a list that can contain from approx 1 element to 65535 elements.

All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, resize dynamically an array. Possible ?
Amigans Defender
Amigans Defender


See User information
@freddix
Dynamic arrays are a feature added to C99. I think you should consider getting a more up to date C book.

ExecSG Team Lead
Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2024 The XOOPS Project