Custom Array class memory location modification

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1896 Views - Last Post: 31 January 2009 - 11:33 AM Rate Topic: -----

#1 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,208
  • Joined: 07-September 06

Custom Array class memory location modification

Posted 29 January 2009 - 09:48 PM

Well, the title pretty much says it all. I have code in which I am creating and mallocing memory for a custom array:

Array(int maxSize){
		size = 0;
		ptrSize = sizeof(T); // from template<class T>
		startingAddr = (int*)malloc(ptrSize*maxSize);
	}


The problem is that when I try to add an item to the array I continue to get errors (there are all sorts of errors I have recieved based on what I have changed in an attempt to get it working). Basically, I am attempting to set the memory at an address to a particular value, which isn't working (the allocation seems to be working fine).

Here is my code for adding an item:

	void push(T obj){
		(startingAddr+(size*ptrSize)) = obj;
	}


I haven't added the size modification yet because I have yet to get this working...

Basically, the startingAddr is the address of the beginning of the allocated memory (which I am placing in an int*), the size if the number of elements in the array, and the ptrSize is the size of the object that the array is holding. The idea behind this is that I will have a memory location (after the equation is done) that I can set to the new object (the one passed into the function).

Any help (or pointing to help, I have googled, but was unable to find a clear, definitive answer) would be great.

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Custom Array class memory location modification

#2 buckrogers1965  Icon User is offline

  • New Member

Reputation: 9
  • View blog
  • Posts: 87
  • Joined: 23-January 09

Re: Custom Array class memory location modification

Posted 29 January 2009 - 10:00 PM

How are you defining T?

For sizeof() to work you need an instance of a structure, not a pointer or a typedef.

This post has been edited by buckrogers1965: 29 January 2009 - 10:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Custom Array class memory location modification

Posted 29 January 2009 - 10:03 PM

What is special about this array? I see you have a template somewhere and that would indicate that any object/primitive could eventually be used with it. You seem to be reinventing the wheel though, because a regular array[] has those functions implicitly built into the subscript operator.

The part that seems odd to me is the push function. Is it a stack? An array stack? Anyway, going along with what you have, the compiler is probably throwing an error because there is no one value on the left side of the assignment operator. An array automatically allocates memory based on the initial values and a vector resizes itself--is this what you're going for? A custom array that can "resize" itself?
Was This Post Helpful? 1
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,208
  • Joined: 07-September 06

Re: Custom Array class memory location modification

Posted 29 January 2009 - 10:31 PM

I found the issue (took a bit of head-on-desk action, but it is solved).

I had to change startingAddr to an int** and then had to change the push function to look like so:
void push(T* obj){
		*(startingAddr+size) = obj;
		//memcpy((startingAddr+(size*ptrSize)), obj, ptrSize); // found and was using at first, but then I found the above way to do it.
		size++;
	}


Basically what I am doing is going about making something like a vector at the moment (though I am going to have it act like an array more or less as well) then I am going to try and extend it so you can place any datatype inside of it, and get backto the datatype easily afterwards (hence the reason for using pointers to objects instead of objects)). And, yes it will eventually be able to modify its own size in an attempt to avoid problems with adding elements to it. So, I guess it is a little like a vercor array...

Thanks for the help
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Custom Array class memory location modification

Posted 29 January 2009 - 10:34 PM

*(startingAddr+size) = obj;



Now you have "one" value on the left

Glad you got it worked out. :)


edit: My curiosity got the better of me, what handle remains to each element in the array? You have the initial allocator T** which points to a number of other pointers, each "T" object has its own pointer right? Which i imagine you'll use the [] operator to access later?

This post has been edited by KYA: 29 January 2009 - 10:43 PM

Was This Post Helpful? 0
  • +
  • -

#6 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,208
  • Joined: 07-September 06

Re: Custom Array class memory location modification

Posted 30 January 2009 - 01:25 PM

Here is the whole code I have:

#include<iostream>
using namespace std;

template<class T> class VectorArray{
private:
	int lastIndex;
	int size;
	int ptrSize;
	int** startingAddr;
public:
	VectorArray(void){
		lastIndex = 10;
		size = 0;
		ptrSize = sizeof(char*);
		startingAddr = (int**)malloc(ptrSize*lastIndex);
	}
	VectorArray(int startingSize){
		lastIndex = startingSize;
		size = 0;
		ptrSize = sizeof(char*);
		startingAddr = (int**)malloc(ptrSize*lastIndex);
	}
	void push(T* obj){
		if(size >= lastIndex){
			resize(lastIndex+100);
		}
		*(startingAddr+size) = obj;
		//memcpy((startingAddr+(size*ptrSize)), obj, ptrSize);
		size++;
	}
	void resize(int newSize){
		int** currentStartingAddr = startingAddr;
		startingAddr = (int**)malloc(ptrSize*newSize);
		for(int i=0; i<size; i++){
			*(startingAddr+i) = *(currentStartingAddr+i);
		}
		delete currentStartingAddr;
		lastIndex = newSize;
		cout << "Resized to " << lastIndex << endl;
	}
	int getlastIndex(void){
		return lastIndex-1;
	}
	T* get(int index){
		if(index >= size || index < 0){
			cout << "Error - Index out of bounds" << endl;
			return NULL;
		}
		return *(startingAddr+index);
	}
	~VectorArray(void){
		delete startingAddr;
	}
	T* operator[](int index){
		return get(index);
	}
};

