3

This C++ code worked for me so far:

Main.cpp:

unsigned __int16** image_data; image_data = Grabber->get_image_data(1); 

interface.cpp:

unsigned __int16** Grabber::get_image_data(int image_num) { unsigned __int16 **pixel_values = 0; pixel_values = new unsigned __int16*[height]; for (int h = 0; h < height; h++) { pixel_values[h] = new unsigned __int16[width]; for (int w = 0; w < width; w++) { pixel_values[h][w] = ...; } } return pixel_values; } 

But now I would like to pass the array as pointer to the function. I tried it like in following code, but it doesnt work anymore.

Main.cpp:

unsigned __int16** image_data; Grabber->get_image_data(1, &image_data); 

Interface.cpp:

int Grabber::get_image_data(int image_num, unsigned __int16*** image_data) { *image_data = new unsigned __int16*[height]; for (int h = 0; h < height; h++) { *image_data[h] = new unsigned __int16[width]; for (int w = 0; w < width; w++) { *image_data[h][w] = ...; } } return 0; } 

Are there any errors in reasoning by me?

5
  • #define dosen't work for me, you are getting compiler error? Commented Sep 26, 2016 at 9:24
  • Sorry, no compiler error Commented Sep 26, 2016 at 9:25
  • I've edited out the dots; if they contain important code, you should include them. If not, it doesn't matter and you can just leave them out. Commented Sep 26, 2016 at 9:26
  • 1
    You shouldn't store the image data like that; it's by far easier and better to utilize a flat buffer (that is, __int16*), and manually calculate the index. Better still, use std::vector<int16_t>, which utilizes the standard typedef and automatically manages its memory. Commented Sep 26, 2016 at 9:28
  • A better solution would be to pass by reference Commented Sep 26, 2016 at 9:37

1 Answer 1

6

*image_data[h] means *(image_data[h]) (that is, image_data[h][0]), not (*image_data)[h] (or image_data[0][h]).
The latter is what you want.

It's easier to get it right if you introduce a local variable:

int ..::get_image_data(int image_num, unsigned __int16*** image_data) { ... unsigned __int16** data = new unsigned __int16*[height]; *image_data = data; for (int h = 0; h < height; h++) { data[h] = new unsigned __int16[width]; for (int w = 0; w < width; w++) { data[h][w] = ...; } } return 0; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.