SIGSEGV Codeblocks

1

Good afternoon! I have to finish a work of a game for the university and I have the typical problem of "sometimes it works". I tell you above: it's a game where you take a character, if the enemies touch you, the game ends and you take points that appear randomly.

struct EstrellasRep{
    Estrella * s;
    int maximo, n;
    };

Estrellas EstrellasCrea (int maximo){
    Estrellas e = malloc (sizeof(struct EstrellasRep));
    e->n=0;
    e->maximo=maximo;
    return e;
    }

void EstrellasLibera (Estrellas e){
    for (int i=0; i<e->n; i++) EstrellaLibera(e->s[i]);
    free(e->s);
    free(e);
}
void EstrellasDibuja (Estrellas e){
    for (int i=0; i<e->n; i++) EstrellaDibuja(e->s[i]);
}
int EstrellasColisiona (Estrellas e, int x, int y, int w, int h){
    int i=0;
    int colisiona=0;
    while(i<e->n){
        if (colision(EstrellaGetX(e->s[i]),EstrellaGetY(e->s[i]),EstrellaGetW(e->s[i]), EstrellaGetH(e->s[i]),x,y,w,h))
    {
        EstrellaLibera(e->s[i]);
        e->s[i]=e->s[e->n-1];
        e->n--;
        colisiona++;
    }
    else i++;
}
return colisiona;
}

void EstrellasInserta (Estrellas e, Imagen i, int x, int y, int w, int h)
{
     if (e->n<e->maximo) {
        e->s[e->n] = EstrellaCrea(i,x,y,w,h);
        e->n++;
    }
}

the sigsev comes to me on the line e-> s [e-> n] = EstrellaCrea (i, x, y, w, h);

And I say, sometimes it goes xD Greetings

Edit:

Stars is a structure that leads to the set of Star, which are the points that must be collected.     struct EstrellaRep {        Image i;        int x, y, w, h, r, cx, cy;        };

Estrella EstrellaCrea(Imagen i, int x, int y, int w, int h) { 
    Estrella e = malloc(sizeof(struct EstrellaRep)); 
    e->i=i; 
    e->x=x; 
    e->y=y; 
    e->w=w; 
    e->h=h; 
    return e; 
 } 

And Estrella is a class with its drawing and coordinates.

    
asked by Esther Lynne 28.06.2017 в 19:46
source

1 answer

0

The first thing that occurs to me is that by making space in the memory with the EstrellasCrea I suppose you want to make an array of pointers to Estrella, (UM xD) just as you are doing, you have made space for ONE pointer and an int maximum and another, n. Then when you release the "treat" as if you had made another malloc for the array, so do it:

Estrellas EstrellasCrea (int máximo) {     Stars e = malloc (sizeof (struct StarsRep));      * e-> s = malloc (sizeof (Star) * maximum); *     e-> n = 0;     e-> maximum = maximum;     return e; }

What I have done has been to make room for MORE than one star, in fact, at most "maximum" star, I hope it is this. Fdo Israel;)

Edit: You also say that the Star structure is struct EstrellaRep {Image i; int x, y, w, h, r, cx, cy; }; Are you treating the star as a circle or as a square? I guess as a square because when you create the star you simply call it with image i int x int and int w int h ... clarify this or pass a picture of the structure and function of the EstrellaCrea () Thank you I'll try to help you later.

    
answered by 29.06.2017 в 08:10