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();
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With