Thread: Memory usage blows up

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    50

    Memory usage blows up

    Hi guys,
    I am using some a piece of code that blows up the memory usage. The function is the following
    Code:
    static double ** matrix(int nrh, int nch)
    {
        int   i;
        double **m;
    
        m = (double **) calloc((nrh + 1), sizeof(double *));
        for (i = 0; i <= nrh; i++)
    	m[i] = (double*) calloc((nch + 1), sizeof(double));
        return m;
    }
    and it is used inside another function (let's say "func") that it is called repeatdly in the main program. In particular inside this function the former is used as follows
    Code:
    double func()
    {
    int n=1;
    double **P
    P=matrix(n, n+1);
    }
    I guess that the problem is that P is not freed at the end of func. I have tried free(P), but of course it did not work. Somebody can help me?
    Thank you!
    ai confini della conoscenza

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by violatro View Post
    I guess that the problem is that P is not freed at the end of func
    Yep, that's the problem!
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    50
    I tried the following inside "func" (hence wothout using the function matrix)
    Code:
    double func()
    {
        int n=1;
        double **P;
        P = (double **) malloc((n + 1)*sizeof(double *));
        for (i = 0; i <= n; i++)
        P[i] = (double*) malloc((n + 2)*sizeof(double));
     
        ....
    
       for (i = 0; i <= n; i++){ free(P[i]); }
       free(P);
    }
    but I get the following error " glibc detected".
    ai confini della conoscenza

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    They really need to change that stupid error message. It leaves you thinking "what the heck is a glibc, and why has one been detected?"

    Actually, it means that glibc (the C runtime library) detected a problem of some kind. The full error message probably looked like this:

    Code:
    *** glibc detected *** the important stuff that you did not post
    Whatever that stuff in red is, that's the key to the problem, not the words "glibc detected."

    From the code you posted, the free() calls look okay, so I suspect the code in between malloc() and free() is overflowing one or more of those arrays you allocate.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    50
    Thank you very much! In fact the trouble was between malloc() and free()!

    Quote Originally Posted by brewbuck View Post
    They really need to change that stupid error message. It leaves you thinking "what the heck is a glibc, and why has one been detected?"

    Actually, it means that glibc (the C runtime library) detected a problem of some kind. The full error message probably looked like this:

    Code:
    *** glibc detected *** the important stuff that you did not post
    Whatever that stuff in red is, that's the key to the problem, not the words "glibc detected."

    From the code you posted, the free() calls look okay, so I suspect the code in between malloc() and free() is overflowing one or more of those arrays you allocate.
    ai confini della conoscenza

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory / CPU Usage
    By javaeyes in forum C Programming
    Replies: 3
    Last Post: 02-27-2012, 06:19 PM
  2. Memory Usage
    By MK27 in forum Linux Programming
    Replies: 2
    Last Post: 07-16-2009, 05:52 PM
  3. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 05:45 AM
  4. Memory Usage
    By ghe1 in forum Linux Programming
    Replies: 0
    Last Post: 03-18-2002, 09:43 AM
  5. Memory usage
    By Razor Ice in forum C++ Programming
    Replies: 3
    Last Post: 01-08-2002, 03:16 PM