Advertisement

new, delete and arrays

Started by September 07, 2000 10:47 AM
14 comments, last by SteveBrown 24 years, 3 months ago
Yeah, ''new'' works exactly the same way like malloc() only it''s much more cooler because it''s an operator
I don''t know the command to resize allocated memory in C++, but if you want to be able to resize the size of the array at runtime you should consider linked lists.
Their harder to get working but better to use if you want to change the size of your array every minute



The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
Thanks again everyone for your help! I have two more questions though, if you don''t mind answering them.

1. What are linked lists?

2. How can I add an item to an array made with new. ex:

int * Pointer;
Pointer = new int[50]
// Here, code to make the array pointer have 51 elements.
Advertisement
Tornado,

Yes, I have considered using linked lists. The problem is that I want to acess the allocated memory directly, just by taking the index of the data. If I use linked lists, I should have a pointer to the first element and iterate until I find the one I''m interested. Well, maybe I''ll end up using something more complicated like binary trees or hash tables, don''t know yet. I''m going to do a little more research and report any findings here.



SteveBrown,



1- Linked lists are structures(or classes) that store the pointer to the next element in the list. You have a pointer to the first element, and the others are "linked" from the first. Of course, there are other types of linked lists, including bidirecional ones. I suggest you learn a little more about them, you''ll need them sooner or later. I can post some code if you need to make clearer.

2- That''s exactly the problem. If you could resize the allocated memory, that would be simple. Anyone considered using the pointer given by new to the realloc function? Will that work? Phew... even if it does, it would make the code ugly... fortunately it should be hidden inside a beautiful class



Gaiomard Dragon
-===(UDIC)===-
Gaiomard Dragon-===(UDIC)===-
Mickey Kawick (sp?) has a series of allocator classes in his book that do this for you. In essence though, it looks like he creates a new array (original size plus growth rate) and simply does a memcpy() from the original array to the new array. This in turn leaves the extended growth area for use (I guess).

Jumpster
Regards,JumpsterSemper Fi
Well, this should work, but it seems like a waste of memory for big structures... Still, I can see some uses for it in my program. Thanks Jumpster.

Gaiomard Dragon
-===(UDIC)===-
Gaiomard Dragon-===(UDIC)===-
What do you people have against vector?
        vector<point> points(1024);  // point *points = new points[1024]vector<int> ones(20,1);  // int *ones = new int[20]; then assign                         // 1 to eachpoints[3].x = 6;  points[y*width+x] = points[3];  // copies points[3] to 2                      //dimentional array of width columnspoints.at(7).x = 2; // throws range_error if 7 is out of bounds                    // just like [7] otherwisepoints.push_back(point(3,4,5)); // Appends a new point to the                                // end of points (may grow)points.size() // number of elements in pointsvector<vector<int>> ez2d; // Ugly, Slower 2 dimentional arrayex2d[x][y] = ones[3];/*  Try to do this as cleanly with int[]*/int avg(const vector<int> &a) {  long sum = a.at(0); // throws if array is empty  for(int i=1;i<a.size();i++) sum += a<i>;  return static_cast<int>(sum/a.size());}/*This is a big huge win over linked lists in every wayexcept for inserting an element into the middle of the vectorin that case linked lists win, but it's still easyer (though the same speed) to do insert with a vector than []*/points.insert(points.begin()+10,point(10,20,10));        


In some cases you are better off talking to the os and getting your own VM segment and simply commiting more pages to it for resizing. realloc in newer compilers/os's is allowed to, and often does, return a new pointer if you are expanding. Vector's resize will likely be implemented with fancy VM magic if it's available, but even in Linux, you need to use mmap() to get that "free" array resizing nowadays.

Use vector or wrap vector with your own class, then, when your done, profile and see if it's squeeking. If so you can go in and hack, but I will bet that less than a 2% improvement could be found with a fancier class.


Edited by - Grib on September 13, 2000 2:24:58 AM

This topic is closed to new replies.

Advertisement