So i wrote a very simple program which includes a linked list,
the nodes within the list are dynamically allocated using malloc(), the program had 2 functions, add a new node to the list and print the nodes in the list.
Every time i added more than 2 nodes to the list(including the start node) and used the print function the program crashed.
With some if and print statements i found that the third (or which ever was last) node's *next was not NULL even though obviously it was not pointing at anything since it was the last node in the list.
The program crashed because there is a while loop that depended on that pointer being NULL to stop.
Anyway, changing the size of the array to 12 fixed the problem.
My question is why? I included the entire program if your interested
but it's kinda messy.
Code:
typedef struct example{
char array[52];
struct example *next;
}typeExample;
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//http://cboard.cprogramming.com/c-programming/167968-please-need-help-linked-list.html#post1239483
typedef struct LSexample{
char name[12]; // name was too big that was the problem
float decimals;
struct LSexample *forward;
}typeLS;
typeLS *returnPointerToAllocatedMemory(typeLS *MainPointers){
char UserInput5[16];
fgets(UserInput5, 15,stdin);
typeLS *PointertoAllocatedMemory = malloc(sizeof(typeLS));
sscanf(UserInput5,"%s %f",PointertoAllocatedMemory->name,&PointertoAllocatedMemory->decimals );
printf("Name:%s float:%f\n",PointertoAllocatedMemory->name,PointertoAllocatedMemory->decimals);
if(MainPointers != NULL){//if there is somethign behind then set
// whats behind pointer to point at the newly created structure
MainPointers->forward = PointertoAllocatedMemory;//mainpointers is the previous structure
// if there isn't one it will be NULL
}
return PointertoAllocatedMemory;
}
void cleanUp(typeLS *FirstStructure1){
typeLS *freeThisAllocatedMemory = NULL;
typeLS *HoldPointerForward = NULL;
freeThisAllocatedMemory = FirstStructure1;
while(freeThisAllocatedMemory != NULL){// if ? REALLY? LOOOOOOOL
printf("Free name:%s float %f\n",freeThisAllocatedMemory->name,freeThisAllocatedMemory->decimals);
HoldPointerForward = freeThisAllocatedMemory->forward; //if ->forward was bellow
free(freeThisAllocatedMemory); //remember its cleaning the pointer not the struct
freeThisAllocatedMemory = HoldPointerForward; //i mean here, then the cleaned pointer woudl get dirty again
}
}
void printfunc(typeLS *FSpointer){
typeLS *holder3 = FSpointer;
while(holder3 != NULL){
printf("name:%s float:%f\n",holder3->name,holder3->decimals);
holder3 = holder3->forward;
if(holder3 != NULL){
}
}
}
int main(){
typeLS *Firststructure = NULL;//this pointer will point at the first structure in the list
typeLS *holderr = NULL; //this pointer will point at multiple things which is the trick of this logic
char UserInput[16];
char translation[16];
while(fgets(UserInput, 15,stdin)){
sscanf(UserInput,"%s",translation);
if(strncmp(translation, "add",3)==0){
if(Firststructure == NULL){
Firststructure = returnPointerToAllocatedMemory(NULL);
holderr = Firststructure;
}else{
holderr = returnPointerToAllocatedMemory(holderr);
}
}else if(strncmp(translation, "print",5)==0){
printf("Printing...\n");
printfunc(Firststructure);
}else if(strncmp(translation, "quit",4)==0){
printf("Breaking...\n");
break;
}
}
cleanUp(Firststructure);
return 0;
}