I have the following data types:
formation.h
typedef struct formation_t {
Player players[FORMATION_NUM_PLAYERS];
int numPlayers; /* How many players are in the above array */
int timesPlayed;
int timesWon;
}* Formation;
team.h
typedef struct team_t {
char* name;
char* coachName;
Formation* formations;
int currFormations;
int maxFormations;
}* Team;
And the following functions:
Team teamCreate(char* name, char* coach, int maxFormations)
{
//Check if parameters make sense.
if (name == NULL || coach == NULL || maxFormations < 1)
{
return NULL;
}
//Try allocating memory for name.
char* teamName = malloc(strlen(name) + 1);
if (teamName == NULL)
{
return NULL;
}
strcpy(teamName, name);
//Try allocating memory for coachName.
char* coachName = malloc(strlen(coach) + 1);
if (coachName == NULL)
{
free(teamName);
return NULL;
}
strcpy(coachName, coach);
//Try allocating memory for formations.
Formation* formations = malloc(sizeof(Formation) * maxFormations);
if (formations == NULL)
{
free(teamName);
free(coachName);
return NULL;
}
//Try allocating memory for team.
Team newTeam = malloc(sizeof(struct team_t));
if (newTeam == NULL)
{
free(teamName);
free(coachName);
free(formations);
return NULL;
}
//Initialize newly created team.
newTeam->name = teamName;
newTeam->coachName = coachName;
newTeam->maxFormations = maxFormations;
newTeam->currFormations = 0;
//Return created team.
return newTeam;
}
TeamResult teamAddFormation(Team team, Formation formation)
{
//Check for TEAM_NULL_ARGUMENT.
if (team == NULL | formation == NULL)
{
return TEAM_NULL_ARGUMENT;
}
//Check for TEAM_IS_FULL.
if (team->currFormations == team->maxFormations)
{
return TEAM_IS_FULL;
}
//Add formation.
printf("\n -about to clone- \n");
team->formations[team->currFormations] = formationClone(formation);
printf("\n -clone completed- \n");
team->currFormations = team->currFormations + 1;
return TEAM_SUCCESS;
}
Formation formationClone(Formation formation)
{
if (formation == NULL)
{
return NULL;
}
Formation newFormation = malloc(sizeof(struct formation_t));
if (newFormation == NULL)
{
return NULL;
}
*newFormation = *formation;
return newFormation;
}
When I try to test my work using the following code, I get a segmentation fault right after "about to clone".
Team team = teamCreate("Ac Milan", "Carletto", 2);
Formation formation1 = formationCreate();
ASSERT_NULL_ARGUMENT(teamAddFormation(NULL, formation1));
ASSERT_SUCCESS(teamAddFormation(team, formation1));
In teamCreate() you never set your formations local variable into your Team structure after allocating it.
This is first:
//Try allocating memory for formations.
Formation* formations = malloc(sizeof(Formation) * maxFormations);
if (formations == NULL)
{
free(teamName);
free(coachName);
return NULL;
}
then you do this after allocation the host-object:
//Initialize newly created team.
newTeam->name = teamName;
newTeam->coachName = coachName;
newTeam->maxFormations = maxFormations;
newTeam->currFormations = 0;
//Return created team.
return newTeam;
You never save the formation pointer to the structure member, so that pointer member is indeterminate and using it is invoking undefined behavior.
Add this to the bottom of that stack of assignments:
newTeam->formations = formations;
//Try allocating memory for name.
char* teamName = malloc(strlen(name) + 1);
if (teamName == NULL)
{
return NULL;
}
strcpy(teamName, name);
teamName is pointing to an alocated char array. This array never deleted. This lead to a memory leak.
Related
I am trying to insert Node to Binary tree. This is my function for creating Node (rest is done).
void BVSCreate_function(TNodef *rootPtr, function_save token) {
TNodef *newPtr = malloc(sizeof(struct tnodef));
if (newPtr == NULL) {
fprintf(stderr, "99");
return;
}
TNodef init;
string initStr;
initStr.str = NULL;
initStr.length = 0;
initStr.alloc = 0;
newPtr = &init;
newPtr->content = &initStr;
newPtr->leftPtr = NULL;
newPtr->rightPtr = NULL;
newPtr->return_type = token.ret_value;
newPtr->parameters = token.param_count;
strCpyStr(newPtr->content, token.content);
rootPtr = newPtr;
}
void BVSInsert_function(TNodef *rootPtr, function_save token) {
if (rootPtr == NULL) {
BVSCreate_function(rootPtr, token);
} else {
if ((strCmpStr(token.content, rootPtr->content)) < 0) {
BVSCreate_function(rootPtr->leftPtr, token);
} else
if ((strCmpStr(token.content, rootPtr->content)) > 0) {
BVSCreate_function(rootPtr->rightPtr, token);
}
}
}
When TNodef and function_save are structs:
typedef struct {
string *content;
int param_count;
int ret_value;
} function_save;
typedef struct tnodef {
string *content;
struct tnodef *leftPtr;
struct tnodef *rightPtr;
int parameters;
int return_type;
} TNodef;
Where string is defined as this struct:
typedef struct {
char *str; // content of string
int length; // length of string
int alloc; // amount of memory allocated
} string;
strCpystr function :
int strCpyStr(string *s1, string *s2) {
int len2 = s2->length;
if (len2 > s1->alloc) {
if (((s1->str) = (char *)realloc(s1->str, len2 + 1)) == NULL) {
return 1;
}
s1->alloc = len2 + 1;
}
strcpy(s1->str, s2->str);
s1->length = len2 + 1;
return 0;
}
I am trying to create a node in binary tree and put there information from struct function_save.
But when I try to print this tree after insert it shows me that tree is still empty.
Your code in BVSCreate_function has undefined behavior because:
newPtr = &init; discards the allocated node and instead uses a local structure that will become invalid as soon as the function returns.
newPtr->content = &initStr; is incorrect for the same reason: you should allocate memory for the string too or possibly modify the TNodeDef to make content a string object instead of a pointer.
Function BVSInsert_function does not return the updated root pointer, hence the caller's root node is never updated. You could change the API, passing the address of the pointer to be updated.
There is also a confusion in BVSInsert_function: it should call itself recursively when walking down the tree instead of calling BVSCreate_function.
Here is a modified version:
/* Allocate the node and return 1 if successful, -1 on failure */
int BVSCreate_function(TNodef **rootPtr, function_save token) {
TNodef *newPtr = malloc(sizeof(*newPtr));
string *newStr = malloc(sizeof(*content));
if (newPtr == NULL || newStr == NULL) {
fprintf(stderr, "99");
free(newPtr);
free(newStr);
return -1;
}
newStr->str = NULL;
newStr->length = 0;
newStr->alloc = 0;
newPtr->content = newStr;
newPtr->leftPtr = NULL;
newPtr->rightPtr = NULL;
newPtr->return_type = token.ret_value;
newPtr->parameters = token.param_count;
strCpyStr(newPtr->content, token.content);
*rootPtr = newPtr;
return 1;
}
int BVSInsert_function(TNodef **rootPtr, function_save token) {
if (*rootPtr == NULL) {
return BVSCreate_function(rootPtr, token);
} else {
if (strCmpStr(token.content, rootPtr->content) < 0) {
return BVSInsert_function(&rootPtr->leftPtr, token);
} else
if ((strCmpStr(token.content, rootPtr->content)) > 0) {
return BVSInsert_function(&rootPtr->rightPtr, token);
} else {
/* function is already present: return 0 */
return 0;
}
}
}
Note also that function strCpyStr may write beyond the end of the allocated area is len2 == s1->alloc, assuming s1->len is the length of the string, excluding the null terminator.
Here is a modified version:
int strCpyStr(string *s1, const string *s2) {
int len2 = s2->length;
if (len2 >= s1->alloc) {
char *newstr = (char *)realloc(s1->str, len2 + 1);
if (newstr == NULL) {
return 1;
}
s1->str = newstr;
s1->alloc = len2 + 1;
}
strcpy(s1->str, s2->str);
s1->length = len2;
return 0;
}
map contains - returns whether or not a key exists inside the map.
mapPut - Gives a specific key a given value and adding it to the map by order, if the key exists, the value is overridden.
mapRemove - Removes a pair of (key, data) elements for which the key matches a given element (by the key compare function).
mapGetFirst - Sets the internal iterator to the first key in the map, and returns it.
MapKeyElement mapGetFirst(Map map){
if(map == NULL){
return NULL;
}
if (map->head == NULL){
return NULL;
}
map->iterator = map->head;
return (map->copyMapKeyElements(map->iterator->key));
}
mapGetNext - Advances the internal iterator to the next key and
returns it.
MapKeyElement mapGetNext(Map map){
if(map == NULL){
return NULL;
}
if((map->iterator->next)== NULL) {
return NULL;
}
map->iterator = map->iterator->next;
return (map->copyMapKeyElements(map->iterator->key));
}
typedef struct MapElements_t{
MapDataElement data;
MapKeyElement key;
struct MapElements_t* next;
} *MapElements;
struct Map_t{
copyMapDataElements copyMapDataElements;
copyMapKeyElements copyMapKeyElements;
freeMapDataElements freeMapDataElements;
freeMapKeyElements freeMapKeyElements;
compareMapKeyElements compareMapKeyElements;
MapElements head;
MapElements iterator;
};
/* ...... */
MapResult mapPut(Map map, MapKeyElement keyElement, MapDataElement dataElement) {
if ((map == NULL) || (keyElement == NULL) || (dataElement == NULL)) {
return MAP_NULL_ARGUMENT;
}
if (mapContains(map, keyElement)) {
mapRemove(map, keyElement);
}
MapElements new_map_element = malloc(sizeof(new_map_element));
if (new_map_element == NULL) {
return MAP_OUT_OF_MEMORY;
}
new_map_element->data = dataElement;
new_map_element->key = keyElement;
new_map_element->next = NULL;
if(map->head == NULL){
map->head = new_map_element;
map->iterator = map->head;
return MAP_SUCCESS;
}
mapGetFirst(map);
if (map->compareMapKeyElements(keyElement, map->iterator->key) < 0){
new_map_element->next = map->iterator;
map->head = new_map_element;
return MAP_SUCCESS;
}
while(map->iterator->next != NULL) {
if (map->compareMapKeyElements(keyElement, map->iterator->next->key) < 0) {
new_map_element->next = map->iterator->next;
map->iterator = new_map_element;
return MAP_SUCCESS;
}
mapGetNext(map);
}
map->iterator->next = new_map_element;
return MAP_SUCCESS;
}
You have typedefs that include a pointer, such as typedef struct MapElements_t{...} *MapElements; which makes the type MapElements a pointer.
This is discouraged and for the following reason:
When you do
MapElements new_map_element = malloc(sizeof(new_map_element));
you are allocating the size of a pointer, not the size of the thing pointed to. In your case you should do:
MapElements new_map_element = malloc(sizeof(*new_map_element));
but preferably you would do:
typedef struct MapElements_t
{
//...
struct MapElements_t* next;
} MapElements;
so you make a variable that is a pointer to the thing have explicitly the *.
MapElements *new_map_element = malloc(sizeof(*new_map_element));
The fault was here; I have to replace this code:
new_map_element->data = dataElement;
new_map_element->key = keyElement;
with this code:
new_map_element->data = map->copyMapDataElements(dataElement);
new_map_element->key = map->copyMapKeyElements(keyElement);
Hi I've been working on school project and need your help with the code. I use valgrind to work out what is wrong and need to get rid of the awful errors what exacly means by your thoughts
Function inserts new element into table
This is one of the errors i get
Invalid write of size 1
at 0x4C29B32: strcpy (vg_replace_strmem.c:458) by 0x401CE9: HTab_insert (ial.c:65)
by 0x4019B5: main (main.c:82) Address 0x5449785 is 0 bytes after a block of size 5 alloc'd
at 0x4C28FA4: malloc (vg_replace_malloc.c:296)
by 0x401CC9: HTab_insert (ial.c:64)
by 0x4019B5: main (main.c:82)
HTab_listitem* HTab_insert(HTab_t* ptrht, Ttoken token) {
unsigned ind = hash_function(ptrht->htable_size,token);
HTab_listitem* item_ptr = NULL;
HTab_listitem* item = ptrht->list[ind];
HTab_listitem* nextitem;
if(item == NULL) {
nextitem = malloc(sizeof(HTab_listitem)+sizeof(char)*(strlen(token.data)+1));
if(nextitem == NULL)
/*allocation error*/
return NULL;
else {
//printf("HERE\n");
//printf("%s\n", token.data);
//memcpy(nextitem->token.data,token.data,strlen(token.data)+1);
int length = strlen(token.data);
nextitem->token.data = malloc(length * sizeof((char) +2));
strcpy(nextitem->token.data,token.data);
nextitem->token.data[length] = '\0';
nextitem->token.stav = token.stav;
//printf("HERE AFTER\n");
nextitem->ptrnext = NULL;
item = ptrht->list[ind] = nextitem;
nextitem = NULL;
if(item == NULL)
return NULL;
}
}
else {
while(item != NULL) {
if(strcmp(item->token.data,token.data) == 0) {
//if found
item_ptr = item;
break;
}
else {
//next item
item_ptr = item;
item = item->ptrnext;
}
}
if(item_ptr != NULL && item != item_ptr) {
//not found insert next item
nextitem = malloc(sizeof(HTab_listitem*)+sizeof(char)*(strlen(token.data)+1));
if(nextitem == NULL)
/*allocation error*/
return NULL;
else {
//memcpy(nextitem->token.data,token.data,strlen(token.data)+1);
int length = strlen(token.data);
nextitem->token.data = malloc(length * sizeof((char) +2));
strcpy(nextitem->token.data,token.data);
nextitem->token.data[length] = '\0';
nextitem->token.stav = token.stav;
nextitem->ptrnext = NULL;
item = nextitem;
if(item == NULL)
return NULL;
item_ptr->ptrnext = item;
}
}
}
return item;
}
You have a few allocation errors that could lead to undefined behavior.
I will go into the code from top to bottom
First, you allocate to much memory
nextitem = malloc(sizeof(HTab_listitem)+sizeof(char)*(strlen(token.data)+1));
Where the following should be enough, since the nextitem->token.data is allocated afterwards.
nextitem = malloc(sizeof(HTab_listitem));
Also, when allocating token.data, use the following :
int length = strlen(token.data);
nextitem->token.data = malloc( sizeof(char) * (length + 1) );
nextitem->token.data[length] = 0;
Your second item allocation is again not the right size.
You allocate the sizeof of a pointer (8 bytes) instead of the size of your struct and adding the token.data is still not usefull.
nextitem = malloc(sizeof(HTab_listitem*)+sizeof(char)*(strlen(token.data)+1));
//Error here (HTab_listitem*)
That should be :
nextitem = malloc(sizeof(HTab_listitem));
Then again allocate token.data as done previousely.
This is my complete program:
#include <stdio.h>
#include <stdlib.h>
#define kArraySize 50
#define kFirstElement 0
struct Name {
char name[kArraySize + 1];
struct Name *nextName;
} *gFirstNameNode, *gLastNameNode;
char GetName(struct Name *currentName);
void AddToList(struct Name *currentName);
void PrintList(struct Name *gFirst);
void FreeTheMemory(struct Name *gFirst);
int main(void) {
struct Name *currentName;
char character;
int counter;
gFirstNameNode = NULL;
gLastNameNode = NULL;
do {
currentName = malloc(sizeof(struct Name));
if (currentName == NULL) {
printf("Out of Memory!\n");
exit(0);
} else {
for (counter = 0; counter <= kArraySize; counter++)
currentName->name[counter] = '\0';
}
character = GetName(currentName);
if ((currentName->name[kFirstElement]) != '\0') {
AddToList(currentName);
}
} while (character != '\r');
PrintList(gFirstNameNode);
printf("Freeing list memory");
FreeTheMemory(gFirstNameNode);
printf("\nFreeing Current Memory!\n");
free(currentName);
printf("Program Ended");
return 0;
}
char GetName(struct Name *currentName) {
char c;
int counter = 0;
printf("Enter a name (hit 'return' to exit):");
for (counter = 0; (counter <= kArraySize) && ((c = getchar()) != '\n'); counter++) {
currentName->name[counter] = c;
}
if (counter == 0) {
return '\r';
} else {
currentName->name[counter + 1] = '\0';
return '\0';
}
}
void AddToList(struct Name *currentName) {
if (gFirstNameNode == NULL) {
gFirstNameNode = currentName;
} else {
gLastNameNode->nextName = currentName;
}
gLastNameNode = currentName;
currentName->nextName = NULL;
}
void PrintList(struct Name *gFirst) {
struct Name *currentPointer;
if (gFirstNameNode == NULL) {
printf("No names in list\n");
printf("----------------\n");
} else {
for (currentPointer = gFirst; currentPointer != NULL; currentPointer = currentPointer->nextName) {
printf("Name: %s\n", currentPointer->name);
}
}
}
void FreeTheMemory(struct Name *gFirst) {
struct Name *currentPointer;
for (currentPointer = gFirst; currentPointer != NULL; currentPointer = currentPointer->nextName) {
free(currentPointer);
printf(".");
}
}
This is where I'm uncertain as to whether what I'm doing is correct. I'm teaching myself C all by myself so please bear with me. What I want to do is delete the node of the linked list from the first node till the last node. For that I created the following function. :
void FreeTheMemory(struct Name *gFirst) {
struct Name *currentPointer;
for (currentPointer = gFirst; currentPointer != NULL; currentPointer = currentPointer->nextName) {
free(currentPointer);
printf(".");
}
}
I'm not sure if by freeing the currentPointer whether I'm actually freeing the linked list. In other words I'm wondering if my logic is correct. I've got no one to ask so I would greatly appreciate some help.
You have a problem doing this:
free(currentPointer);
followed by this:
currentPointer = currentPointer->nextName
You can't deference currentPointer to get the next name after you've freed it.
Instead you should do something like:
currentPointer = gFirst;
while ( currentPointer ) {
struct Name * next = currentPointer->nextName;
free(currentPointer);
currentPointer = next;
}
this is the function that i am calling from main:
char** readTokens(char *userInput, const char *seperator)
{
char** c;
char line1[512],line2[512];
int wordCount = 0;
int index;
char* tmp;
strcpy(line1, userInput);
for (index=0;line1[index]!='\n';index++);
line1[index]='\0';
strcpy(line2,line1);
tmp = strtok(line1,seperator);
while (tmp!=NULL)
{
tmp=strtok(NULL,seperator);
wordCount = wordCount + 1;
}
if((wordCount) == ERROR)
{
return NULL;
}
c=(char**)malloc(((wordCount)+1)*sizeof(char*));
if (c == NULL)
{
printf("failed to allocate memory.\n");
return NULL;
}
tmp = strtok(line2,seperator);
index=0;
while (tmp!=NULL)
{
c[index]=(char*)malloc((strlen(tmp)+1*sizeof(char)));
if (c[index]==NULL)
{
printf("failed to allocate memory.\n");
return NULL;
}
strcpy(c[index],tmp);
tmp=strtok(NULL,seperator);
index++;
}
c[index] = NULL;//put NULL on last place
return c;
}
And this how i use it in main:
while (fgets(words, sizeof(words), filePointer) != NULL) // this line is a command of reading a line from the file.
{
/*here i am calling the function*/
array = readTokens(words, " ");
theGraph->graphEdges[index_i].sourceVertex = array[ZERO];
theGraph->graphEdges[index_i].destinationVertex = array[ONE];
theGraph->graphEdges[index_i].arcValue = atoi(array[TWO]);
for(index_j = ZERO ; index_j < vertexes ; index_j++)
{
if(theGraph->placeInTableIndex[index_j] == NULL)
{
theGraph->placeInTableIndex[index_j] = array[ZERO];
break;
}
else if(strcmp(theGraph->placeInTableIndex[index_j],array[ZERO]) == ZERO)
break;
}
for(index_j = ZERO ; index_j < vertexes ; index_j++)
{
if(theGraph->placeInTableIndex[index_j] == NULL)
{
theGraph->placeInTableIndex[index_j] = array[ONE];
break;
}
else if(strcmp(theGraph->placeInTableIndex[index_j],array[ONE]) == ZERO)
break;
}
theGraph->graphEdges[index_i+ONE].sourceVertex = array[ONE];
theGraph->graphEdges[index_i+ONE].destinationVertex = array[ZERO];
theGraph->graphEdges[index_i+ONE].arcValue = atoi(array[TWO]);
index_i+= TWO;
//freeTokens(array);
}
I tried to do free to the array in the end of the while but it not work i still have memory leak from this function (valgrind check). i am using this function to free:
void freeTokens(char** tokens)
{
while(*tokens != NULL)
{
*tokens = NULL;
free(*tokens);
*tokens++;
}
tokens = NULL;
free(tokens);
}
You're losing the original value of tokens (the thing you need to free) by incrementing it; then you set it to NULL, then try to free NULL.
Instead:
void freeTokens(char** tokens)
{
char **freeTokens = tokens;
while (*freeTokens != NULL)
{
free(*freeTokens);
*freeTokens = NULL; // not actually necessary, but must happen *after* if at all
freeTokens++;
}
free(tokens);
// tokens = NULL; // accomplishes nothing; doesn't change the caller's version
}