L. Sanna
L. Sanna

Reputation: 6552

Values of an array changing randomly during execution

I'm trying to make a little programme in C using SDL, displaying a robot moving on a grid. This grid is represented by a txt file of 0s and 1s.

Here is the fonction creating an array from the txt file, which works.

// create a map(array) from a text file
int (*newMap())[SIZE_HEIGHT][SIZE_WIDTH]
{
static const char filename[] = "input.txt";  /* the name of a file to open */
FILE *file = fopen(filename, "r");  /* try to open the file */
int map[SIZE_HEIGHT][SIZE_WIDTH];

char line[BUFSIZ]; /* space to read a line into */
int k = 0;
while ( fgets(line, sizeof line, file)!=NULL && k<SIZE_HEIGHT) /* read each line */
{
    int i;
    char *token = line; /* point to the beginning of the line */

    for ( i = 0; i<SIZE_WIDTH; i++ )
    {
        map[k][i]=((int)*token)-48;
        token+=sizeof(char);
        printf("map[%d][%d]=%d\n", (int)k,(int)i,map[k][i]);
    }
    puts("----\n");
    k++;
}
fclose(file);

int (*p)[SIZE_HEIGHT][SIZE_WIDTH];
p=&map;
return p;

}

Then I try to put the grid on the sreen (not the whole fonction):

#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <time.h>
#include <string.h>
#include "Parameters.h"
#include "simulation.h"
#include "editor.h"

void simulate(SDL_Surface *ecran)
{
SDL_Surface *carreVert = SDL_LoadBMP("carreVert.bmp");
SDL_Surface *carreRouge = SDL_LoadBMP("carreRouge.bmp");
SDL_Surface *robot = SDL_LoadBMP("robotRouge.bmp");

SDL_SetColorKey(robot, SDL_SRCCOLORKEY, SDL_MapRGB(robot->format, 255, 255, 255));

int (*map)[SIZE_HEIGHT][SIZE_WIDTH];
 map=newMap();
SDL_Rect positionFond;

int i;
int j;

for(j=0; j<SIZE_HEIGHT; j++)
{
    for(i=0; i<SIZE_WIDTH; i++)

    {


        positionFond.x = 100*i;
        positionFond.y = 100*j;
        if((*map)[j][i]+1)
        {

            SDL_BlitSurface(carreVert, NULL, ecran, &positionFond);
        }else
        {


            SDL_BlitSurface(carreRouge, NULL, ecran, &positionFond);
        }

    }

}

And then something strange happens: when I observe the array *map with the debugger, I see that the values are changing when I go through the test. So the grid does appear, but not with the right pattern. Why does that happen?

Edit:no error an compiler.

Edit: Any guess of what might do that would be gladly accepted.

Upvotes: 1

Views: 1382

Answers (2)

ninjalj
ninjalj

Reputation: 43688

You are returning a pointer to an object with automatic storage duration (an array that gets "deallocated" on return from the function where it is declared), so you end up seeing random garbage.

Upvotes: 1

pmg
pmg

Reputation: 108978

The array map is local to the function. It ceases to exist when the function finishes. You pass its address to the caller, but when the caller tries to use it ... BANG!

Fast solution: make the array map a static one: make sure you never call newMap more than once per run.

Other solutions:

  • move the creation of the array outwards and pass the address around
  • use malloc and friends to manage the array

Upvotes: 2

Related Questions