C++ pointer to struct [edited]

Started by
10 comments, last by MarkusPfundstein 12 years, 6 months ago
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.x=...
myObject.y=...
myObject.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
Advertisement
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'
You may want to consider using a std::vector instead.
You also don't need the 'struct' keyword when you're declaring instances of structures in C++.

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

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


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

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


Right, thanks!

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.


[quote name='SiCrane' timestamp='1317846206' post='4869525']
You may want to consider using a std::vector instead.

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.

[quote name='SiCrane' timestamp='1317846206' post='4869525']
You may want to consider using a std::vector instead.


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

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);
if(vec == 17)
printf("I didn't put this number in the vector!");
}


output:

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.
"What? It disintegrated. By definition, it cannot be fixed." - Gru - Dispicable me

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

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

This topic is closed to new replies.

Advertisement