Jump to content

  • Log In with Google      Sign In   
  • Create Account


C++ pointer to struct [edited]


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Alessandro   Members   -  Reputation: 287

Like
0Likes
Like

Posted 05 October 2011 - 02:04 PM

Hi, I have the following code that I use to declare a static number of structs and fill it with data:

struct myObjects::myObjectLib myObject[MAX_OBJECTS];
for (int i=0; i<MAX_OBJECTS; i++)
 {
 	myObject[i].x=...
 	myObject[i].y=...
 	myObject[i].z=...
 }

I'd like to define structs dinamically and so I used the following instruction:

struct myObjects::myObjectLib *myObject;

How can I initialize myObject struct so that later I can fill it up with values like in the static example?

Thanks again for your help

Sponsor:

#2 Molle85   Members   -  Reputation: 172

Like
0Likes
Like

Posted 05 October 2011 - 02:20 PM

struct myObjects::myObjectLib *myObject = new myObjects::myObjectLib[MAX_OBJECTS];

You need to learn about memory allocation, you have to free the memory yourself via 'delete'

#3 SiCrane   Moderators   -  Reputation: 9386

Like
0Likes
Like

Posted 05 October 2011 - 02:23 PM

You may want to consider using a std::vector instead.

#4 Oberon_Command   Crossbones+   -  Reputation: 1794

Like
0Likes
Like

Posted 05 October 2011 - 02:26 PM

You also don't need the 'struct' keyword when you're declaring instances of structures in C++.

#5 Alessandro   Members   -  Reputation: 287

Like
0Likes
Like

Posted 05 October 2011 - 02:40 PM

struct myObjects::myObjectLib *myObject = new myObjects::myObjectLib[MAX_OBJECTS];

You need to learn about memory allocation, you have to free the memory yourself via 'delete'


Mmmhhhh... why using the pointer (*myObject)? Why also the MAX_OBJECTS?

I was thinking instead that the following shoud work:

myObject = new myObjects::myObjectLib;

Everytime I call it, shouldn't it create a new struct, so that I'd have a myObject[0],myObject[1],myObject[n] ?

#6 Alessandro   Members   -  Reputation: 287

Like
0Likes
Like

Posted 05 October 2011 - 02:41 PM

You may want to consider using a std::vector instead.


They seem to be extremely complicated to use. Am I wrong?

#7 Alessandro   Members   -  Reputation: 287

Like
0Likes
Like

Posted 05 October 2011 - 02:41 PM

You also don't need the 'struct' keyword when you're declaring instances of structures in C++.


Right, thanks!

#8 Wooh   Members   -  Reputation: 544

Like
0Likes
Like

Posted 05 October 2011 - 02:55 PM

I was thinking instead that the following shoud work:
myObject = new myObjects::myObjectLib;
Everytime I call it, shouldn't it create a new struct, so that I'd have a myObject[0],myObject[1],myObject[n] ?

No that will not work. new myObjects::myObjectLib gives you a pointer to a newly created myObjectLib object. If you don't keep track of the other objects they are lost, and you have a memory leak.


You may want to consider using a std::vector instead.

They seem to be extremely complicated to use. Am I wrong?

Yes you are wrong. std::vector is much easier to work with. You can declare the vector like this: std::vector<myObjects::myObjectLib> myObject;
To add a new element in the vector you can do something like myObject.push_back(myObjects::myObjectLib());
You can access the elements like in a normal array, myObject[0] being the first element in the vector.

#9 assainator   Members   -  Reputation: 636

Like
0Likes
Like

Posted 05 October 2011 - 02:56 PM


You may want to consider using a std::vector instead.


They seem to be extremely complicated to use. Am I wrong?


yes, you're wrong.

std::vector<int> vec;
vec.push_back(5);
vec.push_back(6);
vec.push_bacK(3);

for(unsignd int i = 0; i < vec.size(); i++)
{
	printf("%d\n", vec[i]);
	if(vec[i] == 17)
		printf("I didn't put this number in the vector!");
}

