gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > array pointer confusion

#167619 - intel3 - Fri Mar 20, 2009 4:54 pm

Hi

I am getting confused with the way how array is being used in c++ as a returned variable

Here is how I declared it:
Code:

Ship::Ship() {
        Laser * beam[10];
   for(int i = 0; i < 10; i++)
      beam[i] = new Laser();

}


I want to return the array and here is the function call:
Code:

Laser * Ship::getlasers(){
   //what should it be?

}


I tried returning beam and it didn't work. So I returned beam[0] and it worked. Is it suppose to be returning beam[0] and why.

Afterward returning the pointer how do I reference other index (for example) like so?

Code:


Laser * las = ship->getLasers();
las[5] = 3;



Here are some of the other question I have in mind about pointer and array,
what is the concept behind this and how to use it properly?
In this case why can't I just use char pointer instead seems like it is just pointing to a memory location?
When I declare

Code:

Laser * beam[10];
or
Laser *beam[] = new Laser[10];


Does a pointer to the array also means the pointer starting at index 0? And since it is a pointer does it mean that for example beam[3] is also an pointer and I can declare a new pointer like so:

Code:

laser * one = beam[3];


Thanks for your explaination

#167621 - elhobbs - Fri Mar 20, 2009 5:59 pm

an array of Laser pointers
Code:
Laser *beam[10];
for(i=0;i<10;i++) {
 beam[i] = new Laser();
}


an array of Lasers
Code:
Laser *beam = new Laser[10];


in the first case beam is a Laser**
in the second case beam is a Laser*

in the first case beam[i] is a Laser*. In the second case beam[i] is a Laser. so the first case uses beam[i]->x the send case would use [beam[i].x

the second case guarentees that the Laser objects are contiguous in memory. the first case does not.

#167623 - sajiimori - Fri Mar 20, 2009 6:55 pm

And best of all:
Code:
Laser beam[10];
Dynamic allocation is a solution to a problem; if you don't have a problem, don't use it.

#167636 - brave_orakio - Sat Mar 21, 2009 3:01 am

Code:


Laser * Ship::getlasers(){
   //what should it be?

}


If you return beam, you're returning a double pointer I think that's why it doesn't work. when you return beam[0], you only returning a pointer.
_________________
help me

#167639 - sgeos - Sat Mar 21, 2009 3:53 am

If I understand your problem, it should be "return beam;".
This is the test setup I used...
Code:
#include <iostream>
using namespace std;

typedef int Laser;

class Ship
{
        Laser beam[10];

public:
        Ship()
        {
                int i;
                for (i = 0; i < 10; i++)
                        beam[i] = i;
        }

        Laser *getlasers()
        {
                return beam;
        }
};

void printLaser(Laser *pLaser, int pSize)
{
        int i;
        for (i = 0; i < pSize; i++)
                cout << pLaser[i] << endl;
}

int main(int argc, char **argv)
{
        Ship myShip;
        printLaser(myShip.getlasers(),10);
        return 0;
}

#167645 - intel3 - Sat Mar 21, 2009 7:02 am

Thanks for the reply

The reason I am confused is because I came from Java background. The way how array is return from a function seems unclear to me.

In Java
Code:


Laser beam[] = new Laser[10];
beam[1] = new Laser();

And then I can return a beam[] object.

In c++, how to code it properly?
from what segeos suggestion, what I return is a pointer to the array.
The way how I would use this pointer to reference objects in array and its members are like,

Code:

Laser * las = beam[4];
las-> radius = 6;


Is this correct?

#167656 - sgeos - Sat Mar 21, 2009 1:02 pm

Specifically, what do you want to do?
Quote:
// allocate your lasers somewhere
Laser beam[10];

// somehow get ahold of your list of lasers
// This is the same as:
// Laser *las = &(beam[0]);
Laser *las = beam;

// you probably want to explicit set one laser from the list
// instead of letting it default the the first one in the array,
// unless I misunderstand your intentions
las[0].radius = 6;

#167726 - intel3 - Mon Mar 23, 2009 10:21 pm

Basically I want to get an array of laser object.

After doing some research I realize that a pointer can work like an array
so basically

Code:

Laser * myLaser(){
Laser list[30];

return list;
}

Laser newlist = myLaser();

It works fine

What I don't understand is once I create a list like so why won't the following code work?

Code:


Laser list[30];
list[0] = new Laser();

#167727 - Miked0801 - Mon Mar 23, 2009 10:47 pm

Because

Laser list[30];

Immediately creates 30 lasers on the stack using Laser's default constructor.

List[0] = new Laser() is attempting to assign a laser pointer (new() returns a pointer) over an existing laser object (non pointer).

If you want to use new, then use

Laser *list[30]; // Create 30 laser object pointers
list[0] = new Laser(); // Create a laser at the first element.

#167730 - sajiimori - Tue Mar 24, 2009 12:20 am

Right, C++ is not Java. If you say:
Code:
Laser beam;
you're creating a real Laser object right there, not a reference to a Laser object.

Java doesn't have the concept of by-value objects; the above declaration would only create a null reference in Java.

By-value objects are destroyed immediately when they goes out of scope, unlike Java which garbage collects objects at some unspecified time in the future.

Hold objects by value whenever possible. Using 'new' is much slower, and the semantics are more complicated because you have to think a lot more about object lifetimes, which are not automatically managed for you in C++.

#167794 - intel3 - Fri Mar 27, 2009 7:45 pm

Thx for the reply
Now I totally understand the problem was

Thanks again!