5 Replies - 284 Views - Last Post: 25 September 2011 - 11:16 PM Rate Topic: -----

#1 Mruby1932  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 11-September 10

Memory Leaks! 2 Extra mallocs unexplainable

Posted 24 September 2011 - 07:07 PM

Attached File  Xstr.txt (3.57K)
Number of downloads: 50So I have been working on this library for a few days. Today I was finally able to devote some significant time to it.

I was able to get the Xstr XstrMalloc(size_t initialCapacity); and Xstr XstrMallocZstring(char * z); working except for the fact that both of them are calling malloc four times, not just two times, like it is supposed to be. When I run valgrind ./Xstr it reports
 
==459== HEAP SUMMARY:
==459==     in use at exit: 4,184 bytes in 2 blocks
==459==   total heap usage: 4 allocs, 2 frees, 4,218 bytes allocated
==459== 
==459== LEAK SUMMARY:
==459==    definitely lost: 0 bytes in 0 blocks
==459==    indirectly lost: 0 bytes in 0 blocks
==459==      possibly lost: 0 bytes in 0 blocks
==459==    still reachable: 4,184 bytes in 2 blocks
==459==         suppressed: 0 bytes in 0 blocks



The memory leak is the biggest concern, but I think it may be a result of maybe a problem with the free() function I have, but I think that is working correctly...

and I simply can't understand why I am having 4 mallocs. The code simply doesn't have more than two calls. They are not in a loop, or anything that could cause it to be called again.. I have created the project in multiple different files to make sure that I was mistakenly not testing the latest version of my own code a few different times as per suggestion, and that still didn't do it, making me think it has to be a bug in the code that I just can't see.

I outlined a few questions in the comments of the code.


/*
 *  Xstr.c
 *  
 *
 *  Created by Michael  on 9/18/11.
 *  Copyright 2011 UNM.
 *
 */

#include "Xstr.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


struct xstr		/*create struct containing to contain extendable string and attributes*/			
{				
	char* string;		/* string array to hold the values*/
	size_t capacity;	/* added capacity to track the size of the Xstr*/
	size_t size;	/* added size to track size of Xstr*/
};


/*creates a new extendable string. This function returns a non-NULL Xstr, and the
 created extendable string will have a size of 0 and a capacity of at least initialCapacity.*/

Xstr XstrMalloc(size_t initialCapacity)
{	
	Xstr newXstr = (Xstr)malloc(sizeof(struct xstr));	/* Declare and initialize newXstr and allocate memory for the struct. */
	if (newXstr == NULL) {								/* Test if there is enough memory in heap for the struct */
		fprintf(stderr, "There was not enough free memory in the heap to be allocated for the Xstr.\n");
		exit(100);							
	}
	newXstr->string = (char*)malloc(initialCapacity);	/* Allocating memory for the string in newXstr */
	if (newXstr->string == NULL) {						/* Test if there is enough memory in heap for the string */
		fprintf(stderr, "There was not enough free memory in the heap to be allocated for the Xstr's string.\n");
		exit(100);							
	}
	*(newXstr->string) = '\0';							/* pointer to the contents of newXstr's string and setting to null character */
	newXstr->capacity = initialCapacity;				/* sets the capacity of the struct to initialCapacity */
	newXstr->size = 0;									/* set the size of the struct to 0 */
	
	return newXstr;										/* return a non-null Xstr*/
}


/*creates a new extendable string. It returns a newly-created non-NULL Xstr that has a
 size equal to strlen(zstring), a capacity at least that large, and the first strlen(zstring) chars of the
 Xstr will have been initialized from the corresponding characters of zstring.*/

