Probleme C++ malloc

Bonjour,
je veux creer un tableau 3D de flottant de facon dynamique seulement j’ai une erreur a la compilation , je ne sais pas comment la regler.

float ***matrice;
matrice= malloc(256*sizeof(float));
for(int i=0 ; i<256 ; i++)
	{
	matrice[i]=malloc(256*sizeof(float));
	for(j=0;j<95;j++)
		{
		matrice[i][j]=malloc(95*sizeof(float));
		}
	}

l’erreur
cannot convert from void* to float***

Merci d’avance pour vos réponse.

Alors il faut que tu cast tes malloc pour avec du (float***) (float**) (float*), mais tu auras que un tableau à 2 dimentions là.


int main(void) {
	
	unsigned int x = 256,y = 256,z =95, i, j, k;
	float ****matrice = (float****)malloc( x * sizeof(float***) );

	for( i = 0; i < x; ++i ) {
		matrice[i] = (float***)malloc( y * sizeof(float**) );
		for( j = 0; j < y; ++j ) {
			matrice[i][j] = (float**)malloc( z * sizeof(float*) );
			for( k = 0; k < z; ++k ) {
				matrice[i][j][k] = (float*) malloc( sizeof(float) );
			}	      
		}
	}

	return 0;
}

Edité le 14/08/2009 à 20:20

merci, les code se compile.
en revanche, il y a trois dimensions, non? la première, elle est calculé avec le premier malloc, la deuxièm avec le deuxième et la troisième avec le trosième malloc, non? est ce que je me trompe?

J’ai rien dit.


int main(void) {
	
	unsigned int x = 256,y = 256,z =95, i, j, k;
	float ***matrice = (float***)malloc( x * sizeof(float**) );

	for( i = 0; i < x; ++i ) {
		matrice[i] = (float***)malloc( y * sizeof(float*) );
		for( j = 0; j < y; ++j ) {
			matrice[i][j] = (float*)malloc( z * sizeof(float) );
		}
	}

	return 0;
}

Dans mon cas, je créais un tableau 3D de pointeur de float, et non de float :slight_smile:
Edité le 14/08/2009 à 20:36

en fait le programme que je fait prend une image médicale, de taille 256x256x95 et calcul le produit de convolution entre le filtre bibi et l’image d’entrée.

et j’ai des érreur au niveau des types;
error C2440: ‘=’ : impossible de convertir de ‘float’ en ‘float
error C2296: '
’ : non conforme, l’opérande gauche est du type ‘float *’
error C2440: ‘=’ : impossible de convertir de ‘float’ en ‘float *’
error C2440: ‘=’ : impossible de convertir de ‘float *’ en ‘float’

si vous voyez quelque chose de louche , faite moi par de vos suggestions;