output:

5
6
3


Allocates space whenever it needs it, frees space it doesn't need, calls the destructor of every object that's inside the vector (in the case of classes/structs).

See http://cplusplus.com/reference/stl/vector/ for more info.
"What? It disintegrated. By definition, it cannot be fixed." - Gru - Dispicable me

"Dude, the world is only limited by your imagination" - Me


#10 Alessandro   Members   -  Reputation: 287

Like
0Likes
Like

Posted 05 October 2011 - 03:04 PM

Shame on me. Followed your suggestions and examples about vectors and they work perfectly. Thanks again for your continued help!

#11 alnite   Crossbones+   -  Reputation: 2038

Like
1Likes
Like

Posted 05 October 2011 - 03:58 PM

Though I am for using vectors, you still need to learn about memory allocations and deallocations. C++ has so many pitfalls that you need to be aware of. Improper usage of memory can turn your app inside out and you won't know why, and it's hell of a bitch to track down.

#12 FlyingDutchman   Members   -  Reputation: 207

Like
0Likes
Like

Posted 05 October 2011 - 05:13 PM

the vector class is pretty cool if u have to change the size of memory at runtime.. consider the following example

t_tile **tile;

void makeMap (int rows, int cols) {

	*tile = (t_tile**)malloc(sizeof(t_tile*rows));
	for (int i = 0; i < rows; i++) {
		tile[i] = (t_tile*)malloc(sizeof(t_tile*cols));
	}

}

So, now we have enough memory for a 2d array which we can access easily through tile[i][j]..
What if we want to change the map size on runtime AND we don't want to delete our old memory because we still need? (Consider a map editor)
We would have to do the following


void resizeMap (int rows, int cols) {

	// allocate temp 2d array of the same size as the old map
	t_tile **temp;

	*temp = (t_tile**)malloc(sizeof(t_tile)*cols);
        for (int i = 0; i < old_rows; i++) {
		temp[i] = (t_tile*)malloc(sizeof(t_tile)*rows);
	}

	// copy the old value into temp
	for (int i = 0; i < old_rows; i++) {
		for (int j = 0; j < old_cols; j++) {
			temp[i][j].dataset = tile[i][j].dataset;
		}
	}

	// recalculate memory for tiles
        *tile = (t_tile**)realloc(NULL, sizeof(t_tile)*rows);
   	for (int i = 0; i < rows; i++) {
		tile[i] = (t_tile*)realloc(NULL, sizeof(t_tile)*cols);
	}

	// copy temp into new reallocated memory
	for (int i = 0; i < old_rows; i++) {
		for (int j = 0; j < old_cols; j++) {
			if (
			tile[i][j].dataset = temp[i][j].dataset;
		}
     }

     for (int i = 0; i < old_rows; i++) {
		free(*temp[i]);	
     }

     free(temp);

	old_rows = rows;
	old_cols = cols;

}


pretty much messy code.. btw, if there are any mistakes.. don't blame me.. its 1:10 AM here, i wrote that out of my memory after watching three apple dev videos about __weak and __autoreleased objects ^^

under the bottom-line, we see that this is a lot of code.. the vector class takes ALL this away from us!!! we can add and remove objects as we want and it is pretty efficient as well..

Only thing why i don't use it pretty often is...

it leaks when u store a pointer to a class in the vector.. so don't use

class_name *object with a STL vector.. only use class_name object

also keep in mind that it will always call the constructor and the destrunctor if u add or remove objects.. this can especially be very tricky if u store openGL textures in one memory locations accessed by a lot of objects and u delete this memory by calling glDeleteTexture in a destructor.. if one object gets removed from the vector list, all objects loose their reference..

i go to sleep now, good night :-)

I open sourced my C++/iOS OpenGL 2D RPG engine :-)


See my blog: (Tutorials and GameDev)

http://howtomakeitin....wordpress.com/





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS