Ask coding questions

← Back to all posts
Memory Misallocation in C
arobbins (2)


I have been working on a project that takes an input text file (from the command line) and reads it into several different functions to produce a gradebook.

Right now, I am having alot of trouble with segmentation faults and bus errors when I attempt to read in a 2D array from the file. It will segmentation fault and even if I can somehow get it to read, when I print the values they appear to just be random memory addresses.

./repl-build.out input.txt
Joel Bob Susan Hillary John
1818586954 0 1866651408 98 0
1634956627 110 1766326272 1918987372 121
1852337994 32512 0 0 0
0 0 0 0 0
0 0 0 0 -323323816
32767 -1015621872 2 5 8
-1 -1 -323324288 32767 -1
-1 -1 -1 -573722016 21876

The functions in question fall on lines 68-95(getGrades and printGrades). If anyone has time to give any advice it would be greatly appreciated. So far I have tried to eliminate the newline char with a dump, I have tried using fgets and sscanf instead of fscanf. If I scan in each element of the grades array individually it will store and print them, but if I use the for loop it gives a segmentation fault. I have also tried not using the functions and putting the appropriate for loop right in main. I have also tried returning to pointer values as well as attempting to initialize my 2D arrays to {0} or {0,0} before running the functions.

Thank you for your time reading this far and any assistance you may be able to offer. I am completely at a loss for understanding why my program is behaving this way.

Answered by DynamicSquid (4359) [earned 5 cycles]
View Answer
DynamicSquid (4359)

I think the problem is here:

  int numStudents; 
  int numAssignments; 

  int grades[numAssignments][numStudents]; 
  char studentNames[numStudents][NAME_LENGTH]; 
  double finalGrades[numStudents];

You created a variable called numStudents, but didn't set a value to it. Then you creates an array called grades that has the size of numStudents.

You might want to create the arrays after you find out how many students and assignments there are

arobbins (2)

@DynamicSquid I thought that could have been a problem initially as well, so I did a version where I initialized both the numStudents and numAssignments to 10 but it didn't help...also it was interesting because even though I initialized the numAssignments it did not seem to carry over the the grades array.

The way it is now they will initialize to 8 and 5 respectively before I try to run the getGrades function. When I print the grades it gives me 40 different ints so I feel like it is allocating the space, even though it doesn't seem to be doing it all in the same chunk? I'm such a newb when it comes to understanding how the nitty-gritty of the language works, lol.

I am going to try your advice though and move my array declaration until after the fscanf for initializing the numStudents and numAssignments. I will let you know how it turns out.

arobbins (2)

@DynamicSquid IT WORKED!!!!!! You are awesome, thank you so much!! I can't believe it was so simple but it makes sense when thinking about weird how one way didn't do anything and then boom, switch a couple placements and completely different outcome. Thanks again, you are a day saver!!

DynamicSquid (4359)

@arobbins yeah np! glad I could help!

DynamicSquid (4359)

So you want to read data from input.txt?

arobbins (2)

@DynamicSquid yes, at least for this round :)