7 Replies - 466 Views - Last Post: 09 April 2012 - 03:57 PM Rate Topic: -----

#1 NecroWinter  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 258
  • Joined: 21-October 11

resizing arrays

Posted 08 April 2012 - 09:37 PM

so I know that arrays are fixed size, but the project im working on requires that i use them, and resize them. the approach that must be taken is to copy all the elements and essentially reset the bounds of the array and copy it back.

Ive done this in java by setting the array = new array[bigger number here](or something to that effect) how does one do this in c++? This should accomplish erasing the data in ary that exists, and essentially starting over with the variable.

It should be pointed out that im doing a circular array (queue) and in my header file for the class, I have the following:

 private:
  int front;
  int back;
  int ary[];


in my constructor i have the following:

queuei::queuei(){
  // basic constructor;
  front=-1;
  back=-1;
  int ary = new int[10]; // this is what i want to do, i know the syntax is wrong...
}



in the end, the idea is to reuse the same variable name. Later on I make a new array, copy everything over to it with a bigger size, reallocate "ary" and copy everything back to avoid using a new variable name.

I know I can use vectors, but im in a class, and this is part of the assignment.

thanks

This post has been edited by NecroWinter: 08 April 2012 - 09:39 PM


Is This A Good Question/Topic? 0
  • +

Replies To: resizing arrays

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 869
  • View blog
  • Posts: 3,996
  • Joined: 09-June 09

Re: resizing arrays

Posted 08 April 2012 - 09:47 PM

In Java, when you allocate memory it's allocated on the run time heap (unless it uses escape analysis). In C++ you can choose to allocate memory on the stack, or the heap (run time). To allocate dynamic memory on the heap you need to create a pointer to it.

example
int x; //allocated on stack
int *y = new int; //alocated on heap
delete y; //you need to delete the memory when you finished
          //there is no life saving garbage collector




Here's a wiki page with the differences between C++ and Java :
http://en.wikipedia....C%2B%2B#Runtime

This post has been edited by jjl: 08 April 2012 - 09:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4888
  • View blog
  • Posts: 11,282
  • Joined: 16-October 07

Re: resizing arrays

Posted 09 April 2012 - 05:44 AM

You're going to want to track the size. I'd go with something like:
private:
	int front;
	// sorry, back confuses the crap out of me with queues
	// int back; 
	int size; // I prefer this
	// int ary[];
	// you want
	int *data;
	// also
	int capacity;
	
// ...

queuei::queuei(){
  // basic constructor;
  front = size = 0;
  data = NULL;
  resize(10);
}

// this should be private
queuei::resize(int newCapacity) {
	int *newData = new int[newCapacity];
	if(data!=NULL) {
		// now copy what's there
		// the copy would be best done by running your dequeue function
		// make sure to "delete []" the current data after
	//...

	capacity = newCapacity;
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 NecroWinter  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 258
  • Joined: 21-October 11

Re: resizing arrays

Posted 09 April 2012 - 01:37 PM

im a little confused. I cant set the int data to null, I get an invalid type error
also, if im gonna use a pointer, wouldnt I need an array of pointers to keep track of every element in the array? If I need an array of pointers, im back right at square one with the need to resize an array.
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 869
  • View blog
  • Posts: 3,996
  • Joined: 09-June 09

Re: resizing arrays

Posted 09 April 2012 - 03:35 PM

When you allocate memory in java like so:
int []arr = new int[5];



arr is actually a pointer to the first element in the array.

This concept is the same with C++, but you have to explicitly give the pointer notation.
int *arr = new int[5];



This is also the case with any object.

Example:
(Java)
MyObject obj = new MyObject();



obj is a pointer to the newly allocated memory

(C++)
MyObject *obj = new MyObject();



obj is a pointer to the newly allocated memory, we just have to show it explicitly.

This post has been edited by jjl: 09 April 2012 - 03:37 PM

Was This Post Helpful? 0
  • +
  • -

#6 NecroWinter  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 258
  • Joined: 21-October 11

Re: resizing arrays

Posted 09 April 2012 - 03:48 PM

View Postjjl, on 09 April 2012 - 03:35 PM, said:

When you allocate memory in java like so:
int []arr = new int[5];



arr is actually a pointer to the first element in the array.

This concept is the same with C++, but you have to explicitly give the pointer notation.
int *arr = new int[5];



This is also the case with any object.

Example:
(Java)
MyObject obj = new MyObject();



obj is a pointer to the newly allocated memory

(C++)
MyObject *obj = new MyObject();



obj is a pointer to the newly allocated memory, we just have to show it explicitly.


although my code is actually working at this point. im not entirely sure why it does.
So if you have a pointer that points to the first element, why then can I iterate and populate the array? Shouldnt I be only able to edit the first element?
Was This Post Helpful? 0
  • +
  • -

#7 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 603
  • View blog
  • Posts: 1,864
  • Joined: 02-August 09

Re: resizing arrays

Posted 09 April 2012 - 03:54 PM

Quote

So if you have a pointer that points to the first element, why then can I iterate and populate the array? Shouldnt I be only able to edit the first element?


The pointer to the base of the array is indexed to access subsequent elements.
Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 869
  • View blog
  • Posts: 3,996
  • Joined: 09-June 09

Re: resizing arrays

Posted 09 April 2012 - 03:57 PM

Array's are allocated sequentially. If you have a pointer (memory address) to the first element, then then then following address will be the next element and so on.

example
int arr[5] = {1,2,3,4,5}; //Allocated on the stack, arr points to first element

int first = *arr ; //equivalent to *(a + 0) or a[0]
int second = *(arr + 1); //equivalent to a[1]
int third =  *(arr + 2); //equivalent to a[2]
int forth =  *(arr + 3); //equivalent to a[3]
int fifth =  *(arr + 4); //equivalent to a[4]



Using the "*" operator deferences (gets the value) at the given memory location. Just remember that pointers simply "point" to a location in memory. The [] operator of arrays is just for syntactical sugar.

This post has been edited by jjl: 09 April 2012 - 04:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1