So far, I have a program written that reads in binary files filled with floats, and then dynamically allocates space for a 2-dimensional array of those numbers (I'm programming matrix operations). This allows me to read in the numbers of the binary file, then convert them into a matrix and reference them in the convenient way of typing array[i][j].
I have a main function that calls two other functions, read and print (print is solely for checking if I am reading the correct values). My program usage is as follows:
So, my program reads and prints out the first binary file successfully, does not matter what file I choose to put first. The problem lies in reading the second file. My program will read and print out the first binary file into a matrix properly, but I always get a segmentation fault immediately after this (it should be clear that I read and print the first file successfully, the numbers are printed on the screen, then "Segmentation Fault" is printed below my first matrix), which I know is caused by the computer trying to access locations in memory it should not.Code:programname <address of first binary file> <address of second binary file>
I believe my problem most likely lies in my usage of malloc, which is why I'm posting here. Ive heard before you should free() whatever pointers you create by using malloc(), but I am confused about what exactly free() does. When I am allocating memory for the pointers, aren't I going to continue to need the pointers throughout the rest of my program to continue the reference the 2-d array I made? Ultimately, I would like to have two arrays so I may add, subtract, or multiply them.
And when I have repeated uses of malloc(), does that try to use the same portions of memory over and over? Or does it just find available memory anywhere in the computer? Sorry, I know this was quite a lot to read and it was probably confusing, if anything is unclear, please ask me. All help is appreciated. Thank you.