Se connecter avec
S'enregistrer | Connectez-vous

Pb d allocation memoire en C

Dernière réponse : dans Programmation

Hi,

Je suis debutant en c mais la, g beau lire le man malloc de linus ds ts les sens je comprends po pourkoi ca marche po. Le code n est pas encore fini, ms ca c po bien grave.
Evidement ca doit po vous paraitre bien complique ms bon je suis en anne erasmus et du coup g absolument occune aide de la part du staff de mon universite ( enfin si ms ... ils parlent trop vite les rascals ).

Peut etre qu il va mieux allouer de la memoire pour chaque case de la sturcture, je sais po :na: 

Je vous met mon code, en esperant une reponse.....

code

#include <stdio.h>
#include <stdlib.h>

#define lignes 8
#define colums 7
#define possibilites 10000

struct tree {
int pere;
char tble[lignes][colums];
};

void affiche_table(char tbl[lignes][colums]);

int compare(char tbl[lignes][colums],char tbl2[lignes][colums]);

int testsol(char tbl[lignes][colums],int x,int y);

int movement(struct tree arbre[possibilites],int x,int y);

void copy_tbl(char tbl[lignes][colums],char tbl2[lignes][colums]);



int main(void)
{
int i,j,x,y,k=0;
char tbl[lignes][colums];
struct tree arbre[possibilites];

arbre = malloc(possibilites*lignes*colums*sizeof(char));
if(arbre[0].tble == NULL)
{
printf("\nAllocation failed>\n");
return(1);
}

for(i=0;i<lignes/2;i++)
for(j=0;j<colums;j++)
arbre[0].tble[j] = '.';
for(i=lignes/2;i<lignes;i++)
for(j=0;j<colums;j++)
arbre[0].tble[j] = 1;

arbre[0].pere = 0;
copy_tbl(tbl,arbre[0].tble);
affiche_table(arbre[0].tble);
affiche_table(tbl);

printf("Type the coordinate of an empty square to start the game\ny ? (Between 0 and 3)\n");
scanf("%d",&y);
if( y<0 || y>3 )
{
printf("\nYou have enter something unexpected\n\n");
k = 1;
}
else
{
printf("x ? (Between 0 and 6)\n");
scanf("%d",&x);
if( x<0 || x>6 )
{
k=1;
printf("\nYou have enter something unexpected\n\n");
}
}

// copy_tbl(tbl,arbre[0].tble);

// movement(arbre,x,y);

return 0;
}


void affiche_table(char tbl[lignes][colums])
{

int i,j;

system("clear");

for(i=0;i<lignes;i++)
{
for(j=0;j<colums;j++)
printf("%c",tbl[j]);
printf("\n");
}
printf("\n");
}


int compare(char tbl[lignes][colums],char tbl2[lignes][colums])
{
int i,j;

for( i=0 ; i<=lignes ; i++ )
for( j=0 ; j<=colums ; j++ )
if( tbl[j] != tbl2[j] )
return 1;

return 0;
}


int testsol(char tbl[lignes][colums],int x,int y)
{
if( tbl[y][x] == 1)
return 1;
return 0;
}


void copy_tbl(char tbl[lignes][colums],char tbl2[lignes][colums])
{
int i,j;
for( i=0 ; i<=lignes ; i++ )
for( j=0 ; j<=colums ; j++ )
tbl[j] = tbl2[j];

}


int movement(struct tree arbre[possibilites],int x,int y)
{
int i,j,l=0,g=0;
char tbl[lignes][colums];

while(1)
{
for(i=0;i<=lignes;i++)
for(j=0;j<=colums;j++)
{
affiche_table(tbl);
if( tbl[j] == 1)
{
if(i != lignes-1 || i != lignes-2 )
if( (tbl[i+1][j] == 1) && (tbl[i+2][j] == '.') )
{
copy_tbl(tbl,arbre[l].tble);
g++;
tbl[j] == '.';
tbl[i+1][j] == '.';
tbl[i+2][j] == 1;
copy_tbl(arbre.tble,tbl);
arbre.pere = l;
if( testsol(tbl,x,y) == 1 )
break;
}
if(i != 1 || i != 0 )
if( (tbl[i-1][j] == 1) && (tbl[i-2][j] == '.') )
{
copy_tbl(tbl,arbre[l].tble);
g++;
tbl[j] == '.';
tbl[i-1][j] == '.';
tbl[i-2][j] == 1;
copy_tbl(arbre.tble,tbl);
arbre.pere = l;
if( testsol(tbl,x,y) == 1 )
break;
}
if(j != colums-1 || j != colums-2 )
if( (tbl[j+1] == 1) && (tbl[j+2] == '.') )
{
copy_tbl(tbl,arbre[l].tble);
g++;
tbl[j] == '.';
tbl[j+1] == '.';
tbl[j+2] == 1;
copy_tbl(arbre.tble,tbl);
arbre.pere = l;
if( testsol(tbl,x,y) == 1 )
break;
}
if(j != 0 || j != 1 )
if( (tbl[j-1] == 1) && (tbl[j-2] == '.') )
{
copy_tbl(tbl,arbre[l].tble);
g++;
tbl[j] == '.';
tbl[j-1] == '.';
tbl[j-2] == 1;
copy_tbl(arbre.tble,tbl);
arbre.pere = l;
if( testsol(tbl,x,y) == 1 )
break;
}

}
}
}
l++;
}

code

Autres pages sur : allocation memoire

Lassé par la pub ? Créez un compte

et deja pourquoi tu fait pas un malloc sur la structure au lieu de supposer la taille qu'elle va faire?
Tu eviterai déjà les problemes d'alignement de bit qui pourrai te gener, et en plus c'est plus simple

Mais en fait
  1. struct tree arbre[possibilites];

t'alloue déjà la mémoire, pas besoin d'un malloc

T'en aurai besoin d'un si tu travaillais avec un pointeur sur structure
  1. struct tree *arbre;

Lassé par la pub ? Créez un compte