Jump to content

[c++] problem with memory...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
mamo139

mamo139

    Newbie

  • Members
  • Pip
  • 4 posts
i'm writing in c++ a class to manage matrixs.

matrici.h

#include <cstdlib>

#include <iostream>


#include <stdio.h>

#include <stdlib.h>

#include <math.h>


#ifndef _MATRICI_H

#define _MATRICI_H 1


//**************** class ******************//


class matrice {

    public:

        int r;

        int c;

        double ** m;

        

        //costruttore e distruttore

        matrice();

        ~matrice();

        

        //crea matrice

        void create(int r, int c);

        void create_with_value(int r, int c, double value);

        

        //visualizza matrice

        void print();

        

        //caricamento - salvataggio matrici

        void load_from_file(char * file);

        void save_in_file(char * file);

        

        //operazioni su matrici

        friend matrice operator+(matrice, matrice);

        

        

};

    

#endif


matrici.cpp

#include "matrici.h"


//costruttore

matrice::matrice(){

    r = 0;

    c = 0;    

}


//distruttore

matrice::~matrice(){

    

}


//creazione matrice

void matrice::create(int r, int c){ // r = righe, c = colonne


    int x,y;

     

    this->r = r;

    this->c = c;

    m = new double *[r];

    for(x=0;x<r;x++)

        m[x] = new double[c];


         

}

void matrice::create_with_value(int r, int c, double value){

    

    int x,y;

     

    this->r = r;

    this->c = c;

    m = new double *[r];

    for(x=0;x<r;x++)

        m[x] = new double[c];

            

    for(x=0;x<r;x++)

        for(x=0;x<r;x++)

            m[x][y] = value;

     

}


//visualizza matrice

void matrice::print(){

     

     int x,y;

     

     for(x=0;x<r;x++){

          for(y=0;y<c;y++)

               printf("%12.6f\x20",m[x][y]);

          printf("\n");     

     }

     printf("\n");

     

}


//caricamento - salvataggio matrici

void matrice::load_from_file(char * file){

    

    FILE *stream;

    char *buffer, *number;

    long filesize, readed;

    long x, y=0, mr=0, mc=0, r=0, c=0;


    stream = fopen(file,"rb");

    fseek(stream,0,SEEK_END);

    filesize = ftell(stream);

    rewind(stream);

    

    buffer = (char *) malloc(filesize * sizeof(char));

    number = (char *) malloc(100 * sizeof(char));

    readed = fread(buffer,1,filesize,stream);

    

    for(x=0; x<filesize ;x++){ //conta colonne

        if(buffer[x] == '\x09')

            mc++;

        if(buffer[x] == '\x0D'){

            mc++;

            break;

        }

    }

    for(x=0; x<filesize ;x++){ //conta righe

        if(buffer[x] == '\x0D')

            mr++;

    }

    

    create(mr, mc);

    

    for(x=0; x<filesize ;x++){

        if(buffer[x] == '\x0A')

            continue;

        if(buffer[x] == '\x09' || buffer[x] == '\x0D'){

            number[y] = '\x00';

            this->m[r][c] = atof(number);

            y=0;

            c++;

            if(buffer[x] == '\x0D'){

                r++;

                c=0;

            }

        }

        else{

            number[y] = buffer[x];

            y++;

        }

    }


}


void matrice::save_in_file(char * file){


    int x,y;

    FILE * stream;

    stream = fopen(file, "w+");

    

    for(x=0;x<r;x++){

        for(y=0;y<c;y++){

            fprintf(stream,"%.6f",m[x][y]);

            if(y<c-1) fprintf(stream,"\x09");

        }

        fprintf(stream,"\n");     

    }

    fclose(stream);


}


//operazioni su matrici

matrice operator+(matrice a,matrice b){

    

    matrice c;

    int x,y,i;

     

    if(a.r != b.r || a.c != b.c)

        exit(0);

     

    c.create(a.r, a.c);

     

     for(x=0;x<c.r;x++)

          for(y=0;y<c.c;y++)

               c.m[x][y] = a.m[x][y]+b.m[x][y];   


    return c;

}



we can try this class with this code

#include "matrici.h"



int main(int argc, char *argv[])

{

    long x;


    matrice a,b;

    a.load_from_file("a.txt");

    a.save_in_file("b.txt");


    a.print();


    for(x=0;x<1000000000;x++)

        b = a + a;

    

    b.print();


    getchar();

    return 1;

}


this example shows very well the problem: doing 1000000000 times b=a+a; the memory used by the program increases a lot... i should find a way to free memory when i do not need it anymore.
but i don't know how to do it...

thank you very much,
Marco

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
I can see you're writing a C++ program, since you're using classes, but that leads me to my next question... why are you using fopen and malloc? The C++ way is to use ifstream and new, then use delete to free memory. Using malloc, the way you free memory after you're done using it is with free, like so:
MyStruct *str = (MyStruct*) malloc(sizeof(MyStruct));
if (str != 0) {
// Use *str here.
free(str);

Wow I changed my sig!

#3
mamo139

mamo139

    Newbie

  • Members
  • Pip
  • 4 posts
you're right... :rolleyes:
it'happened because i'm rewriting in c++ a program that i've originally written in c...:P

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Your create function should probably be in the constructor. You are allocating memory there that you never free in your destructor. That will cause your memory leak.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
mamo139

mamo139

    Newbie

  • Members
  • Pip
  • 4 posts

WingedPanther said:

Your create function should probably be in the constructor. You are allocating memory there that you never free in your destructor. That will cause your memory leak.

yes, i've putted in the destructor the code to free the bidimensional array and now it works :)

i've also defined a copy constructor and an assignment operator.

thanks everybody ;)