C -Element of a structure changes value after printf() call -
[note using c] have issue when call printf twice in row "garbage data" second time. first printf print out "rajan1" expected, , second printf print out random symbols. i'm not sure if random or maybe address. know why might recieving these results? here snippet code highlights problem (not 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 said output expected string first time(it prints "rajan1") , random second time(ussually symbols). have scoured internet similair problem haven't been able find one. searching indicates may need "free" variable have no idea need free, or possibly unique compiler version unsure of(i using whatever default c compiler in visual studio 2012 ultimate v11.0.61219.00 update 5). have included of other code because can reproduce this. of question under circumstances can printf statement change 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 struct type consists of following attributes: * artist – string * album title – string * song title – string * genre – string * song length – struct type consisting of seconds , minutes, both integers * number times played – integer * rating – 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 text file using test 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 missing makesonglength() @ bottom of header.c
while (fscanf(pf, "%100[^,],%100[^,],%100[^,],%100[^,],%2[^,],%2[^,],%5[^,],%2[^,]", &artist, &album, &song, &genre, &minutes, &seconds, &ntp, &rating) == 8)
this line cause ub in these array don't left space null character
.also format specifier read int
%d
. change -
while (fscanf(pf, " %99[^,],%99[^,],%99[^,],%99[^,],%d,%d,%d,%d,",artist, album,song,genre,&minutes, &seconds, &ntp, &rating) == 8)
void main(void)
-> int main(void)
or int main(int argc,char **argv)
don't cast result of malloc
did in every case.
Comments
Post a Comment