merci encore pour votre aie



	for( i = 0; i < x; ++i ) {unsigned int x = 256,y = 256,z =95, i, j, k;
	float ****matrice = (float****)malloc( x * sizeof(float***) );

	for( i = 0; i < x; ++i ) {
		matrice[i] = (float***)malloc( y * sizeof(float**) );
		for( j = 0; j < y; ++j ) {
			matrice[i][j] = (float**)malloc( z * sizeof(float*) );
			for( k = 0; k < z; ++k ) {
				matrice[i][j][k] = (float*) malloc( sizeof(float) );
			}	 
		}
	}

float ****tempo = (float****)malloc( x * sizeof(float***
		matrice[i] = (float***)malloc( y * sizeof(float**) );
		for( j = 0; j < y; ++j ) {
			matrice[i][j] = (float**)malloc( z * sizeof(float*) );
			for( k = 0; k < z; ++k ) {
				matrice[i][j][k] = (float*) malloc( sizeof(float) );
			}	 
		}
	}

int L=0;

ofstream fecrit("Att_modif_smooth.img", ios:: out | ios::binary);


float bibi[7][7] ={{0.0034  ,  0.0073 ,   0.0116 ,   0.0135 ,   0.0116  ,  0.0073  ,  0.0034}
		,{0.0073  ,  0.0158  ,  0.0251 ,   0.0293   , 0.0251 ,   0.0158,    0.0073}
		,{0.0116  ,  0.0251,    0.0399,    0.0465 ,   0.0399 ,   0.0251   , 0.0116}
		,{0.0135 ,   0.0293  ,  0.0465  ,  0.0543  ,  0.0465  ,  0.0293 ,   0.0135}
		,{0.0116  ,  0.0251  ,  0.0399,    0.0465,    0.0399 ,   0.0251 ,   0.0116}
		,{0.0073 ,   0.0158,    0.0251 ,   0.0293 ,   0.0251  ,  0.0158 ,   0.0073}
		,{0.0034 ,   0.0073  ,  0.0116 ,   0.0135,    0.0116 ,   0.0073 ,   0.0034}};


	// pour faciliter le calcul de convolution je place les données image dans une matrice 3D
	for(int coupe = 0 ; coupe<95 ; coupe++)
			{
			for(int xxx = 0 ; xxx <256 ; xxx++)
				{
				for(int yyy = 0 ; yyy<256 ; yyy++)
					{
					matrice[xxx][yyy][coupe]=image[L]; //1ère erreur a cette ligne
					L++;
					}
				}
			};
	

//produit de convolution
 for(int coupe =0; coupe<95 ; coupe++)
	for (int jjj = 0; jjj < 256; jjj++)
		for (int iii = 0; iii < 256; iii++)
			{
			float tmp = 0;
			
			for (int y = 0; y <7; y++)
				for (int x = 0; x < 7; x++)
					tmp += matrice[j+ y][ i + x][coupe] * bibi[y][x]; //deuxième erreur
				
					tempo[j][i][coupe] = tmp; //trosième érreur
			}
	
//je retransfere lmage dans un tableau a une dimensions
	L=0;
	for(int coupe = 0 ; coupe<95 ; coupe++)
			{
			for(int xxx = 0 ; xxx <256 ; xxx++)
				{
				for(int yyy = 0 ; yyy<256 ; yyy++)
					{
					image[L]=tempo[xxx][yyy][coupe]; //quatrième érreur
					L++;
					}
				}
			};	
	
//enregistrement des données
fecrit.write((char *)image,95*x_pixels*y_pixels*sizeof(float));


fecrit.close();








Edité le 14/08/2009 à 21:01

Il faut que tu met le code de création de la matrice que j’ai mis en dernier. C’est des float et non des float*

J’ai moifié le code de création de matrice et de tempo , mais maintenant a l’execution j’ai une erreur de violation d’accès , a la ligne en majuscule



[b]	unsigned int x = 256,y = 256,z =95, i, j, k;
	float ***matrice = (float***)malloc( x * sizeof(float**) );

	for( i = 0; i < x; ++i ) {
		matrice[i] = (float**)malloc( y * sizeof(float*) );
		for( j = 0; j < y; ++j ) {
			matrice[i][j] = (float*)malloc( z * sizeof(float) );
		}
	};


float ***tempo = (float***)malloc( x * sizeof(float**) );

	for( i = 0; i < x; ++i ) {
		tempo[i] = (float**)malloc( y * sizeof(float*) );
		for( j = 0; j < y; ++j ) {
			tempo[i][j] = (float*)malloc( z * sizeof(float) );
		}
	
	};[/b]
int L=0;

ofstream fecrit("Att_modif_smooth.img", ios:: out | ios::binary);


float bibi[7][7] ={{0.0034  ,  0.0073 ,   0.0116 ,   0.0135 ,   0.0116  ,  0.0073  ,  0.0034}
		,{0.0073  ,  0.0158  ,  0.0251 ,   0.0293   , 0.0251 ,   0.0158,    0.0073}
		,{0.0116  ,  0.0251,    0.0399,    0.0465 ,   0.0399 ,   0.0251   , 0.0116}
		,{0.0135 ,   0.0293  ,  0.0465  ,  0.0543  ,  0.0465  ,  0.0293 ,   0.0135}
		,{0.0116  ,  0.0251  ,  0.0399,    0.0465,    0.0399 ,   0.0251 ,   0.0116}
		,{0.0073 ,   0.0158,    0.0251 ,   0.0293 ,   0.0251  ,  0.0158 ,   0.0073}
		,{0.0034 ,   0.0073  ,  0.0116 ,   0.0135,    0.0116 ,   0.0073 ,   0.0034}};


	// pour faciliter le calcul de convolution je place les données image dans une matrice 3D
	for(int coupe = 0 ; coupe<95 ; coupe++)
			{
			for(int xxx = 0 ; xxx <256 ; xxx++)
				{
				for(int yyy = 0 ; yyy<256 ; yyy++)
					{
					matrice[xxx][yyy][coupe]=image[L]; 
					L++;
					}
				}
			};
	

//produit de convolution
 for(int coupe =0; coupe<95 ; coupe++)
	for (int jjj = 0; jjj < 256; jjj++)
		for (int iii = 0; iii < 256; iii++)
			{
			float tmp = 0;
			
			for (int y = 0; y <7; y++)
				for (int x = 0; x < 7; x++)
	------------------------>>> tmp += matrice[j+ y][ i + x][coupe] * bibi[y][x]; //* bug ici!!
				
					tempo[j][i][coupe] = tmp; 
			}
	
//je retransfere lmage dans un tableau a une dimensions
	L=0;
	for(int coupe = 0 ; coupe<95 ; coupe++)
			{
			for(int xxx = 0 ; xxx <256 ; xxx++)
				{
				for(int yyy = 0 ; yyy<256 ; yyy++)
					{
					image[L]=tempo[xxx][yyy][coupe]; 
					L++;
					}
				}
			};	
	
//enregistrement des données
fecrit.write((char *)image,95*x_pixels*y_pixels*sizeof(float));


fecrit.close();








Edité le 14/08/2009 à 21:29

quand j’execute mon programme ca me renvoir une image blanche sur toutes les coupes, il y a un serieux problème

Apres sans pouvoir testé moi même c’est dur de te dire.

Sinon tu as le mode debug ? (parce que là…)

oui , je vais éssayer de faire un débuggage demain, je vous tiens au courant