Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

URGENT! What is wrong with this code?

Posted on 2002-03-08
2
Medium Priority
?
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
nebeker earned 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

688 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