开发者

return 2d array from function in C++

I have a function declared like so:

unsigned char** Classifier::classify(){
      //...
    unsigned char **chars = new unsigned char *[H];
for(int i = 0; i < H; i++)
    chars[i] = new unsigned char[W*3];

//...

return &chars;
//note: when this is "return chars;" I get the following:  cannot convert ‘unsigned char*’ to ‘unsigned char**’ in return

This i开发者_如何学Cs giving me the warning:

Classifier.cpp: In member function ‘unsigned char** Classifier::classify()’:
Classifier.cpp:124: warning: address of local variable ‘chars’ returned

Is this ok to ignore? Basically, my question is how do you return a reference to an array that is defined in the function?

I want to be able to do

unsigned char** someData = classify();


Just return the array, not its address:

return chars;

&chars is a pointer to a pointer to a pointer, but chars is a pointer to a pointer (what you want). Also note that chars is not an array. Pointers and arrays are not the same thing, although they are often confused.


This is never okay to ignore. You're returning the address of a local variable. That address will become invalid when you leave classify()'s stack frame, before the caller has a chance to use it.

You only need to return the value of that variable instead:

return chars;


@Adam Rosenfield has got the correct answer and so have some others, (remove that ampersand) but as food for thought, a nice way to do this is to use a std::vector (of std::vectors) and pass it into the function as a reference parameter.

#include <vector>

void Classifier::classify(std::vector<std::vector<unsigned char>> & chars)
{
      //construct a vector of W*3 integers with value 0
      //NB ( this gets destroyed when it goes out of scope )
      std::vector<unsigned char> v(W*3,0);

      //push a copy of this vector to the one you passed in - H times.
      for(int i = 0; i < H; i++)
         chars.push_back(v);
}

chars is populated with the stuff you want and when it comes to deleting the vector, you don't have to worry about how to call the correct delete[] syntax that you would with those two calls to new in your 2D array.

You can still reference items in this vector as you would with your 2D array e.g. chars[5][2] or whatever.

although I can see you want to be able to go:

 unsigned char** someData = classify();

So if you wanted to use vectors, you'd have to declare someData as follows:

 std::vector<std::vector<unsigned char>> someData;

and to make that clearer perhaps:

typedef std::vector<std::vector<unsigned char>> vector2D;
vector2D someData;
classify(someData);
...


  1. If an array defined in function and if you want to use it outside the function - you should describe it (array) as static or declare an array outside the function and pass it as parameter.

  2. Use "return chars;" only;


No, it's not okay to ignore that warning. The value you're returning is the address of chars on the stack, not the thing it points to. You want to return just chars.


Others have given teh answer; but as a general observation I would recommend you look at the STL. You've tagged the question C and C++, so I'm assuming you're in a C++ environment and the STL is available. You can then use typedefs to define vectors in a readable form , and even vectors of vectors (ie. 2d arrays). You can then return a pointer or reference (as appropriate) to your vector of vectors.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