Shrinking An Array After Creation

Sage
Posts: 1,066
Joined: 2004.07
Post: #1
I have an array of, say, 3000 items created as such in C++:
Code:
int myArray[3000];

Is there a way, after creating this variable, to reduce the memory usage from 3000 ints to only 1500 or any other number? I thought maybe a delete function but those are for pointers, right? I couldn't think of a way to do this.

The reason I would like to do this is for a class I'm working on. The class holds the maximum number of items that are allowed to be stored. After initialization, it usually never needs all the spaces. I just want to free up the extra memory for use for other variables.
Quote this message in a reply
Member
Posts: 277
Joined: 2004.10
Post: #2
In C: no

In C++: probably not

Global warming is caused by hobos and mooses
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #3
Smile haha. Ok. That's what I figured. Oh well. Hopefully I don't waste too much memory. Hopefully I'll just figure out how to use fewer items in the array.
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #4
Why not just dynamically allocate it?
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #5
Code:
int yourSize = 100;
int* yourArray = new int[yourSize];
int resizeSize = 50;
int* tempArray = new int[resizeSize];

for(int i = 0;i<resizeSize;i++)
     tempArray[i] = yourArray[i];
delete[] yourArray;
yourArray = new int[resizeSize];
for(int i = 0;i<resizeSize;i++)
     yourArray[i] = tempArray[i];
delete tempArray;

doStuff(yourArray);

delete[] yourArray;
Quote this message in a reply
Member
Posts: 257
Joined: 2004.06
Post: #6
You can't use an STL vector?

The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #7
lets say at run time you determine you need "X" ints - use the "new" and "delete" operator:

Code:
int* myArray = new int[X];  // Allocate memory for the array
//
// do stuff with myArray
// dont access past myArray[X-1] or trouble ensues
//
// when you are done with the array
delete [] myArray;
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #8
Once you exit the current function that array will be popped off the stack, automatically be resized to 0, and recycled for you! Twice as much work done for you and all you have to do is call 'return; ' Grin Then, whenever you want to quit your program, just reference that array again and get a segmentation fault, the user doesn't even have to click quit. Oh modern technology, the utility never ends!

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
Member
Posts: 567
Joined: 2004.07
Post: #9
just create a pointer, then malloc it when you know the size, if that's what your looking for. If you have objects, just create a new array with the appropriate size, then memcp(is that right?)the old one into the new one.

It's not magic, it's Ruby.
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #10
or if your array will grow and shrink use a STL vector.
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #11
lol - looks like we all typed in the same answers at the same time.
Quote this message in a reply
Member
Posts: 198
Joined: 2005.01
Post: #12
Heh, response-blitz Smile

To directly answer your question, C++ objects have a fixed size at compile time and that can never change at runtime. You can cheat a little with structs in C by doing this:

struct foo {
int baz;
int bar[0];
}

And then allocate it with malloc(sizeof(foo) + sizeof(int)*numelems). That won't work with C++ objects (or structs with methods) because you have things like the vtable in there somewhere.

I second the STL (std::vector) recommendation Smile

Cryptic Allusion Games / Cryptic Allusion, LLC
http://www.cagames.com/
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #13
You could also use a single or doubly linked list... depending on what you need to do.

Linked lists are more memory efficient than arrays, but at the expense of performance (since you need to iterate through the list to get to the item you want, and also have to make lots of malloc calls). So it all depends on what you intend to do with it.

Here's a doubly linked list:

typedef struct Hanger {
Hanger *prev;
Hanger *next;
int data; // could alternatively be a pointer to some malloc'ed data
} Hanger;

In this implementation, the first an last Hangers don't hold data. This makes the code much cleaner. Initially, the first Hanger in the linked list would have prev set to NULL, and next pointing to last Hanger. The last Hanger's prev would point to the first Hanger, and its next would be set to NULL.
To insert a new item, malloc a Hanger struct, iterate to where you'd like to put it in the list, the adjust its prev/next pointers, as well as those of the 2 adjacent Hangers.

Of course, the above leaves out a lot of implementation details, but the basic concept of a doubly linked list is there.

And remember, linked lists are built with plain C code, so they're compatible with C, C++, and objective-C Smile
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #14
Andrew Wrote:Linked lists are more memory efficient than arrays

I think you mean "much less memory-efficient than arrays".

By the time you've got the extra link pointers, and all the extra overhead of having many malloc blocks, you're looking at a /lot/ more memory than a correctly-sized array.

And that's before we start talking about the horrendous inefficiency and cache behavior in linked lists.

They do have their place, but it's a long, long time since I've found a home for them in any of my code.
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #15
I think the next obvious question here is: do you REALLY need to resize your array? Leaving half the array at worst will only waste a kilobyte or two for the duration of your function, and you can probably afford that with a modern computer Rasp

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  memcpy(stuct array pointer struct array point) unknown 22 10,961 Sep 29, 2005 03:16 PM
Last Post: unknown