1

i want to create a function(in a project) that returns an array. I m not quite sure about how can i do that.

int worker::*codebook(UnitType type){
    int  code[12]; 
    if  (type == UnitTypes::center){
        int temp[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
        code=temp;
    }
    return code;
}

where worker is the class and unitType an enumeration. I define the function in the header file as follows:

int *codebook(UnitType type);

My problems is the following:

cannot convert from 'int' to 'int Worker::*

Any idea about this??

1

8 Answers 8

5

The first problem in the code is the syntax. The signature should be:

 int* worker::codebook(UnitType type)

Then, it is assigning to an array:

code=temp;

This is just not allowed by the language.

Finally, it's returning a pointer to a local variable:

return code;

The array will no longer exist when the function returns, so any attempt to use it from outside will result in undefined behaviour.

Now, to answer the main question, how to properly return an array from a function?

One option is to use std::vector:

 std::vector<int> worker::codebook(UnitType type) {
     std::vector<int> code(12); // vector with 12 zeros
     if  (type == UnitTypes::center){
         code[11] = 1;
     }
     return code;
 } 

Another is to use std::array:

 std::array<int, 12> worker::codebook(UnitType type) {
     std::array<int, 12> code = {{}};
     if  (type == UnitTypes::center){
         code[11] = 1;
     }
     return code;
 }
Sign up to request clarification or add additional context in comments.

Comments

2

You can't return a local array, it will go out of scope when the function exits.

It's better to use an actual data structure, such as std::vector perhaps. IF you want to use bare C-level arrays, you must allocate the memory dynamically and add a way for the function to express the length of the array, perhaps by also taking a size_t& length argument.

1 Comment

So my two alternatives are either to use vector or define in the function the size of the array?
2

You have error in function prototype:

int worker::*codebook(UnitType type){

should be

int* worker::codebook(UnitType type){

And it is not correct since code is allocated on stack and destructed when it goes out of scope.

You should allocate this code array on the heap. Body of this function could then look like this:

int* code = new int[12]; 

if  (type == UnitTypes::center){

    int temp[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
    memcpy(code, temp, 12*sizeof(int));
}
return code;

but then the caller should call delete[] when it finishes with this array:

int* array = codebook(type);
delete[] array;

Ugly memory management is connected with this kind of solution. Since you are using C++ you should use some object that will make it easier (for example std::vector).

Hope this helps.

Comments

1

The signature should be:

int* worker::codebook(UnitType type)

Note that you're encountering undefined behavior.

int  code[12]; 

is a local variable, which will get destroyed when the function exits. And you're returning it. Never do this. You should allocate the array dynamically, via new.

Comments

0

Your pointer declaration is in the wrong place.

 int* worker::codebook(UnitType type)

However, be warned that you are creating your array on the stack, which will get destroyed when your function exits. You need to create it on the heap with new, and remember to delete it when you're done.

Comments

0

You don't want to do this because here the array memory is allocated inside the function and will be gone when the function finishes. Allocate the memory/array ouside the function and pass the array in as a pointer, in one of the parameters.

Comments

0

You can bury the array inside a struct and then return the struct by value - the classic C style:

struct my_array
{
int code[12];
};

my_array worker::codebook(UnitType type){
    my_array arr = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; 
    if  (type == UnitTypes::center){
       arr.code[11] = 1;
    }
    return arr;
}

Comments

0

This should be int* worker::codebook(UnitType type). The * goes with the int, to make it a pointer-to-int (int*).

However, you really don't want to return code like that - when the function exits, code will point to garbage (because it is allocated on the stack).

2 Comments

Well... To be picky: code does NOT point to garbage just because it's allocated on the stack. If it was so the stack would be useless. It's because code is a locally allocated array and it goes out of scope when the function returns.
@SaniHuttunen That's why I said "when the function exits". Typo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.