1

I'm very new to C++, so this code is just basically C compiled with g++. It works when I compile it on Windows but it does a core dump on Linux. Can anyone see what I'm doing wrong?

Here is more of the code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "student.h"

#define MAX_LINE_LENGTH 200
#define DELIMITER ','

struct _node
{
    StudentRecord student;
    struct _node  *next;
};
typedef struct _node Node;

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        fprintf(stderr, "Usage: demo studentdata_2.txt\n");
        return 1;
    }

    FILE* fp = fopen("studentdata_2.txt", "r");
    if (!fp)
    {
        fprintf(stderr, "Cannot read file\n");
        return 2;
    }

    char  line[MAX_LINE_LENGTH + 1];

    Node *head = 0; 
    Node *latest;

    fgets(line, MAX_LINE_LENGTH, fp);
    printf("reaches here\n");
    while (!feof(fp))
    {
        if (strlen(line) >= 3)
        {

            Node *current = (Node*)malloc(sizeof(Node));
            current->next = 0;
            if (!head) 
                head = current;
            else
                latest->next = current;
            latest = current;

            parseStudent(line, DELIMITER, &current->student);
        }

        fgets(line, MAX_LINE_LENGTH, fp);
    }
    fclose(fp);
    printf("doesn't reach here\n");

    int count = 0;
    float sum = 0;
    Node* ptr = head;
    while (ptr)
    {
        count++;
        sum += ptr->student.m_score;
        ptr = ptr->next;
    }
    float average = sum / count;

    ptr = head;


    ptr = head;
    while (ptr)
    {
        releaseStudent(&ptr->student);

        Node* nxt = ptr->next;

        free(ptr);

        ptr = nxt;
    }

    return 0;
}
user2817749
  • 134
  • 9
  • Why is this tagged as `C` and `C++`? They are different languages. If you're only using C constructs, why are you compiling it with g++? – Jonathon Reinhart Sep 26 '13 at 03:17
  • Can you give a full source file including #includes please? – Frankie Robertson Sep 26 '13 at 03:18
  • I did not see a `free` in your code. why did you free the memory allocated by malloc? – CS Pei Sep 26 '13 at 03:19
  • 6
    You should run your program in a debugger, or using a tool such as [Valgrind](http://valgrind.org/) to find memory problems. – Some programmer dude Sep 26 '13 at 03:22
  • [“while( !feof( file ) )” is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) may be useful to you - your `if (strlen(line) >= 3)` is a hack to get round it being wrong. This has nothing to do with your actual question, however. – Ken Y-N Sep 26 '13 at 03:26
  • Post `parseStudent()` and `releaseStudent()`, particularly the latter. – Crowman Sep 26 '13 at 03:41
  • Use "rt" in `fopen("studentdata_2.txt", "r");` – chux - Reinstate Monica Sep 26 '13 at 03:45
  • You nicely put in `printf("reaches here\n");` , why not put in `printf("%zu '%s'\n", strlen(line), line);` just before `parseStudent()` and let us/you know the last line read? – chux - Reinstate Monica Sep 26 '13 at 03:52
  • Nothing to do with your problem, but names starting with an underscore at file scope are reserved, so `struct _node` is not allowed. – Crowman Sep 26 '13 at 03:54
  • Add `fflush(stdout)` after `printf("doesn't reach here\n");` or you can't be sure that this line is never reached or **better**, use a debugger ! – Michael M. Sep 26 '13 at 07:15

0 Answers0