Return local variables of the function

2

I have two methods:

// Apenas define um vetor de 4 posições e retorna ele
int *verticesFromFace(int v1, int v2, int v3, int v4) {
  int vertices[4] = {v1, v2, v3, v4};
  return &(vertices);
}

// pega um cubo e uma face dele, retorna um vetor de vertices daquela face
Vertice *getPointsFromFace(Cube c, Face f) {
  Vertice points[4];
  int i;
  for (i = 0; i < FACE_VERTICES; i++)
    points[i] = c.vertices[f.vertices[i] - 1];

  return &(points);
}

Example usage:

// pega uma face do cubo, armazena nela o vetor de vértices (usando aquele método) e também as cores daquela face (3 últimos parâmetros)
createFace(c.faces[0], verticesFromFace(1, 2, 3, 4), 1, 0, 0);


// Um vetor de vertices, quando chamo o metodo com o cubo e uma face do cubo
Vertice *points = getPointsFromFace(c, c.faces[i]);

GCC returns the error:

  

warning: function returns address of local variable   [-Wreturn-local-addr] return & (points [0]);

Although it is a warning , the code does not work because the local variable is not returned. How can I fix this?

Q.: This &() in return was an attempt I made when I saw a post in Stack Overflow English

    
asked by anonymous 10.11.2016 / 17:39

1 answer

2

You can not do this. The array is allocated in the stack , when the function terminates it can no longer be accessed.

The worst thing is that in C you can even in some circumstances you do not have control and this can be tragic because most programmers think testing and running is right. All language has these things, but C has much more. C allows you to do a lot of wrong and look right. Luckily in that case it went wrong before it was too late.

Understand What are and where are the stack and heap? .

To keep the value "alive" when exiting the function, you need to allocate the object in heap . This is done with malloc() . And then you have to release it with free() . Very experienced programmer slides and lets memory leak. Or you can try to release what has already been released, which can be tragic. Beginner programmer does not even bother about it because it "works" without it.

All this code is wrong. It would look something like this:

int *verticesFromFace(int v1, int v2, int v3, int v4) {
    int *vertices = malloc(4 * sizeof(int));
    //inicaliza os elementos aqui
    return vertices;
}

Asymmetric allocation

In fact, the ideal is neither to do this, it is to allocate memory where you need that data, so it is easier to follow its flow and avoid forgetting to call free() . Not doing so is practically asking to create confusion, it becomes asymmetrical.

Each function must be "owner" of its object completely ( malloc() and free() must come in pairs, it must be declared, allocated and released in my place.

  • Of course you have a case to allocate on the spot, but you can only use this if you know what you are doing very well, have a very strong motive and know that you have to be careful to manage memory.

    Warnings

    Warnings are errors that do not prevent compilation, that's all. The fact that it does not look like an error does not mean it is not.

        
  • 10.11.2016 / 17:47