1

Hello stackoverflow fellow members?

Struct Declaration in class A

struct PointSprite 
{
    GLfloat x;
    GLfloat y;
    GLfloat size;
    Color4f color;
} ParticleSystems[MAXIMUM_PARTICLES_ON_SCREEN];
// I generally put some stuffs in ParticleSystem array. 
// for ex) struct PointSprite *ps = &ParticleSystems[index];
// and it works well on the class A, but I want to get class B to access this array.

My question is, how am I suppose be return the array of 'ParticlelSystems' array so that other class can access to it? I have tried below code to return the pointer, but compiler gives me a warning.

- (struct ParticleSystems *) commitParticles
{
    struct ParticleSystems *ptr = &ParticleSystems; // it said, assigning incompatible pointer type
    
    return ptr;
}

Or should I need to allocate the 'ParticleSystems' array? Please help ! Thanks

1
  • 3
    The correct answer is different in C, C++, and objective-c. Please pick one language for your question. Commented Aug 5, 2011 at 16:49

3 Answers 3

2

If you are creating the array inside the function then you should dynamically allocate it using new and then return a pointer to it.

You cannot return arrays from a function, you will have to return a pointer to it.

Sample Code:

ParticleSystems* doSomethingInteresting()
{
    ParticleSystems *ptr = new ParticleSystems[MAXIMUM_PARTICLES_ON_SCREEN];

    //do the processing

    return ptr;

}

The caller takes the ownership of the returned dynamically allocated array and needs to deallocate it to avoid memory leaks:

delete []ptr;
Sign up to request clarification or add additional context in comments.

4 Comments

While you're technically correct, in C++ I'd use a std::vector instead of a regular array. If you want to avoid the overhead of copying the vector data around, instead of using a plain pointer I recommend using a boost::shared_ptr, i.e. shared_ptr<vector<PointSprite>> ptr(new vector<PointSprite>>.
@datenwolf: Absolutely, One really needs to use RAII and almost never use raw pointers in C++, but again if the OP already knows the number of elements of his array, then it would make sense to use a array rather than a std::vector to avoid the overhead of extensive allocations a vector would make.
You can initialize a std::vector with a size, then stay within the bounds of that size. That way no reallocation will occcur: typedef std::vector<PointSprite> PointSprite_vector; boost::shared_ptr<PointSprite_vector> ptr(new PointSprite_vector(size)).
Or you can use vector::reserve to pre-allocate the memory; then you can do all the push-backs you want up to that pre-allocated limit. I do this a lot to prevent extra allocations, while not having to run the default constructor alot.
2

You can either return it, after allocating one, or you can fill one passed to you by the user. The latter leaves the responsibility to the user to provide a ParticleSystem to the method which receives the data. It can be a local array, or a malloced one.

- (void) commitParticles: (ParticleSystems *) sprites
{
    // set members of the structs here
}

I prefer this kind of passing to returning a malloced array. Your mileage may vary.

Comments

0

You're getting the assigning incompatible pointer type compiler warning because your ptr declaration should be of type PointSprite *, not ParticleSystems *

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.