Solved

URGENT! What is wrong with this code?

Posted on 2002-03-08
2
189 Views
Last Modified: 2010-04-15
I am doing a project and the following is one part of my code.

#include <stdio.h>
#include <stdlib.h>

#define NO_VOTERS 2977
#define NO_MOVIES 1627

struct Person {
  short id;
/*  char zipcode[6];  */
  short age;
  char gender;
};

struct Movie {
/*  char theatre_status[8];
  char video_status[8]; */
  short id;
  short action;
  short animation;
  short art_foreign;
  short classic;
  short comedy;
  short drama;
  short family;
  short horror;
  short romance;
  short thriller;
};

struct Vote {
  short person_id;
  short movie_id[NO_MOVIES];
  float score[NO_MOVIES];
};

int main(int argc, char** argv) {
  FILE *infile1, *infile2, *infile3, *outfile;
  struct Person voter[NO_VOTERS];
  struct Movie movie[NO_MOVIES];
  struct Vote vote[NO_VOTERS];
  short last_read_person_id, person_id, movie_id;
  float score;
  short notfound;
  short movietype[10];

  int i, j, k, m;
  float t;

  fprintf(stderr, "Opening file...\n");
  if ((infile1=fopen("person_simplified.txt", "r"))==NULL) {
    fprintf(stderr, "Error opening file: person_simplified.txt\n");
    exit(-1);
  }  if ((infile2=fopen("movie_simplified.txt", "r"))==NULL) {
    fprintf(stderr, "Error opening file: movie.txt\n");
    exit(-1);
  }
  if ((infile3=fopen("vote1_simplified.txt", "r"))==NULL) {
    fprintf(stderr, "Error opening file: vote1.txt\n");
    exit(-1);
  }
  if ((outfile=fopen("vote.nominal.arff", "w"))==NULL) {
    fprintf(stderr, "Error opening file: vote1.txt\n");
    exit(-1);
  }
  fprintf(stdout, "Initialization...\n");
  for (i=0; i<NO_VOTERS; i++) {
    for (j=0; j<NO_MOVIES; j++) {
      vote[i].movie_id[j]=-1;
      vote[i].score[j]=-1;
    }
  }

  return 0;
}

I am getting "Segmentation fault" when getting into the for loop. Can someone helps me?
0
Comment
Question by:Peter Kwan
2 Comments
 
LVL 5

Accepted Solution

by:
nebeker earned 100 total points
ID: 6852379
When I tried your program, I got a stack overflow exception.  Essentially, the "automatic" variables you're declaring at the beginning of your program are being allocated on the stack (instead of the heap), and they're just too big.

The variable: "struct Vote vote[NO_VOTERS];" alone is around 9 MB in size....

You shouldn't EVER declare large variables on the stack - always get them on the heap (via malloc).  If you don't believe me, just delete ALL of your code after the statement:

 float t;

and put this:

 printf( "Hello!\n" );
 exit(0);

and you'll still get the segmentation fault.

Reorganize the larger variables:

 struct Person voter[NO_VOTERS];
 struct Movie movie[NO_MOVIES];
 struct Vote vote[NO_VOTERS];

to be pointers, and then allocate their storage with malloc, and the problem will go away.
0
 
LVL 16

Author Comment

by:Peter Kwan
ID: 6852380
Thanks for your comment. I am going to try out your method and tell you the results later.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now