Sign in to follow this  
Alessandro

C++ pointer to struct [edited]

Recommended Posts

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

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

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

[code]struct myObjects::myObjectLib *myObject;[/code]

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

Share this post


Link to post
Share on other sites
[code]struct myObjects::myObjectLib *myObject = new myObjects::myObjectLib[MAX_OBJECTS];[/code]

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

Share this post


Link to post
Share on other sites
[quote name='Molle85' timestamp='1317846057' post='4869523']
[code]struct myObjects::myObjectLib *myObject = new myObjects::myObjectLib[MAX_OBJECTS];[/code]

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

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] ?

Share this post


Link to post
Share on other sites
[quote name='Alessandro' timestamp='1317847250' post='4869536']
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] ?
[/quote]
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.

[quote name='Alessandro' timestamp='1317847283' post='4869537']
[quote name='SiCrane' timestamp='1317846206' post='4869525']
You may want to consider using a std::vector instead.
[/quote]
They seem to be extremely complicated to use. Am I wrong?
[/quote]
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.

Share this post


Link to post
Share on other sites
[quote name='Alessandro' timestamp='1317847283' post='4869537']
[quote name='SiCrane' timestamp='1317846206' post='4869525']
You may want to consider using a std::vector instead.
[/quote]

They seem to be extremely complicated to use. Am I wrong?
[/quote]

yes, you're wrong.

[code]
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!");
}
[/code]

output:
[quote]
5
6
3
[/quote]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
the vector class is pretty cool if u have to change the size of memory at runtime.. consider the following example

[code]
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));
}

}
[/code]

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

[code]

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;

}

[/code]

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 :-)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this