1

我是该站点的新手,我正在寻找有关存储 Mp3 与艺术家姓名、歌曲名称、专辑名称、日期和运行时间的双向链接列表的帮助。如果您能帮助我,我将不胜感激。根据 GDB,我在 add 函数中遇到了段错误。

警告也如下:

Description Resource    Path    Location    Type
'data' is used uninitialized in this function [-Wuninitialized] Mp3.c   /OperatingSystemsLab1   
line 7  C/C++ Problem
assignment makes pointer from integer without a cast [enabled by default]   Mp3.c   /OperatingSystemsLab1   
line 7  C/C++ Problem
implicit declaration of function 'malloc' [-Wimplicit-function-declaration] Mp3.c   /OperatingSystemsLab1   
line 21 C/C++ Problem
assignment makes pointer from integer without a cast [enabled by default]   Mp3.c   /OperatingSystemsLab1   
line 8  C/C++ Problem
implicit declaration of function 'free' [-Wimplicit-function-declaration]   Mp3.c   /OperatingSystemsLab1   
line 40 C/C++ Problem
incompatible implicit declaration of built-in function 'malloc' [enabled by default]    Mp3.c   /OperatingSystemsLab1   
line 21 C/C++ Problem
incompatible implicit declaration of built-in function 'malloc' [enabled by default]    Mp3.c   /OperatingSystemsLab1   
line 57 C/C++ Problem
incompatible implicit declaration of built-in function 'free' [enabled by default]  Mp3.c   /OperatingSystemsLab1   
line 40 C/C++ Problem

我的代码如下

#include <stdio.h>
#include "Mp3.h"



void add(struct MP3 *pointer, char artistname, char albumname, char songname, int date, int runtime){
    /*make structure for new data*/
struct MP3 *data;
data->artistName = artistname;
data->albumName = albumname;
data->date=date;
data->runTime=runtime;
data->next=NULL;
data->prev=NULL;

if(pointer->next==NULL) {
    pointer->next = data;
}
else {
        while(pointer->next != NULL){
        pointer = pointer->next;
    }
    pointer->next = (struct MP3*)malloc(sizeof(struct MP3));
    pointer->next = data;
    struct MP3 *temp = pointer;
    pointer = pointer->next;
    pointer->prev = temp;
}
};



void delete(struct MP3 *pointer, char *artistname){
while(pointer->next!=NULL && (pointer->next)->artistName!=artistname){
    pointer = pointer->next;
}
if(pointer->next==NULL){
    return;
}
struct MP3 *temp;
temp = pointer->next;
pointer->next = temp->next;
pointer->next->prev = pointer;
free(temp);
};



void print(struct MP3 *pointer){
if(pointer==NULL){
    return;
}
printf("Artist name: %s \n", pointer->artistName);
printf("Album name: %s \n", pointer->albumName);
printf("Title: %s \n", pointer->songName);
printf("Date: %d \n", pointer->date);
printf("Runtime: %d\n", pointer->runTime);

print(pointer->next);
};



int main(){
struct MP3 *start,*current;
start = (struct MP3 *)malloc(sizeof(struct MP3));
current = start;
current->next = NULL;
current->prev = NULL;

printf("1. Add\n");
printf("2. Delete\n");
printf("3. Print\n");
while(1)
{
    int query;
    scanf("%d",&query);
    if(query==1)
    {
        int rt,d;
        char artn,albn,sn;
        scanf("%c %c %c %d %d",&artn,&albn,&sn,&d,&rt);
        add(start,artn,albn,sn,d,rt);
    }
    else if(query==2)
    {
        char artn;
        scanf("%c",&artn);
        delete(start,&artn);
    }
    else if(query==3)
    {
        printf("The list is ");
        print(start->next);
        printf("\n");
    }
}
};

和头文件

#ifndef MP3_H_
#define MP3_H_

struct MP3{
char *artistName;
char *albumName;
char *songName;
int date;
int runTime;
struct MP3 *next;
struct MP3 *prev;

};


#endif /* MP3_H_ */

就像我说的,我是新人,所以请怜悯我。任何帮助将不胜感激!谢谢!

4

3 回答 3

2

You have a problem here

struct MP3 *data;
data->artistName = artistname;

you declare a pointer to MP3 but you do not allocate memory for it. Use malloc to allocate the space or just use a plain instance instead.

You also have a problem here

 pointer->next = (struct MP3*)malloc(sizeof(struct MP3));

this allocates memory and sets pointer->next to point to it. Then in the next line

 pointer->next = data;

you reassign pointer->next and so that allocated memory in the previous line no longer has its address stored by any variable - the result of this is a memory leak.

于 2012-09-02T22:30:05.033 回答
1

struct MP3 *data;

declares a pointer to a data structure, but it doesn't create the structure. So when you try to access it, you are accessing unallocated memory, which causes a segmentation fault. You need to allocate the memory using malloc:

struct MP3 *data; /* declares a pointer to a MP3 structure */
data = malloc(sizeof(MP3)); /* creates a new MP3 structure and makes the pointer point to it */
data->artistName = artistname; /* the structure can now be used */
于 2012-09-02T22:29:10.950 回答
1

正如错误消息所说 -"'data' is uninitialized in this function"这显然是正确的。您声明了一个指向名为“data”的 MP3 结构的指针。您永远不会通过分配内存和初始化指向该内存的指针来初始化指针。

于 2012-09-02T22:31:00.287 回答