Xstr XstrMallocZstring(char * z)
{	
	Xstr newZstr = (Xstr)malloc(sizeof(struct xstr));			/* Declare and initialize newXstr and allocate memory for the struct. */
	if (newZstr == NULL) {										/* Test if there is enough memory in heap for the struct */
		fprintf(stderr, "There was not enough free memory in the heap to be allocated for the Zstr.\n");
		exit(100);							
	}
	newZstr->string = (char*)malloc(2*(sizeof(z)));				/* Allocating memory for the string in newXstr attempting to create double the needed space*/
	if (newZstr->string == NULL) {								/* Test if there is enough memory in heap for the string */
		fprintf(stderr, "There was not enough free memory in the heap to be allocated for the Zstr's string.\n");
		exit(100);							
	}
	strcpy((char*)(newZstr->string), z);						/* copy the passed char* z to newZstr->string */
	
	newZstr->capacity = (sizeof(newZstr->string));				/* sets the capacity of the struct to sizeof(newZstring->string) */
	newZstr->size = strlen(z);									/* set the size of the struct to strlen(z) */
	
	return newZstr;												/* return a non-null Zstr*/
}


/* The library reclaims all storage currently associated with this extendable string, x. */

void XstrFree(Xstr x)
{
	free(x->string);			/* I could be potentially freeing mallocs incorrectly here?*/ /* frees the allocated memory from the structs string */ 
	free(x);															/* frees the memory alloc'd for the struct */
}


/* returns the current size of x.*/

size_t XstrSize(Xstr x)
{
	return strlen(x->string);		/* return structs string in type size_t */
}


/* returns the current capacity of x.*/

size_t XstrCapacity(Xstr x)
{
	return sizeof(x->string);			/* not exactly sure why this is not returning the size of the capacity, the previous function works perfectly,*/    /* return structs capicity in type size_t */	
}

/* accesses an individual char within x. if index>=XStrSize(x), this function returns -1. Otherwise it accesses the indexth character of x, counting from zero, and returning its unsinged char value cast to an int*/
	/* haven't had much time to test this function but I believe it is working correctly */
int XstrGet(Xstr x, size_t index)
{
	int i; 
	int foo = -1;
	if (index >= XstrSize(x))
	{
		return foo;
	}
	else
		for (i = 0; i < index; i++){
			foo = getchar();			/* foo is set to the unsinged char value of the indexth char in x,*/
		}
	return foo;							/* foo returns either the unsigned char value converted to an int, or -1 if index >= XstrSize(x) */
}


/* main method for testing */
int main(int argc, char **argv) {
	
	Xstr x = XstrMallocZstring("tester for funzies ");
	printf("z string = %s \n size of string = %d\n capacity of string is = %d\n XstrGet = %d \n", x->string, (int)XstrSize(x), (int)XstrCapacity(x), XstrGet(x,3));
	XstrFree(x);
	return 0;
}



I attached the "Xstr.h" file but had to upload it through .txt
Any help would be much appreciated!!

Thanks for your time guys!

Mike

This post has been edited by Mruby1932: 24 September 2011 - 07:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Memory Leaks! 2 Extra mallocs unexplainable

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1418
  • View blog
  • Posts: 2,681
  • Joined: 30-May 10

Re: Memory Leaks! 2 Extra mallocs unexplainable

Posted 24 September 2011 - 11:46 PM

	newZstr->string = (char*)malloc(2*(sizeof(z)));				/* Allocating memory for the string in newXstr attempting to create double the needed space*/
	newZstr->capacity = (sizeof(newZstr->string));				/* sets the capacity of the struct to sizeof(newZstring->string) */


You need to understand the difference between strlen() and sizeof().
sizeof() is only telling you the size of the pointer, not the extent of the data it is pointing to.
Was This Post Helpful? 3
  • +
  • -

#3 Mruby1932  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 11-September 10

Re: Memory Leaks! 2 Extra mallocs unexplainable

Posted 25 September 2011 - 02:19 PM

View PostSalem_c, on 24 September 2011 - 11:46 PM, said:

	newZstr->string = (char*)malloc(2*(sizeof(z)));				/* Allocating memory for the string in newXstr attempting to create double the needed space*/
	newZstr->capacity = (sizeof(newZstr->string));				/* sets the capacity of the struct to sizeof(newZstring->string) */


You need to understand the difference between strlen() and sizeof().
sizeof() is only telling you the size of the pointer, not the extent of the data it is pointing to.


