Archived

This topic is now archived and is closed to further replies.

simple problem with doubles and memmory

This topic is 5147 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all. i''m developing a simple program for matrix add/multiplication but i don''t know if i''m going the right path. I''m supposed to receive doubles in the output of my program but instead i receive: 6, 8, etc... Is this right or not? Shouldn''t i receive 6.00000, 8.0000? This is my first c program... i think it crashes at the end, though i''m not quite sure(i''m using Dev c++/MingW). Maybe it''s because i didn''t freed memory correctly? Also, when i try to add a second matrix program crashes... It must because of the memmory. But where and why? Here is the source code:
#include <stdio.h>
#include <stdlib.h>

/* matrix structure */
typedef struct {
 int col;
 int lin;
 double *element;
} matrix;

/* allocates matrix space in memory*/
matrix init_matrix(int lin, int col)
{
    matrix M;
    M.element = (double *) calloc(lin * col, sizeof(M.element));
    M.lin = lin;
    M.col = col;
    return M;
}

/* add two matrix */
matrix add_matrix(matrix mat1, matrix mat2)
{
    matrix M;
    int a, b;
    M = init_matrix(mat1.lin, mat1.col);
    printf("Insert the matrix number 1\n");
    for (a = 0; a< (mat1.lin * mat1.col); a++)
        {
            scanf("%d", &mat1.element[a]);
        }
    printf("Insert the matrix number 2\n");
    for (b = 0; b< (mat1.lin * mat1.col); b++)
        {
            scanf("%d", &mat2.element[b]);
        }
    for (a = 0; a< (mat1.lin * mat1.col); a++)
        {
            M.element[a] = mat1.element[a] + mat2.element[a];
        }
    M.lin = mat1.lin;
    M.col = mat1.col;
    return M;
}

/*mul two matrix */
matrix mul_matrix(matrix mat1, matrix mat2)
{
/* in development*/
}

/* prints the resulting matrix in screen*/
void show_matrix(matrix mat1)
{
    int a, b;
    printf("Matrix = \n");
    for (a = 0; a< mat1.lin; a++)
        {
            for (b = 0; b< mat1.col; b++)
                {
                    if (b == mat1.col - 1)
                       printf("%d\n", mat1.element[a * mat1.col + b]);
                     else
                          printf("%d, ", mat1.element[a * mat1.col + b]);
                }
        }
}

int main()
{
 matrix m1, m2, result;
 int esc = 0, tam1 = 0, tam2 = 0, tam3 = 0, tam4 = 0;
 system("CLS");
 do {
     printf("1 - Add Matrix\n");
     printf("2 - Mul matrix\n");
     printf("3 - Exit\n");
     scanf("%i", &esc);
     fflush (stdin) ;
     if ((esc == 1) || (esc == 2)){
         printf("insert the number of lines for the first matrix?\n");
         scanf("%i", &tam1);
         printf("insert the number of colowns for the first matrix?\n");
         scanf("%d", &tam2);
         printf("insert the number of lines for the second matrix?\n");
         scanf("%d", &tam3);
         printf("insert the number of colowns for the second matrix??\n");
         scanf("%d", &tam4);
         if (esc == 1) {
            /* codigo adição */
            if ((tam1 ==tam3) && (tam2 == tam4)) {

                /* AT THE SECOND TIME CRASHES HERE */

                m1 = init_matrix(tam1, tam2);
                m2 = init_matrix(tam3, tam4);
                result = init_matrix(tam1, tam2);
                result = add_matrix(m1, m2);
                show_matrix(result);
                free(m1.element);
                free(m2.element);
                free(result.element);
            } else {
              printf("can''t add matrix!\n");
            }
         } else {
            /* codigo multiplicação */
            if (tam1 == tam4) {

            } else {
              printf("can''t multiply matrix!\n");
            }
         };
     };
 } while (esc != 3);
 system("PAUSE");
 return 0;
/* CRASH HERE :D */
}

Share this post


Link to post
Share on other sites
Well, the most obvious thing off the top of my head is that you need to use %f to display doubles in printf and load them in scanf, not %d (%d is an integer display/load). To decide the precision in display use %.2f (for 2 places post-decimal, for example).

-fel

[edited by - felisandria on November 12, 2003 10:35:46 AM]

Share this post


Link to post
Share on other sites
Ok, i now know that the program crashes at this function:


matrix init_matrix(int lin, int col)
{
matrix M;
M.element = (double *) calloc(lin * col, sizeof(M.element));
M.lin = lin;
M.col = col;
return M;
}


more specificaly at:
M.element = (double *) calloc(lin * col, sizeof(M.element));

this is how i see the problem:
I call the function for the first time, it allocates the space, everything goes ok. It returns a copy of M to the main(?) and destroys M because the function ends right?
So why does it crashes the second time the functions runs?

/me brain's explodes

EDIT: Thanks for the double tip fel.

[edited by - Erivan on November 12, 2003 10:38:04 AM]

Share this post


Link to post
Share on other sites
I''m not quite sure but i think the sizeof should be sizeof(double), now you take the size of a pointer (4 on 32 bit platforms, while the size of a double is 8 on 32 bit platforms). I am not sure why this would cause a crash though.

Share this post


Link to post
Share on other sites
I always though that stuff like this only happened to others
Just kidding... Need more time to get used to all this litle stuff that can get a guy to become crazy.

Share this post


Link to post
Share on other sites