[注意我正在使用C] 我有一个问题,当我连续两次调用printf时,我得到"垃圾数据"第二次。第一个printf将打印出来#ra; rajan1"正如所料,第二个printf将打印出随机符号。我不确定它是随机的还是地址。有谁知道为什么我会收到这些结果? 这是我的代码中的一个片段,它突出了我的问题(不是连续的printf语句:
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);
}
正如我所说,输出是我第一次预期的字符串(它打印" rajan1")和第二次随机的东西(ussually symbols)。我已经在互联网上搜索了类似问题,但我还没能找到一个。我的搜索表明我可能需要"免费"某个变量,但我不知道我需要释放什么,或者可能它是我不确定的编译器版本所独有的(我使用的是默认的c编译器在visual studio 2012 ultimate v11.0.61219.00更新5)。我已经包含了所有其他代码,因为希望有人可以重现这一点。最重要的是我的问题是在什么情况下printf语句可以改变一个值?
的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
最后我用来测试它的文本文件(songs.DATA):
rajan1,summer india1,sweet summer1,pop1,21,31,01,11,
rajan2,summer india2,sweet summer2,pop2,22,32,02,12,
edit1:在header.c底部包含缺少的makeSongLength()
答案 0 :(得分:1)
while (fscanf(pF, "%100[^,],%100[^,],%100[^,],%100[^,],%2[^,],%2[^,],%5[^,],%2[^,]", &artist, &album, &song, &genre, &minutes, &seconds, &ntp, &rating) == 8)
此行会导致 UB ,就像在这些数组中一样,您没有为null character
留出空间。另外,格式说明符int
也是%d
。
改为这个 -
while (fscanf(pF, " %99[^,],%99[^,],%99[^,],%99[^,],%d,%d,%d,%d,",artist, album,song,genre,&minutes, &seconds, &ntp, &rating) == 8)
void main(void)
- &gt; int main(void)
或int main(int argc,char **argv)
不要将malloc
的结果投射到几乎所有情况下。