int main(){
	VectorArray<int> a(1);
	for(int i=0; i< 200; i++){
		a.push(new int(rand()%1000));
	}
	for(int i=0; i< a.getlastIndex(); i++){
		cout <<	"a[" << i << "] = " << *a[i] << endl;
	}
	return 0;
}


(So you can see exactly what I am doing, should be easier that way).

I have come up with a nother question though. Is there any way to have a variable return type for a function? Something like so:
class test{
private:
  class returnType;
public:
  // ... standard functions
  returnType r(int a){
	returnType = double;
	return (double)a*3.14;
  }
};
int main(){
  test a;
  cout << a.r(5) << endl; // should (with any luck) output 15.6
  return 0;
}


I have googled it, but haven't been able to find anything... any help would be great :)
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Custom Array class memory location modification

Posted 30 January 2009 - 04:14 PM

Why wouldn't you know that at compile time though? While I explore your question, an alternative would be to use pointer functions to return the correct function based on parameter or desired return type.

edt: C++ is very type specific, so if (and I'm working on coming up with an example) we could do what you're asking, we'd have to pass that information to the program before the function executes,, however we couldn't compile because of errors regarding a missing return type.


5 x 3.14 = 15.7 btw ;)

This post has been edited by KYA: 30 January 2009 - 04:24 PM

Was This Post Helpful? 0
  • +
  • -

#8 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,208
  • Joined: 07-September 06

Re: Custom Array class memory location modification

Posted 30 January 2009 - 04:39 PM

Quote

5 x 3.14 = 15.7

Once you get to calc it doesn't matter. If the derivative is the same it is fine (both come out to 0 lol) :)

Quote

Why wouldn't you know that at compile time though?

What I am hoping to be able to do eventually is make this VectorArray hold any datatype you give it (since pointers are all the same size it should be possible... I think...) which would mean that when you use the get(index) or [index] it couldn't just return a T*, T would have to be variable (able to change).

I am pretty sure it can be done in some fashion (PHP allows for it (I believe) at least). The hard part is recreating it (Or, in the possible case I am mistaken about PHP allowing it creating it for the first time).

Thanks for trying to help out.
Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Custom Array class memory location modification

Posted 30 January 2009 - 04:44 PM

OH, in that case:

//include the T template thing up here
class test{
public:
  // ... standard functions
  T r(int a){ //will return whatever T is
	return a*3.14;
  }
};
int main(){
  test a;
  cout << a.r(5) << endl; // should (with any luck) output 15.6
  return 0;
}



retrunType is whatever is the the primitive, class, etc... so you can use your tempalte variable here :)

This post has been edited by KYA: 30 January 2009 - 04:44 PM

Was This Post Helpful? 0
  • +
  • -

#10 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,208
  • Joined: 07-September 06

Re: Custom Array class memory location modification

Posted 30 January 2009 - 04:47 PM

But, does that allow for you to change what the templatived variable is inside of a function?

Like:
  T r(int a){ //will return whatever T is
	T = double;
	return a*3.14;
  }


Or can you just set it once and it is set that way for the life of the object?
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Custom Array class memory location modification

Posted 30 January 2009 - 04:50 PM

This array holds the same objects. So once it is created that is what it will hold forever and ever until the program gets killed. Why would you want to change T? If T is already a double, then it will return 15.7, if T is an int it will return 15. As far as I know you can't change the return type of the function, mid function, I could be wrong though.
Was This Post Helpful? 0
  • +
  • -

#12 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Custom Array class memory location modification

Posted 31 January 2009 - 07:48 AM

Don't question why somebody makes something, simply help them make it.
Was This Post Helpful? 0
  • +
  • -

#13 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Custom Array class memory location modification

Posted 31 January 2009 - 08:22 AM

View PostHyper, on 31 Jan, 2009 - 07:48 AM, said:

Don't question why somebody makes something, simply help them make it.


Doing so helps further my learning as well.
Was This Post Helpful? 0
  • +
  • -

#14 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Custom Array class memory location modification

Posted 31 January 2009 - 08:33 AM

View PostKYA, on 31 Jan, 2009 - 07:22 AM, said:

View PostHyper, on 31 Jan, 2009 - 07:48 AM, said:

Don't question why somebody makes something, simply help them make it.


Doing so helps further my learning as well.


Well, in that case...
Was This Post Helpful? 0
  • +
  • -

#15 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Custom Array class memory location modification

Posted 31 January 2009 - 09:14 AM

Plus I don't see the point of doing something unless there's a "how" and a "why".
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2