【发布时间】:2019-11-26 20:07:24
【问题描述】:
我有一个在 Windows 10 中编写的程序,它运行良好。 在这种情况下(我将过度简化,因为这是程序失败的地方),它需要一个包含 200 个元素(结构)的简单链表,并将前 100 个元素的数据复制到一个 100 个元素的数组(数组[100])。 该程序在 Windows 上完美运行,但它不会在 Ubuntu 上复制单个结构。 两个系统的 GCC 编译器有什么不同会导致这种情况吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
typedef struct{
int id;
char title[100];
char director[100];
char genre[100];
int likes;
int number_of_voters;
float average_rating;
int year;
int cost;
char color[100];
float ratingW;
}Movie;
struct Node{
Movie movie;
struct Node *next;
};
//Simply linked list
typedef struct{
struct Node *head;
}List;
//Array
typedef struct{
Movie movies[SIZE];
int num; //number of elements
}Array;
void Initialize(List *l);
void PrintList(List l);
void InsertNode(List *l, Movie reg);
void PrintMovie(Movie mov);
void PrintArray(Array arr);
void FromListToArray(List l, Array *arr);
int main(){
List sls;
Array a;
Initialize(&sls);
PrintList(sls); //Prints 200 movies, and shows the message "Movies loaded in the list ::: 200"
FromListToArray(sls, &a);
PrintArray(a); //Doesn't print any movie, and shows the message "Movies loaded in the array ::: 0"
return 0;
}
//Initializes the list
void Initialize(List *l){
l->head = NULL;
}
void PrintList(List l){
struct Node *p;
p = l.head;
while(p != NULL)
{
PrintMovie(p->movie);
p = p->next;
}
}
//Inserts a node at the beginning of the list
void InsertNode(List *l, Movie reg){
struct Node *r;
r = (struct Node *) malloc (sizeof(struct Node));;
if (r == NULL){
printf("No memory!\n");
}
else{
r->movie.id = reg.id;
strcpy(r->movie.title, reg.title);
strcpy(r->movie.director, reg.director);
strcpy(r->movie.genre, reg.genre);
r->movie.likes = reg.likes;
r->movie.number_of_voters = reg.number_of_voters;
r->movie.average_rating = reg.average_rating;
r->movie.year = reg.year;
r->movie.cost = reg.cost;
strcpy(r->movie.color, reg.color);
r->movie.ratingW = reg.ratingW;
r->next = l->head;
l->head = r;
}
}
/*Copies the data from a txt file into a simply linked list.
Line 1 is the id of the first movie, line 2 the title... line 10 is the color.
Line 11 is the id of the second movie, and so on... This repeated 200 times (200 movies = 2000 lines)*/
void FromTxtToList(List *l, FILE *f){
int j = 0;
char cad[100];
Movie reg;
f = fopen("movies.txt", "r");
if (f == NULL){
printf("Error, could not open the file\n");
}
else{
while(!feof(f)){
fgets(cad, 100, f);
reg.id = atoi(cad);
fgets(cad, 100, f);
strcpy(reg.title, cad);
fgets(cad, 100, f);
strcpy(reg.director, cad);
fgets(cad, 100, f);
strcpy(reg.genre, cad);
fgets(cad, 100, f);
reg.likes = atoi(cad);
fgets(cad, 100, f);
reg.number_of_voters = atoi(cad);
fgets(cad, 100, f);
reg.average_rating = atof(cad);
fgets(cad, 100, f);
reg.year = atoi(cad);
fgets(cad, 100, f);
reg.cost = atoi(cad);
fgets(cad, 100, f);
strcpy(reg.color, cad);
reg.ratingW = 0;
InsertNode(l, reg);
j++;
}
}
fclose(f);
printf("Movies loaded in the list ::: %d\n", j);
}
void PrintMovie(Movie mov){
printf("///////////////////////////////////\n");
printf("Id: %d\n", mov.id);
printf("Title: %s", mov.title);
printf("Director: %s", mov.director);
printf("Genre: %s", mov.genre);
printf("Likes: %d\n", mov.likes);
printf("Number of voters: %d\n", mov.number_of_voters);
printf("Average rating: %.2f\n", mov.average_rating);
printf("Year: %d\n", mov.year);
printf("Cost: $%d\n", mov.cost);
printf("Color or BW: %s", mov.color);
printf("Rating: %.2f\n", mov.ratingW);
printf("///////////////////////////////////\n");
}
void PrintArray(Array arr){
int i;
printf("\nArray: \n");
for(i=0; i < arr.num; i++){
PrintMovie(arr.movies[i])
}
}
void FromListToArray(List l, Array *arr){
int i = 0;
arr->num = 0;
struct Node *p;
p = l.head;
while ((p != NULL) && (arr->num < SIZE)){
if (strcmp(p->movie.color, "Color\n")==0){
//If I find a "colored" movie (color = "Color")
//Copy the data into the array
arr->movies[i].id = p->movie.id;
strcpy(arr->movies[i].title, p->movie.title);
strcpy(arr->movies[i].director, p->movie.director);
strcpy(arr->movies[i].genre, p->movie.genre);
arr->movies[i].likes = p->movie.likes;
arr->movies[i].number_of_voters = p->movie.number_of_voters;
arr->movies[i].average_rating = p->movie.average_rating;
arr->movies[i].year = p->movie.year;
arr->movies[i].cost = p->movie.cost;
strcpy(arr->movies[i].color, p->movie.color);
arr->movies[i].ratingW = p->movie.ratingW;
arr->num++;
i++;
}
p = p->next;
}
printf("Movies loaded in the array ::: %d\n", arr->num);
}
【问题讨论】:
-
也许吧,但绝大多数情况下你有一个 UB 错误:(
-
另外,'不编译'!='运行时不能正常工作'
-
@Martin James UB?
-
为了帮助您,我们需要一个minimal reproducible example 来说明问题。
-
UB 表示您正在做一些未定义的事情,而漂亮的 windows 人员对您想要它做的事情做出了假设
标签: c arrays windows list compiler-errors