Rikkin 0 Denunciar post Postado Junho 28, 2013 Olá pessoal, sou novo na programação em C/C++ e estou com um problema na alocação dinamica de memória tenho um seguinte caso, é um programa que lê uma matriz e um vetor e depois faz uns calculos e joga num arquivo de saída: int main(){ int tamanho=0; float x[5]; float A[5][5]; float b[5]; int l=0,c=0; char linha[100]; /* ou maior se vc precisar */ FILE *arquivo; char caminho[50]; char caminho2[50]; char caminho3[50]; char caminho4[50]; printf("Digite o caminho do arquivo da matriz:"); gets(caminho); printf("Digite o caminho do arquivo do vetor:"); gets(caminho2); printf("Digite o caminho que ira conter o arquivo de saida1:"); gets(caminho3); //Para pegar o tamanho da matriz arquivo = fopen(caminho,"r"); if(arquivo == NULL) { printf("arquivo desconhecido - nome_do_arquivo.txt\n"); return 0; } while(fgets(linha, sizeof(linha), arquivo) != NULL) { tamanho++; } fclose(arquivo); //Para alocar a matriz do arquivo arquivo = fopen(caminho,"r"); if(arquivo == NULL) { printf("arquivo desconhecido - nome_do_arquivo.txt\n"); return 0; } while(fgets(linha, sizeof(linha), arquivo) != NULL) { char *split = strtok(linha," "); if(split) { A[l][c] = (float)strtod(split,NULL); c++; } while(split != NULL) { split=strtok(NULL," "); if (c < tamanho) A[l][c] = (float)strtod(split,NULL); c++; } c=0; l++; } fclose(arquivo); c=0; l=0; //Para o arquivo da variavel b arquivo = fopen(caminho2,"r"); if(arquivo == NULL) { printf("arquivo desconhecido - nome_do_arquivo.txt\n"); return 0; } while(fgets(linha, sizeof(linha), arquivo) != NULL) { if (c < tamanho) b[c] = (float)strtod(linha,NULL); c++; } int k,i,j; float m; for (k=0;k<=tamanho-2;k++){ for (i=k+1;i<=tamanho-1;i++){ m=A[i][k]/A[k][k]; for(j=0;j<tamanho;j++){ A[i][j]=A[i][j]-(m*A[k][j]); } b[i]=b[i]-(m*b[k]); } ofstream fout(strcat(caminho3,"\\matriz.txt")); for(i=0; i<tamanho; i++){ for(j=0; j<tamanho; j++){ fout << A[i][j] << "\t"; } fout << endl; } } retrosubstituicao(A,b,x,tamanho); ofstream fout("C:\\Projetos1\\matriz2.txt"); for(i=0; i<tamanho; i++){ fout << "x " << i << " "<< x[i] << "\t"; fout << endl; } return 0; } E isso esta funcionando perfeitamente, só que agora eu queria passar os vetores e matrizes para alocação dinamica, e fiz isso: int main(){ int tamanho=0; int l=0,c=0; char linha[100]; /* ou maior se vc precisar */ FILE *arquivo; char caminho[50]; char caminho2[50]; char caminho3[50]; char caminho4[50]; float *x = (float*) malloc(sizeof(float)); float **A = (float**) malloc(sizeof(float)); float *b = (float*) malloc(sizeof(float)); printf("Digite o caminho do arquivo da matriz:"); gets(caminho); printf("Digite o caminho do arquivo do vetor:"); gets(caminho2); printf("Digite o caminho que ira conter o arquivo de saida1:"); gets(caminho3); //Para pegar o tamanho da matriz arquivo = fopen(caminho,"r"); if(arquivo == NULL) { printf("arquivo desconhecido - nome_do_arquivo.txt\n"); return 0; } while(fgets(linha, sizeof(linha), arquivo) != NULL) { tamanho++; } fclose(arquivo); //Para alocar a matriz do arquivo arquivo = fopen(caminho,"r"); if(arquivo == NULL) { printf("arquivo desconhecido - nome_do_arquivo.txt\n"); return 0; } while(fgets(linha, sizeof(linha), arquivo) != NULL) { char *split = strtok(linha," "); if(split) { A[l][c] = (float)strtod(split,NULL); c++; } while(split != NULL) { split=strtok(NULL," "); if (c < tamanho) A[l][c] = (float)strtod(split,NULL); c++; } c=0; l++; } fclose(arquivo); c=0; l=0; //Para o arquivo da variavel b arquivo = fopen(caminho2,"r"); if(arquivo == NULL) { printf("arquivo desconhecido - nome_do_arquivo.txt\n"); return 0; } while(fgets(linha, sizeof(linha), arquivo) != NULL) { if (c < tamanho) b[c] = (float)strtod(linha,NULL); c++; } int k,i,j; float m; for (k=0;k<=tamanho-2;k++){ for (i=k+1;i<=tamanho-1;i++){ m=A[i][k]/A[k][k]; for(j=0;j<tamanho;j++){ A[i][j]=A[i][j]-(m*A[k][j]); } b[i]=b[i]-(m*b[k]); } ofstream fout(strcat(caminho3,"\\matriz.txt")); for(i=0; i<tamanho; i++){ for(j=0; j<tamanho; j++){ fout << A[i][j] << "\t"; } fout << endl; } } retrosubstituicao(A,b,x,tamanho); ofstream fout("C:\\Projetos1\\matriz2.txt"); for(i=0; i<tamanho; i++){ fout << "x " << i << " "<< x[i] << "\t"; fout << endl; } return 0; } Ai ele compila normal, só que na hora de executar ele fala que o arquivo .exe parou de funcionar e nao dá erro nenhum alguem sabe o que pode estar acontecendo? Compartilhar este post Link para o post Compartilhar em outros sites
GBecker 51 Denunciar post Postado Junho 29, 2013 Tentou debugar o código? Deve ser alugma referencia errada de memória. Compartilhar este post Link para o post Compartilhar em outros sites
Mateus GP 13 Denunciar post Postado Julho 3, 2013 O erro está em: float *x = (float*) malloc(sizeof(float)); float **A = (float**) malloc(sizeof(float)); float *b = (float*) malloc(sizeof(float)); Pesquise um pouco sobre alocação dinâmica, suas utilizações e correlações com a estática. Compartilhar este post Link para o post Compartilhar em outros sites