memory loss multidimensional array

This is a discussion on memory loss multidimensional array within the C Programming forums, part of the General Programming Boards category; Hi all, with the below code valgrind tells me i am leaking memory, does anyone have an idea? my main ...

  1. #1
    llb
    llb is offline
    Registered User
    Join Date
    Mar 2012
    Posts
    1

    Question memory loss multidimensional array

    Hi all,
    with the below code valgrind tells me i am leaking memory, does anyone have an idea?


    my main intention here is to have an array of strings (fixed string lengh but dynamic number of strings) which i can pass back and forth between main and other functions. (without decrlaring it global)

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXLINES 65534
    #define BUFSIZE 1024
    #define BUFROW 80
    
    #define ALLOC_ERR {fprintf(stderr,"Error allocating memory\n"); exit(EXIT_FAILURE);}
    
    int lIndex;
    
    char **extendArr(char **tempArr)
    {
        int i, len;
        if(lIndex==0)
        {
            tempArr = malloc(BUFROW * sizeof(char *));
            if(tempArr ==NULL)
                ALLOC_ERR
            for(i=0; i< BUFROW; i++)
            {
                tempArr[i]=(char *) malloc(BUFSIZE * sizeof(char));
                tempArr[i]="aaa";
            }
        }
        return(tempArr);
    }
    
    int readLine(char **tempArr)
    {
        int i;
        //if(extendArr(tempArr) == NULL)
        //    ALLOC_ERR
        for(i=0;i<BUFROW; i++)
        {
            tempArr[i]="aaa";
        //    fprintf(stdout,"%d\t%s\n",i,tempArr[i]);
        }
    
        return(1);
    }
    
    
    
    int main(int argc, char **argv)
    {
        int i;
        char **tempArr;
    // not checking for NULL here, its just a test-setup because of the array passing-around
        tempArr=malloc(BUFROW * sizeof(char *));
        for (i=0; i< BUFROW; i++)
            tempArr[i]=malloc(BUFSIZE*sizeof(char));
    
    
        readLine(tempArr);
    
        for (i=0; i< BUFROW; i++)
        {
            fprintf(stdout,"%d\t%s\n",i,tempArr[i]);
            //readLine(tempArr);
        }
        for(i=0;i < BUFROW; i++)
            free(tempArr[i]);
        free(tempArr);
        return(1);
    }
    ==12738== Invalid free() / delete / delete[]
    ==12738== at 0x40282A3: free (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    ==12738== by 0x405F002: (below main) (in /lib/libc-2.14.1.so)
    ==12738== Address 0x80487d9 is not stack'd, malloc'd or (recently) free'd
    ==12738==
    ==12738==
    ==12738== HEAP SUMMARY:
    ==12738== in use at exit: 81,920 bytes in 80 blocks
    ==12738== total heap usage: 81 allocs, 81 frees, 82,240 bytes allocated
    ==12738==
    ==12738== Searching for pointers to 80 not-freed blocks
    ==12738== Checked 48,820 bytes
    ==12738==
    ==12738== 81,920 bytes in 80 blocks are definitely lost in loss record 1 of 1
    ==12738== at 0x402913D: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    ==12738== by 0x405F002: (below main) (in /lib/libc-2.14.1.so)
    ==12738==
    ==12738== LEAK SUMMARY:
    ==12738== definitely lost: 81,920 bytes in 80 blocks
    ==12738== indirectly lost: 0 bytes in 0 blocks
    ==12738== possibly lost: 0 bytes in 0 blocks
    ==12738== still reachable: 0 bytes in 0 blocks
    ==12738== suppressed: 0 bytes in 0 blocks


    the invalid free and the mem loss does not occur when i comment out
    tempArr[i]="aaa";


    thanks a lot

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
            for(i=0; i< BUFROW; i++)
            {
                tempArr[i]=(char *) malloc(BUFSIZE * sizeof(char));
                tempArr[i]="aaa";
            }
    Right there you throw away your allocated memory and make your pointer point to a string literal. You want something like strcpy.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Well, you're mallocing for temparr twice. Once in main and again in extendarr. Thus, you're losing track of the first block allocation when the second allocation replaces the address pointed to with the second block allocation.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Quote Originally Posted by quzah View Post
    tempArr[i]="aaa";
    }[/code]Right there you throw away your allocated memory and make your pointer point to a string literal. You want something like strcpy.
    Quzah.
    I'm surprised that didn't blow the compile right away since you can't even assign strings like that.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Cynic View Post
    I'm surprised that didn't blow the compile right away since you can't even assign strings like that.
    You can assign strings like that, because he's assigning to a pointer, not to an array. This is perfectly fine:
    Code:
    char *helloworld;
    
    helloworld = "Hello world!";
    printf( "%s\n", helloworld );
    You are making a pointer point to a string literal. This is what you can't do:
    Code:
    char hw[ BUFSIZ ];
    
    hw = "Hello world!"; /* error */
    But this is fine:
    Code:
    char hw[ BUFSIZ ] = "Hello world!";

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Ok. It seems I've forgotten a lot more than I thought, but I can't recall ever assigning string literals.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocating memory for multidimensional arrays - how to?
    By SpectreZz in forum C Programming
    Replies: 6
    Last Post: 04-29-2011, 09:40 AM
  2. Help -- allocating memory in a multidimensional array
    By jonathan.plumb in forum C Programming
    Replies: 10
    Last Post: 05-21-2009, 12:04 AM
  3. Singly-linked lists and memory loss (valgrind report)
    By Nazgulled in forum C Programming
    Replies: 5
    Last Post: 03-11-2008, 07:45 AM
  4. memory loss again
    By darfader in forum C Programming
    Replies: 3
    Last Post: 08-13-2003, 04:14 AM
  5. How to allocate memory in multidimensional array?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-15-2001, 11:07 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21