EvilZone
Programming and Scripting => C - C++ => : Super_mario666 March 05, 2013, 06:55:36 PM
-
Is there a way to expand a Dynamically allocated array after it has been declared?i know that this an be done with vectors but in the project im working on im not allow to include the standard template library.
-
realloc() ?
-
realloc() ?
i can't use cstdlib.h
-
Then use system calls. brk() and sbrk() are the ones malloc uses for allocating memory from the heap.
Look up on Google.
-
Then use system calls. brk() and sbrk() are the ones malloc uses for allocating memory from the heap.
Look up on Google.
i don't think i should use any OS specific libraries. there's a chance my prof. wont be able to compile it. is there a standard way of doing it?
-
Well, if you want to do it without using the std libs, you have to do it with system calls. And system calls depend upon system implementations. Different (paged) OS es handle memory allocations in slightly different ways. You can even do it with assembly if you would. But there 'll always be a dependency on how the OS manages memory.
-
What's about coding an own vector class. Then you would not use a library. Anyways you have to use at least new; and delete;.
The follow example code could be done a lot more efficient but you could use it as a reference
class DynamicIntArray {
private: int* data;
int size;
int curPos;
void realloc() {
size *= 2;
int* tmpData = new int[size];
for(int i = 0; i < curPos; i++)
tmpData[i] = data[i];
delete [] data;
data = tmpData;
};
public: DynamicIntArray(int size = 200) {
this->size = size;
curPos = 0;
data = new int[size];
};
inline int getAt(int pos) {
return data[pos];
};
inline void add(int value) {
if(curPos == size)
realloc();
data[curPos] = value;
curPos++;
}
inline void replace(int pos, int value) {
if(pos > size) return;
data[pos] = value;
}
~DynamicIntArray() {
delete [] data;
}
};
-
+1 to Teddy for denoting that you can implement your own vector.
In truth, the common vector implementation uses a linked-list. For that, you need only allocate memory for a single element and attach it to the chain. You can even sort simply by rearranging links.
@bluechill: damn..you got to this before I did...oh well. As for linked-lists relying on malloc(), there's no reason that it's necessary. Links can be allocated with 'new' just as well as they could with malloc().
@Super_mario666: it would help if you'd given all of the restrictions in the original post. Using realloc() is perfectly reasonable for a C-like solution, if you're permitted. However, of course, in C++ you should probably be using 'new' & 'delete'. It really sounds like your instructor is pushing you for the simplest, naive solution: allocate a new array, copy the data to it, change a pointer to point to the new array. bluechill's later post does indicate the general idea, but don't use the code verbatim. You MUST ensure that you are not invalidating any important data when you delete a temporary pointer. Again, a linked-list would likely be ideal. Keep in mind that a linked-list is a technique, not a library feature like a vector, map, deque, set, etc.
Sorry I didn't get to post on this sooner...work likes to keep me busy.
-
What's about coding an own vector class. Then you would not use a library. Anyways you have to use at least new; and delete;.
The follow example code could be done a lot more efficient but you could use it as a reference
class DynamicIntArray {
private: int* data;
int size;
int curPos;
void realloc() {
size *= 2;
int* tmpData = new int[size];
for(int i = 0; i < curPos; i++)
tmpData[i] = data[i];
delete [] data;
data = tmpData;
};
public: DynamicIntArray(int size = 200) {
this->size = size;
curPos = 0;
data = new int[size];
};
inline int getAt(int pos) {
return data[pos];
};
inline void add(int value) {
if(curPos == size)
realloc();
data[curPos] = value;
curPos++;
}
inline void replace(int pos, int value) {
if(pos > size) return;
data[pos] = value;
}
~DynamicIntArray() {
delete [] data;
}
};
You're using the standard libraries for new & delete just saying.
@OP If your professor is asking you to do C++ without the standard libraries.... that doesn't make much sense *at all*. Memory allocation is through the standard libraries and new and delete rely on that. Basically your professor either isn't telling you everything (such as new and delete relying on malloc & std. libraries) or you missed something in class or your professor is an idiot.
@Xires linked lists sure but that relies on malloc too..... basically I think the professor isn't telling him everything or he missed something.
-
You're using the standard libraries for new & delete just saying.
@OP If your professor is asking you to do C++ without the standard libraries.... that doesn't make much sense *at all*. Memory allocation is through the standard libraries and new and delete rely on that. Basically your professor either isn't telling you everything (such as new and delete relying on malloc & std. libraries) or you missed something in class or your professor is an idiot.
@Xires linked lists sure but that relies on malloc too..... basically I think the professor isn't telling him everything or he missed something.
i probably didnt explain as well as i wanted to. basically we are not allowed to use certain aspects of cstdlib or STL that we havent gone over in class. this includes vectors, linked list, deques and other part of STL. as for malloc() functions we were told specificity not to use them in any program we turn in, and to instead use new and delete.
-
i probably didnt explain as well as i wanted to. basically we are not allowed to use certain aspects of cstdlib or STL that we havent gone over in class. this includes vectors, linked list, deques and other part of STL. as for malloc() functions we were told specificity not to use them in any program we turn in, and to instead use new and delete.
new and delete both use malloc and he cannot tell the difference unless you also have to turn in source code. And well you can do the following if you need to "realloc"/expand your array:
int *main; //allocated to 50 places
int *temp; //temp copy
temp = main;
main = new int[100];
for (int i = 0;i < 50;i++)
main = temp;
delete temp;
that will do the equivalent of a realloc but it is quite inefficient. You will have to modify that a fair bit for sizes etc. but that's the jist of what you want. You basically create a new variable to store the original pointer, overwrite the pointer on the original variable with a bigger version, copy all the elements from the old pointer to the new one, delete the old pointer.