Why are contents of 2d array in C persisting through function calls?

Viewed 73

I have the following file, the parts I am asking about are float depth_buffer[1920][1076]; and the section toward the bottom labeled "fixes issue."

void draw_screen(cairo_t *cr, struct triangle_list triangles) {
    struct point_3d temp_points[3];
    struct point_3d projected_points[3];
    float shades[3] = { 1.0, 1.0, 1.0 };
    float depth_buffer[1920][1076];

    cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
    cairo_paint(cr);

    cairo_rectangle(cr, 0.0, 0.0, 30.0, 30.0);

    float d = 20;
    float v_w = 160;
    float v_h = 90;

    projected_points[0] = project_vertex(triangles.verteces[0], d, v_w, v_h);
    projected_points[1] = project_vertex(triangles.verteces[1], d, v_w, v_h);
    projected_points[2] = project_vertex(triangles.verteces[2], d, v_w, v_h);

    int current_triangle = 0;
    float color[3];

    for (int i = 0; i < triangles.num_triangles * 3; i += 3) {
        for (int j = 0; j < 3; j++) {
            projected_points[j] = project_vertex(triangles.verteces[i + j], d, v_w, v_h);
            shades[j] = triangles.shades[i + j];
        }

        color[0] = triangles.colors[current_triangle].r;
        color[1] = triangles.colors[current_triangle].g;
        color[2] = triangles.colors[current_triangle].b;

        draw_filled_triangle(cr, projected_points, shades, color, depth_buffer);

        current_triangle++;
    }
    
    // Fixes issue
    for (int i = 0; i < 1920; i++) {
        for (int j = 0; j < 1076; j++) {
            depth_buffer[i][j] = 0.0;
        }
    }
}

When this function is called, all of the values in depth_buffer should be zero. However, when I used a debugger I discovered the values of depth_buffer were not zero, and appeared to be unchained from the last time it was called.

When added the loop to manually set all the values to zero, the issue went away.

Why isn't the memory for depth_buffer being cleared at the end of the function? Should I clear it manually, if so, how?

1 Answers

From C11 Standard#6.7.9p10 [emphasis added]

10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. ...

The way you are trying to ensure the depth_buffer array should be cleared in subsequent call of draw_screen() function, by assigning 0.0 to all elements of depth_buffer array just before returning from draw_screen() function, is not going to help you out because depth_buffer is a automatic variable and objects with automatic storage duration live for the lifetime of the block in which they declared. The depth_buffer array declared in draw_screen() function block and it will gets destroyed upon exiting of draw_screen() function.

Instead, you should initialise the depth_buffer array elements with 0 before using it. To initialise all elements of depth_buffer array with 0, you can do:

float depth_buffer[1920][1076] = {{0}};

Check more about array initialisation in C here.

The size of depth_buffer array is roughly around 8 MB. Allocating such a large array on the stack is bad practice and should be avoided. Instead, allocate memory dynamically to it. You may want to explore the C memory management library functions.

Related