Valgrind says I suck at memory management :)

This is a discussion on Valgrind says I suck at memory management :) within the C Programming forums, part of the General Programming Boards category; So I decided to write a function to take a file, and put each line into its own element of ...

  1. #1
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398

    Valgrind says I suck at memory management :)

    So I decided to write a function to take a file, and put each line into its own element of an array, for example...
    file.txt
    Code:
    X
    YHOO
    FCX
    Code:
    char **data;
    data[0] = X
    data[1] = YHOO
    data[2] = FCX
    Here is what I am doing, but I believe I am just doing some dirty things to memory, but I can't spot it.
    main.c
    Code:
    #include <stdio.h>
    
    #include "strlib.h"
    
    int main (void)
    {
    	struct strlib_gl fileinfo;
    	int i, j;
    	
    	fileinfo.filename = "data.dat";
    	fileinfo.count = 0;
    
    	getlines(&fileinfo);
    
    	for (i = 0; i < fileinfo.count+1; i++)
    	{
    		for (j = 0; j < strlen(fileinfo.data[i]); j++)
    			printf("%c", fileinfo.data[i][j]);
    		printf("\n");
    		free(fileinfo.data[i]);
    	}
    
    	free(fileinfo.data);
    
    	return 0;
    }
    strlib.c
    Code:
    #include "strlib.h"
    
    void getlines(struct strlib_gl *info)
    {
    	FILE *fp;
    	int count = 0;
    	char *temp;
    
    	fp = fopen(info->filename, "r");
    
    	info->data = malloc((count+1) * sizeof(char *));
    	info->data[count] = malloc(4 * sizeof(char));
    
    	temp = fgets(info->data[count], 100, fp);
    	while (temp != NULL)
    	{
    		info->data[count] = temp;
    		count++;
    		info->data = realloc(info->data, (count+1) * sizeof(char *));
    		info->data[count] = malloc(4 * sizeof(char));
    
    		temp = fgets(info->data[count], 100, fp);
    	}
    
    	fclose(fp);
    	info->count = count;
    }
    struct strlib_gl
    Code:
    struct strlib_gl
    {
    	char *filename;
    	char **data;
    	int count;
    };
    You guys see anything alarming right off the bat? Thanks for any help!

    EDIT: I made one stupid error, fixed, but valgrind is still telling me some stuff
    Code:
    ==20207== Invalid write of size 1
    ==20207==    at 0x4026944: memcpy (mc_replace_strmem.c:402)
    ==20207==    by 0x4097AB2: _IO_getline_info (in /lib/tls/i686/cmov/libc-2.8.90.so)
    ==20207==    by 0x40979A0: _IO_getline (in /lib/tls/i686/cmov/libc-2.8.90.so)
    ==20207==    by 0x4096889: fgets (in /lib/tls/i686/cmov/libc-2.8.90.so)
    ==20207==    by 0x804872C: getlines (strlib.c:22)
    ==20207==    by 0x804857E: main (main.c:13)
    ==20207==  Address 0x4195264 is 0 bytes after a block of size 4 alloc'd
    ==20207==    at 0x4025D2E: malloc (vg_replace_malloc.c:207)
    ==20207==    by 0x8048702: getlines (strlib.c:20)
    ==20207==    by 0x804857E: main (main.c:13)
    
    ==20207== Invalid write of size 1
    ==20207==    at 0x40968C5: fgets (in /lib/tls/i686/cmov/libc-2.8.90.so)
    ==20207==    by 0x804872C: getlines (strlib.c:22)
    ==20207==    by 0x804857E: main (main.c:13)
    ==20207==  Address 0x4195265 is 1 bytes after a block of size 4 alloc'd
    ==20207==    at 0x4025D2E: malloc (vg_replace_malloc.c:207)
    ==20207==    by 0x8048702: getlines (strlib.c:20)
    ==20207==    by 0x804857E: main (main.c:13)
    
    ==20207== Invalid read of size 1
    ==20207==    at 0x80485A5: main (main.c:18)
    ==20207==  Address 0x4195264 is 0 bytes after a block of size 4 alloc'd
    ==20207==    at 0x4025D2E: malloc (vg_replace_malloc.c:207)
    ==20207==    by 0x8048702: getlines (strlib.c:20)
    ==20207==    by 0x804857E: main (main.c:13)
    
    ==20358== Conditional jump or move depends on uninitialised value(s)
    ==20358==    at 0x402643B: strlen (mc_replace_strmem.c:242)
    ==20358==    by 0x80485CF: main (main.c:17)
    Last edited by carrotcake1029; 02-01-2009 at 06:33 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    	info->data[count] = malloc(4 * sizeof(char));  //So we have room for three characters....
    
    	temp = fgets(info->data[count], 100, fp);  //...And we then read 100 characters into it

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Ok. Since then I changed it to 5, because at max, there will be 4 letters per line, plus the newline, plus a null. Also, it doesn't read 100 chars into it, it reads up until 100 chars, or a newline or EOF is encountered, which in my case is newline and EOF.
    EDIT: nvm fixed it. I forgot it needed null terminator. DUH

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Also, data[count] doesn't exist (you free it at the bottom of getlines), and yet you try to access it inside your for loop.

    And you have no idea whether or not it reads 100 chars in it; however, you are promising the system that if it needs to, it can store 100 characters into your memory. Since you can't....

  5. #5
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Wait, I don't think it can even reach data[count] in my for loop because it is i < count. Since it was unused to begin with, I just free it immediately.
    Last edited by carrotcake1029; 02-01-2009 at 07:03 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by carrotcake1029 View Post
    Wait, I don't think it can even reach data[count] in my for loop because it is i < count. Since it was unused to begin with, I just free it immediately.
    Code:
    for (i = 0; i < fileinfo.count+1; i++)

  7. #7
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Oh, so sorry, I edited that, forgot to mention. But ya, you are right, when haven't you been when it comes to my postings, lol. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-01-2007, 01:06 AM
  2. memory management when linking to a C DLL
    By bithub in forum C# Programming
    Replies: 2
    Last Post: 02-01-2006, 10:35 AM
  3. Memory Management
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-01-2003, 09:01 PM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. Why america sucks
    By muttski in forum A Brief History of Cprogramming.com
    Replies: 138
    Last Post: 04-10-2002, 11:57 PM

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