Author Topic: [C++]Expand a Dynamic array  (Read 6481 times)

0 Members and 1 Guest are viewing this topic.

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
[C++]Expand a Dynamic array
« on: 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.
« Last Edit: March 05, 2013, 07:08:39 pm by Super_mario666 »
The Bigger they are...The more likely you'll get your ass kicked

Offline 0poitr

  • Peasant
  • *
  • Posts: 149
  • Cookies: 64
    • View Profile
Re: Expand a Dynamic array
« Reply #1 on: March 05, 2013, 07:03:20 pm »
realloc() ?
Imagination is the first step towards Creation.

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: Expand a Dynamic array
« Reply #2 on: March 05, 2013, 07:07:36 pm »
« Last Edit: March 05, 2013, 07:19:08 pm by Super_mario666 »
The Bigger they are...The more likely you'll get your ass kicked

Offline 0poitr

  • Peasant
  • *
  • Posts: 149
  • Cookies: 64
    • View Profile
Re: [C++]Expand a Dynamic array
« Reply #3 on: March 05, 2013, 07:30:42 pm »
Then use system calls. brk() and sbrk() are the ones malloc uses for allocating memory from the heap.
Look up on Google.
Imagination is the first step towards Creation.

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: [C++]Expand a Dynamic array
« Reply #4 on: March 05, 2013, 07:53:55 pm »
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?
The Bigger they are...The more likely you'll get your ass kicked

Offline 0poitr

  • Peasant
  • *
  • Posts: 149
  • Cookies: 64
    • View Profile
Re: [C++]Expand a Dynamic array
« Reply #5 on: March 05, 2013, 08:03:29 pm »
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.
Imagination is the first step towards Creation.

Offline Teddy

  • /dev/null
  • *
  • Posts: 12
  • Cookies: 8
    • View Profile
Re: [C++]Expand a Dynamic array
« Reply #6 on: March 06, 2013, 11:18:29 am »
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
Code: [Select]

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;
      }
};




« Last Edit: March 06, 2013, 11:21:13 am by Teddy »

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: [C++]Expand a Dynamic array
« Reply #7 on: March 25, 2013, 06:52:48 pm »
+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.
« Last Edit: March 27, 2013, 04:35:38 pm by Xires »
-Xires

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: [C++]Expand a Dynamic array
« Reply #8 on: March 26, 2013, 09:49:30 pm »
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
Code: [Select]

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.
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: [C++]Expand a Dynamic array
« Reply #9 on: March 26, 2013, 10:06:38 pm »
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.     
« Last Edit: March 26, 2013, 10:09:34 pm by Super_mario666 »
The Bigger they are...The more likely you'll get your ass kicked

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: [C++]Expand a Dynamic array
« Reply #10 on: March 27, 2013, 02:32:18 pm »
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.
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.