-3

First of all, I am using C++ and I am a newbie on it. I have an interface with exposes a method unsigned char * readRequest(). All of my classes inherit this interface and implement the unsigned char * readRequest() in its way (dealing with float, string, int, double, char), but return unsigned char *. Below are some of my implementations of readRequest() that converts float to unsigned char *. Edited: I am returning a pointer to a local variable and this is an undefined behavior. How do I return the real value in C++?

unsigned char * readRequest() {
    float preCent = distanceCentimeters();
    std::cout << preCent << std::endl; // print correct result
    unsigned char buf[4];
    memcpy(buf, &preCent, sizeof(preCent));

    float result;
    memcpy(&result, buf, sizeof(result));
    std::cout << result << std::endl; // print correct result

    return buf;
}

int main(void) {
    unsigned char *readRequestArray;
    readRequestArray = (unsigned char*) readRequest();
    float converted_back;
    memcpy(&converted_back, readRequestArray, sizeof converted_back);
    std::cout << converted_back; // ERROR due to return a pointer to local variable
}

Output and error:

24.2882
jN�A 
Segmentation fault
Felipe
  • 7,013
  • 8
  • 44
  • 102
  • You should try stepping through your code, line by line, in your debugger. Pay attention to variable values and control flow along the way. When it crashes, also look at the call stack to see how you got there. – Jesper Juhl Oct 09 '18 at 14:18
  • Returning a raw pointer from a function is almost always a bad idea. – Slava Oct 09 '18 at 14:19
  • You should be using a `std::unique_ptr` or `std::vector` (or `std::byte` in place of `unsigned char`) if you want to return data from the function. – NathanOliver Oct 09 '18 at 14:22
  • I was using `char _value[];` and returning `char *`. I am concerning about the time execution. If `std::unique_ptr` does not increase the time execution do you have any example of using it? – Felipe Oct 09 '18 at 14:33
  • 1
    It doesn't increase the execution time, it just protects against memory leaks. No cost. – Matthieu Brucher Oct 10 '18 at 08:56
  • @NathanOliver and @Matthieu Brucher. I tested using `std::vector` and my buffer increased 10 times to return the value. So, it is why I am using `unsigned char array`. However, I edited the question to return the real value of the local variable, not the pointer. If you have some idea to help me I will appreciate that. I guess this is the main problem in my function and it is why I am getting `Segmentation fault`. Thanks – Felipe Oct 10 '18 at 09:03

1 Answers1

2

You are returning a pointer to local variables. This doesn't work and will be raised by your compiler in the warnings.

Allocated data and return a unique pointer, or just simply use a string, as you are passing memory management to the caller. You are in C++, use C++ style code, not C (printf and others).

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
  • https://stackoverflow.com/users/2266772/matthieu-brucher - So what should I return? I have another class that implements `readRequest()` function and returns `unsigned char*`. This function is converting string to an `unsigned char array` and it is working. I used this post https://stackoverflow.com/a/52737355/2096986 – Felipe Oct 10 '18 at 10:34
  • A `std::unique_ptr`. That's the contract that modern c++ gives to enforce proper memory management. – Matthieu Brucher Oct 10 '18 at 10:40
  • And both methods should return this. You cannot have a child class that returns a managed array and another an unmanaged array. It's bad design. Either you return a naked pointer that tells the user that he doesn't have to free the memory, or you return a unique pointer. There must not be any confusion on who is responsible for the memory. – Matthieu Brucher Oct 10 '18 at 10:43
  • I have never worked with unique_ptr. Could you please give a help on https://stackoverflow.com/questions/52739477/how-to-return-copy-values-of-an-unique-ptrunsigned-char ? – Felipe Oct 10 '18 at 11:44