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

New Topic/Question
Reply




MultiQuote





|