Thread: Problem with malloc in C

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    4

    Lightbulb Problem with malloc in C

    Having some problems with this bit of code allocating memory correctly:

    I get to the middle of the 1st function call and during runtime I get:
    0 [main] a 3888 handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
    9946[main] a 3888 stackdump: Dumping stat trace to a.exe.stackdump

    There is a comment in the code that notates where it seems to be crashing.

    Basic concept is to ask user for name of file, allocate memory for X number of structs to hold the data of the file. Then store the file in an array of structs.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct
    {
    	char lname[30];
    	char fname[30];
    	int score;
    }data;
    
    
    data *read_file (char file[],int *numbstudents);
    
    
    int main()
    {
    	char filename[20];
    	int numbstudents;
    	data *student_list;
    	
    	printf("Please enter the name of the file containing the grades.\n");
    	scanf ("%s", filename);
    	printf("File name recorded.\n");
    	
    	student_list = read_file (filename, &numbstudents);	
    	
    	
    	free (student_list);
    	return 0;
    	
    }
    
    data *read_file(char file[], int *numbstudents)
    {
    	FILE *fp;
    	int i;
    	data *list;
    	
    		printf("Attempting to open file...\n");
    		fp = fopen(file, "r");
    		if (fp == NULL)
    		{
    			printf("File cannot be opened");
    			exit(0);
    		}
    		else
    		{
    			printf("File opened successfully.\n");
    		}
    		
    		
    		fscanf ( fp, "%d", &numbstudents);
    		printf ("Total Students: %d \n", numbstudents);
    		
    		printf ("Allocating Memory\n");\\This is where it is crashing 
    
    		
    		list = (data*)malloc((*numbstudents)*sizeof(data));
    		
    		for (i = 0; i < *numbstudents; i++)
    		{
    			fscanf( fp, "%s", list[i].lname);
    			fscanf( fp, "%s", list[i].fname);
    			fscanf( fp, "%d", list[i].score);
    		}
    		
    		fclose (fp);
    		return list;
    	
    }

    This is an example of the file to open:
    Code:
    6 
    Ball Adam 80
    Arnold Joshua 60
    Scot Michael 80 
    Cook Bret 60
    Davis Ronald 60
    Fox Brian 80
    Singh Jaadu 91
    Any assistance why this might not run would be appreciated.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    fscanf ( fp, "%d", &numbstudents);
    Since numbstudents is already a pointer, taking its address gives you a pointer to a pointer.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    4
    Thanks!
    I tried
    Code:
    fscanf (fp, "%d", numbstudents);
    It prints out that numbstudents is 37813904 instead of 6 like it should be from the example below.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    numbstudents is a pointer to an int.
    *numbstudents is an int.
    Quote Originally Posted by Dave_Sinkula
    Since numbstudents is already a pointer
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    And here you need a pointer.
    Code:
    fscanf( fp, "%d", &list[i].score);
    A good compiler ought to tell you these things.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    4
    Yeah!

    Switched

    Code:
    fscanf( fp, "%d", list[i].XXXX);
    to
    Code:
    fscanf( fp, "%d", &list[i].XXXX);

    and

    Code:
    fscanf ( fp, "%d", &numbstudents);
    printf ("Total Students: %d \n", numbstudents);
    to

    Code:
    fscanf ( fp, "%d", numbstudents);
    printf ("Total Students: %d \n", *numbstudents);
    Got to get the pointers down. Thanks Dave!!

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You shouldn't cast malloc:
    Code:
    list = (data*)malloc((*numbstudents)*sizeof(data));
    But at least you include <stdlib.h> and you free the memory.

    Code:
    9946[main] a 3888 stackdump: Dumping stat trace to a.exe.stackdump
    It appears to be GCC? If so, then turn on full warnings:
    Code:
    gcc -W -Wall -ansi -pedantic -O2 -o executable.exe source.c
    You can leave out the -o executable.exe if you want it to make a.exe.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    4
    Yes it is GCC. Thanks for the info on the full warnings. That should be helpful .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  3. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  4. freeing problem with concurrent processes
    By alavardi in forum C Programming
    Replies: 2
    Last Post: 03-07-2005, 01:09 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM