C/Visual C - Inconveniente con memoria dinamica

 
Vista:
sin imagen de perfil

Inconveniente con memoria dinamica

Publicado por Sebastián (31 intervenciones) el 07/02/2017 01:03:26
Hola saludos a todos, quisiera me ayuden a ver mi error, estoy haciendo un programa que sume dos matrices, utilizando matrices dinámicas, pero me sale un error, y no me imprime la matriz luego de que compilo e ingreso los datos, muchas gracias de antemano por su gentil ayuda.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#include <stdlib.h>
 
void ingreso( int *f, int *c)
{
	printf("Ingrese el numero de filas para la metriz : ");
	scanf("%d", f);
	printf("\nIngrese el numero de columnas para la matriz : ");
	scanf("%d", c);
}
 
void matrizdin (int **a,int **puntm2,int **puntm,int *f, int *c)
{
	puntm= new int*[*c];
	for (int i = 0; i <*f; i++)
	{
        *(puntm+i) = new int[*c];
    }
    puntm2=new int*[*c];
    for (int i = 0; i <*f; i++)
	{
        *(puntm2+i) = new int[*c];
    }
    printf("\nIngrese los valores de la matriz 1 : ");
    for (int i = 0; i < *f; i++)
	{
        for (int j = 0; j < *c; j++)
		{
			printf("\nIngrese el elemento [ %d, %d ]: ", i,j);
			scanf("%d", (*(puntm+i)+j));
        }
        printf("\n");
    }
    printf("\nIngrese los valores de la matriz 2 : ");
    for (int i = 0; i < *f; i++)
	{
        for (int j = 0; j < *c; j++)
		{
			printf("\nIngrese el elemento [ %d, %d ]: ", i,j);
			scanf("%d", (*(puntm2+i)+j));
        }
        printf("\n");
    }
    a=new int*[*c];
     for (int i = 0; i <*f; i++)
	{
        *(a+i) = new int[*c];
    }
 
    for (int i = 0; i < *f; i++)
	{
        for (int j = 0; j < *c; j++)
		{
			*(*(a+i)+j)= *(*(puntm+i)+j) + *(*(puntm2+i)+j);
 
        }
        printf("\n");
    }
    for (int i = 0; i <*f; i++)
	{
        delete[] puntm[i];
    }
     for (int i = 0; i <*f; i++)
	{
        delete[] puntm2[i];
    }
    delete[] puntm;
    delete[] puntm2;
}
void imprimir(int **a, int *f, int *c )
{
    printf("\nLa matriz final es: \n");
    for (int i = 0; i < *f; i++)
	{
 
        for (int j = 0; j < *c; j++)
		{
			printf("%d", *(*(a+i)+j));
		    printf("\n");
        }
    }
}
 
int main()
{
	int f,c;
	int **a;
	int **puntm2;
	int **puntm;
	ingreso(&f,&c);
	matrizdin(&*a,&*puntm2,&*puntm,&f,&c);
	imprimir(&*a,&f,&c);
	return 0;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 296
Bronce
Ha mantenido su posición en C/Visual C (en relación al último mes)
Gráfica de C/Visual C

Inconveniente con memoria dinamica

Publicado por agustin (272 intervenciones) el 07/02/2017 02:50:13
En la funcion matrizdin no necesitas pasar como punteros a c y f, pasalos por valor.
Cuando creas las matrices usas c para asignar memoria tanto a las filas cómo a las columnas en todas las matrices. El primero debe usar f ya que es para el número de filas y el segundo (el del bucle) usa c ya que asigna memoria a las columnas.
Haz esos cambios y prueba.
Por cierto, new y delete pertenecen a C++ pero no veo que uses C++ salvo por eso seria un código C así que si es así debes usar malloc y free.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Inconveniente con memoria dinamica

Publicado por Sebastián (31 intervenciones) el 07/02/2017 17:01:47
Hola Agustín, gracias por tu ayuda, la verdad tome tus recomendaciones en cuenta pero el programa se crashea cuando me va a imprimir la matriz final, la verdad no doy con el error.

Adjunto mi código
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <stdlib.h>
 
void ingreso( int *f, int *c)
{
	printf("Ingrese el numero de filas para la metriz : ");
	scanf("%d", f);
	printf("\nIngrese el numero de columnas para la matriz : ");
	scanf("%d", c);
}
 
void matrizdin (int **a,int **puntm2,int **puntm,int f, int c)
{
	puntm= (int**)malloc(f*sizeof(int));
	for (int i = 0;i<f;i++)
	{
        *(puntm+i)=(int*)malloc(c*sizeof(int));
    }
    puntm2= (int**)malloc(f*sizeof(int));
    for (int i = 0; i <f; i++)
	{
        *(puntm2+i)=(int*)malloc(c*sizeof(int));
    }
    printf("\nIngrese los valores de la matriz 1 : ");
    for (int i = 0; i < f; i++)
	{
        for (int j = 0; j < c; j++)
		{
			printf("\nIngrese el elemento [ %d, %d ]: ", i,j);
			scanf("%d", (*(puntm+i)+j));
        }
        printf("\n");
    }
    printf("\nIngrese los valores de la matriz 2 : ");
    for (int i = 0; i < f; i++)
	{
        for (int j = 0; j < c; j++)
		{
			printf("\nIngrese el elemento [ %d, %d ]: ", i,j);
			scanf("%d", (*(puntm2+i)+j));
        }
        printf("\n");
    }
    a= (int**)malloc(f*sizeof(int));
	for (int i = 0; i <f; i++)
	{
        *(a+i)=(int*)malloc(c*sizeof(int));
    }
 
    for (int i = 0; i < f; i++)
	{
        for (int j = 0; j < c; j++)
		{
			*(*(a+i)+j)= *(*(puntm+i)+j) + *(*(puntm2+i)+j);
 
        }
        printf("\n");
    }
    free(puntm);
    free(puntm2);
 
}
void imprimir(int **a, int f, int c )
{
    printf("\nLa matriz final es: \n");
    for (int i = 0; i < f; i++)
	{
 
        for (int j = 0; j < c; j++)
		{
			printf("%4d", *(*(a+i)+j));
		    printf("\n");
        }
    }
 
    free(a);
}
 
int main()
{
	int f,c;
	int **a=NULL;
	int **puntm2=NULL;
	int **puntm=NULL;
	ingreso(&f,&c);
	matrizdin(&*a,&*puntm2,&*puntm,f,c);
	imprimir(&*a,f,c);
	return 0;
}

espero me puedas ayudar, gracias de antemano
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 88
Ha mantenido su posición en C/Visual C (en relación al último mes)
Gráfica de C/Visual C

Inconveniente con memoria dinamica

Publicado por Thanatos (29 intervenciones) el 07/02/2017 12:52:50
Si estás trabajando en Windows, el especificador de formato %zu podría ocasionar que el programa no funcione correctamente. En su lugar utiliza %u si estás compilando para 32 bits, o %llu para 64 bits.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <stdlib.h>
 
void ingresarDimensiones(size_t *, size_t *);
void sumarMatrices(int **, int **, int ***, size_t, size_t);
void reservarMemoria(int ***, size_t, size_t);
void cargarMatriz(int **, size_t, size_t);
void mostrarMatriz(int **, size_t, size_t);
void liberarMemoria(int ***, size_t);
 
int main(void)
{
    int **matA = NULL;
    int **matB = NULL;
    int **matS = NULL;
    size_t f = 0;
    size_t c = 0;
 
    ingresarDimensiones(&f, &c);
 
    reservarMemoria(&matA, f, c);
    reservarMemoria(&matB, f, c);
    reservarMemoria(&matS, f, c);
 
    printf("\nIngrese los %zu elementos de la matriz A:\n", f * c);
    cargarMatriz(matA, f, c);
    printf("\nIngrese los %zu elementos de la matriz B:\n", f * c);
    cargarMatriz(matB, f, c);
 
    sumarMatrices(matA, matB, &matS, f, c);
 
    printf("\nLa matriz suma es:\n");
    mostrarMatriz(matS, f, c);
 
    liberarMemoria(&matA, f);
    liberarMemoria(&matB, f);
    liberarMemoria(&matS, f);
 
    return 0;
}
 
void ingresarDimensiones(size_t *f, size_t *c)
{
    printf("Ingrese el numero de filas -----> ");
    scanf("%zu", f);
    printf("Ingrese el numero de columnas --> ");
    scanf("%zu", c);
}
 
void sumarMatrices(int **matA, int **matB, int ***matS, size_t f, size_t c)
{
    for (size_t i = 0; i < f; ++i)
    {
        for (size_t j = 0; j < c; ++j)
        {
            *(*(*matS + i) + j) = *(*(matA + i) + j) + *(*(matB + i) + j);
        }
    }
}
 
void reservarMemoria(int ***mat, size_t f, size_t c)
{
    *mat = malloc(f * sizeof *mat);
    if (*mat)
    {
        for (size_t i = 0; i < f; ++i)
        {
            *(*mat + i) = malloc(c * sizeof *(*mat + i));
        }
    }
}
 
void cargarMatriz(int **mat, size_t f, size_t c)
{
    for (size_t i = 0; i < f; ++i)
    {
        for (size_t j = 0; j < c; ++j)
        {
            printf("mat[%zu, %zu] = ", i + 1, j + 1);
            scanf("%d", *(mat + i) + j);
        }
    }
}
 
void mostrarMatriz(int **mat, size_t f, size_t c)
{
    for (size_t i = 0; i < f; ++i)
    {
        for (size_t j = 0; j < c; ++j)
        {
            printf("%4d", *(*(mat + i) + j));
        }
        printf("\n");
    }
}
 
void liberarMemoria(int ***mat, size_t f)
{
    if(mat)
    {
        for (size_t i = 0; i < f; ++i)
        {
            free(*(*mat + i));
        }
        free(*mat);
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar