views:

90

answers:

4

Hi, I am trying to do the following:

in main.cpp:

// Create an array of pointers to Block objects
Block *blk[64];
for (i=0; i<8; i++) {
  for (j=0; j<8; j++) {
    int x_low = i*80;
    int y_low = j*45;
    blk[j*8+i] = new Block(30, x_low+40.0f, y_low+7.5f, &b);
  }
}

And then I am trying to pass it to the graphics object I have created:

Graphics g(640, 480, &b, &p, blk[0], number_of_blocks);

the graphics constructor looks like:

Graphics::Graphics(int width, int height, Ball *b, Paddle *p, Block *blk, int number_of_blocks) {

if I look at what is contained in the array from the graphics object, only the first item exists and then all the other items are in hyperspace:

for (int i=0; i<64; i++) {
  printf("for block %d, %f, %f ", i, (_blk+(sizeof(_blk)*i))->_x_low, (_blk+(sizeof(_blk)*i))->_y_low);

  printf("blah %d\n", (_blk+(sizeof(_blk)*i)));

}

and if I look at the addresses, they are different (6956552 rather than 2280520 when I examine the addresses in the main class using:

printf(" blah %d\n", &blk[j*8*i]);

I am sure there must be something subtle I am doing wrong as its like I have copied the first item from the blk array to a new address when passed to the graphics object.

Does this make sense? Any ideas?

Cheers, Scott

A: 

It looks like:

Graphics::Graphics(int width, int height, Ball *b, Paddle *p, Block *blk, int number_of_blocks) {

is expecting an array of Blocks, not an array of pointers to Blocks. Passing the first element would probably work if you made_number_of blocks 1, but it can't work for anything else using your current data structure. If I were you, I would give up on using arrays and use std::vector instead - it will greatly simplify your code.

anon
+4  A: 

If you want to pass the whole array, the constructor should look like this:

Graphics::Graphics(int width, int height, Ball *b, Paddle *p,
    Block **blk, int number_of_blocks)

and you should pass the array like this:

Graphics g(640, 480, &b, &p, blk, number_of_blocks);
Péter Török
will this pass references to the array created in main or create a copy and pass a reference to that?
@user310153 Arrays are practically (almost) pointers, so `Block **blk` - a pointer to pointer to `Block` - as function parameter declaration, is equivalent to `Block *blk[]` - an array of pointers to `Block`. In other words, nothing is copied, the function accesses the contents of the original array.
Péter Török
A: 

The Graphics function is expecting a contiguous array of Block objects in memory and you are creating each new Block independently. Try

Block* blk = new Block[64];

then loop through and initialize each Block's values. This will only work if you can initialize the block objects in another way from using the constructor with arguments since new in this case can only call the default constructor. If the only way to initialize a Block is using the constructor with arguments, you'll have to do something else like passing Block** to the function.

Jason
A: 

Hello, Scott:

From what I can see, you are passing the first element of the array to the contructor, not the whole array. This is what you are doing:

#include <iostream>
#include <cstdlib>

void foo(int* item, const int length);

int main() {
  const int length = 10;
  int* array[length];
  for (int i = 0; i < length; ++i) {
    array[i] = new int(i + 100);
  }
  foo(array[0], length);
  return (EXIT_SUCCESS);
}

void foo(int* item, const int length) {
  for (int i = 0; i < length; ++i) {
    std::cout << item[i] << std::endl;
  }
}

I believe this is what you wanted to do:

#include <iostream>
#include <cstdlib>

void foo(int** array, const int length);

int main() {
  const int length = 10;
  int* array[length];
  for (int i = 0; i < length; ++i) {
    int* item = new int(i + 100);
    array[i] = item;
  }
  foo(array, length);
  return (EXIT_SUCCESS);
}

void foo(int** array, const int length) {
  for (int i = 0; i < length; ++i) {
    int* item = array[i];
    std::cout << *item << std::endl;
  }
}

Regards, Rafael.

Rafael Forte