return 2d array from function in C++

喜你入骨 提交于 2020-01-03 17:29:29

问题


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 is 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();

回答1:


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;



回答2:


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.




回答3:


@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);
...



回答4:


  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;




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/4650911/return-2d-array-from-function-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!