Oh so I think what you are saying is that in
	newZstr->string = (char*)malloc(2*(sizeof(z)));				/* Allocating memory for the string in newXstr attempting to create double the needed space*/ 


is just allocating enough memory for the pointer z. So I should be taking (char*)malloc(strlen(z)); ?

I also am attempting to created double the amount of room necessary for the Zstring, but it isn't allocating that space with a simple 2*(sizeof(z)). Why is that? why doesn't it simply double the amount of what sizeof is returning?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 3055
  • View blog
  • Posts: 9,291
  • Joined: 25-December 09

Re: Memory Leaks! 2 Extra mallocs unexplainable

Posted 25 September 2011 - 04:39 PM

Have you tried printing out what sizeof(z) evaluates to? Does it show the size of the array? You may want to review sizeof and insure you are using it properly.

Jim

This post has been edited by jimblumberg: 25 September 2011 - 04:39 PM

Was This Post Helpful? 0
  • +
  • -

#5 Mruby1932  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 11-September 10

Re: Memory Leaks! 2 Extra mallocs unexplainable

Posted 25 September 2011 - 06:06 PM

View Postjimblumberg, on 25 September 2011 - 04:39 PM, said:

Have you tried printing out what sizeof(z) evaluates to? Does it show the size of the array? You may want to review sizeof and insure you are using it properly.

Jim


sizeof() evaluates to 8, I think its the size of the pointer. I changed it to sizeof(*z) and it gave me 1, the size of the character.

I changed the code to:

Xstr XstrMallocZstring(char * z)
{	
	Xstr newZstr = (Xstr)malloc(sizeof(struct xstr));			/* Declare and initialize newXstr and allocate memory for the struct. */
	if (newZstr == NULL) {										/* Test if there is enough memory in heap for the struct */
		fprintf(stderr, "There was not enough free memory in the heap to be allocated for the Zstr.\n");
		exit(100);							
	}
	newZstr->capacity = strlen(z)*2;				/* sets the capacity of the struct to double the string length, because I am trying to allocated double the amount of space needed for a whatever string I pass in */
	newZstr->size = strlen(z);									/* set the size of the struct to strlen(z) */
	
	newZstr->string = (char*)malloc(newZstr->capacity);					/* Allocating memory by getting newZstr->capacity */
	if (newZstr->string == NULL) {								/* Test if there is enough memory in heap for the string */
		fprintf(stderr, "There was not enough free memory in the heap to be allocated for the Zstr's string.\n");
		exit(100);							
	}
	strncpy((char*)(newZstr->string), z, strlen(z)+1);						/* copy the passed char* z to newZstr->string +1 for a null byte at the end*/
	
	
	return newZstr;												/* return a non-null Zstr*/
}




This code seems to be working but I am still having 4 mallocs and only 2 frees which I cannot explain.
I think I understand that in my previous code, I was only allocating 8 for the newZstr->string. I guess the newZstr's malloc was storing the extra characters in longer strings?? I don't know, because longer than 8 character strings would print out successfully.

Thanks for responding

View PostSalem_c, on 24 September 2011 - 11:46 PM, said:

	newZstr->string = (char*)malloc(2*(sizeof(z)));				/* Allocating memory for the string in newXstr attempting to create double the needed space*/
	newZstr->capacity = (sizeof(newZstr->string));				/* sets the capacity of the struct to sizeof(newZstring->string) */


You need to understand the difference between strlen() and sizeof().
sizeof() is only telling you the size of the pointer, not the extent of the data it is pointing to.



Thank you I see now, I was missing that before.

This post has been edited by Mruby1932: 25 September 2011 - 06:04 PM

Was This Post Helpful? 0
  • +
  • -

#6 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1418
  • View blog
  • Posts: 2,681
  • Joined: 30-May 10

Re: Memory Leaks! 2 Extra mallocs unexplainable

Posted 25 September 2011 - 11:16 PM

Can you post your latest .c file, then I can also run valgrind on it here this afternoon (approx 6 hours from now).

At the moment, your two mallocs and two frees seem to match up.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1