[Note that I am using C] I have an issue where when I call printf twice in a row I get "garbage data" the second time. The first printf will print out "rajan1" as expected, and the second printf will print out random symbols. I'm not sure if its something random or maybe an address. Does anyone know why I might be recieving these results? Here is a snippet from my code which highlights my problem (not the consecutive printf statements:
void main(void)
{
struct listNode **pStart = (struct listNode**) malloc(sizeof(struct listNode**));
load(pStart);
printf(" %s\n",*(*pStart)->data->artist);
printf(" %s\n",*(*pStart)->data->artist);
}
As I said the output is my expected string the first time(it prints "rajan1") and something random the second time(ussually symbols). I have scoured the internet for a similair problem but i haven't been able to find one. My searching indicates I may need to "free" a certain variable but I have no idea what I would need to free, or that possibly it is unique to my compiler version which I am unsure of(I am using whatever the default c compiler is in visual studio 2012 ultimate v11.0.61219.00 update 5). I have included all of my other code because hopefully someone can reproduce this. Most of all my question is under what circumstances can a printf statement change a value?
main.c
#include "Header.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(void)
{
struct listNode **pStart = (struct listNode**) malloc(sizeof(struct listNode**));
load(pStart);
printf(" %s\n",*(*pStart)->data->artist);
printf(" %s\n",*(*pStart)->data->artist);
}
header.c
#include "Header.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void load(struct listNode **temp)
{
struct listNode **head = (struct listNode**) malloc(sizeof(struct listNode**));
FILE *pF;
char artist[100];
char album[100];
char song[100];
char genre[100];
int ntp = 0;
int rating = 0;
int minutes = 0;
int seconds = 0;
pF = fopen("Songs.DATA", "r");
*temp = NULL;
*head = (struct listNode*) malloc(sizeof(struct listNode*));
while (fscanf(pF, "%100[^,],%100[^,],%100[^,],%100[^,],%2[^,],%2[^,],%5[^,],%2[^,]", &artist, &album, &song, &genre, &minutes, &seconds, &ntp, &rating) == 8)
{
*head = makeNode(makeRecord(artist, album, song, genre, makeSongLength(minutes, seconds), ntp, rating));
insertNode1(temp,head);
*temp=*head;
}
fclose(pF);
}
struct listNode * makeNode ( struct record * newData)
{
struct listNode mem;
struct listNode * pMem = &mem;
pMem = (struct listNode *) malloc (sizeof (struct listNode*));
pMem->data = (struct record *) malloc (sizeof (struct record*));
pMem->data = newData;
pMem->pNext = (struct listNode *) malloc (sizeof (struct listNode*));
pMem->pNext = pMem;
pMem->pLast = (struct listNode *) malloc (sizeof (struct listNode*));
pMem->pLast = pMem;
return pMem;
}
struct record * makeRecord(char*artist,char *album,char *song,char *genre,struct songLength *length, int ntp, int rating)
{
struct record * pMem = NULL;
pMem = (struct record *) malloc (sizeof (struct record));
*pMem->artist = (char *) malloc ((sizeof (char) *100)+1);
*pMem->artist = artist;
*pMem->album = (char *) malloc ((sizeof (char) *100)+1);
*pMem->album = album;
*pMem->song = (char *) malloc ((sizeof (char) *100)+1);
*pMem->song = song;
*pMem->genre = (char *) malloc ((sizeof (char) *100)+1);
*pMem->genre = genre;
pMem->length = (struct songLength *) malloc (sizeof (struct songLength));
pMem->length = length;
pMem->ntp = (int) malloc (sizeof (int));
pMem->ntp = ntp;
pMem->rating = (int) malloc (sizeof (int));
pMem->rating = rating;
return pMem;
}
void insertNode1(struct listNode ** old, struct listNode ** fresh)
{
if (*old == NULL)
{
(*fresh)->pLast=*fresh;
(*fresh)->pNext=*fresh;
return;
}
if ((*fresh)->data->artist <= (*old)->data->artist )
{
(*fresh)->pLast=(*old)->pLast;printf("1\n");
(*fresh)->pNext=(*old);printf("2\n");
if ((*fresh)->pLast==NULL)
{
(*fresh)->pLast==*old;
}
(*fresh)->pLast->pNext=*fresh;printf("3\n");
(*fresh)->pNext->pLast=*fresh;printf("4\n");
}
else insertNode1(&(*old)->pNext,fresh);
return;
}
struct songLength * makeSongLength(int minutes, int seconds)
{
struct songLength * pMem = NULL;
pMem = (struct songLength *) malloc (sizeof (struct songLength));
pMem->minutes = minutes;
pMem->seconds = seconds;
return pMem;
}
header.h
#ifndef Header_h
#define Header_h
void sort();
void load(struct listNode **temp);
void display(struct listNode **head);
struct listNode * makeNode ( struct record * newData);
struct record * makeRecord(char*artist,char *album,char *song,char *genre,struct songLength *length, int ntp, int rating);
struct songLength * makeSongLength(int minutes, int seconds);
void insertNode1(struct listNode ** old, struct listNode ** fresh);
/*A record is a struct type which consists of the following attributes:
* Artist – a string
* Album title – a string
* Song title – a string
* Genre – a string
* Song length – a struct type consisting of seconds and minutes, both integers
* Number times played – an integer
* Rating – an integer (1 – 5)*/
/*typedef struct record
{
char *artist[100];
char *album[100];
char *song[100];
char *genre[100];
struct songLength *length;
int ntp;
int rating;
};
typedef struct songLength
{
int minutes;
int seconds;
};
typedef struct listNode
{
struct record * data;
struct listNode * pNext;
struct listNode * pLast;
};
#endif
finally the text file which I am using to test this with(songs.DATA):
rajan1,summer india1,sweet summer1,pop1,21,31,01,11,
rajan2,summer india2,sweet summer2,pop2,22,32,02,12,
edit1: included the missing makeSongLength() at the bottom of header.c