Thread: malloc() and free()

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    20

    malloc() and free()

    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:

    Code:
    programname <address of first binary file> <address of second binary file>
    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.

    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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rlesko
    I believe my problem most likely lies in my usage of malloc, which is why I'm posting here.
    Post the smallest and simplest compilable version of your program that demonstrates the problem.

    Quote Originally Posted by rlesko
    Ive heard before you should free() whatever pointers you create by using malloc(), but I am confused about what exactly free() does.
    Yes, you should, but only when you are done with whatever objects the pointers point to.

    Quote Originally Posted by rlesko
    And when I have repeated uses of malloc(), does that try to use the same portions of memory over and over?
    If you have not used free(), that should not happen, otherwise you would have problems where two distinct objects mysteriously turn out to be the same object.

    Quote Originally Posted by rlesko
    Or does it just find available memory anywhere in the computer?
    This depends on the implementation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by rlesko
    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.
    You shouldn't call free() until you're done with the allocated memory.
    e.g.:
    Code:
    char *dynstring = malloc(100);
    strcpy(dynstring, "this is my dynamically allocated string buffer.");
    puts(dynstring);
    free(dynstring);
    Once the memory is free()'d, you should not ever touch that memory again. Like, this is bad:
    Code:
    char *dynstring = malloc(100);
    free(dynstring);
    strcpy(dynstring, "This is very very bad.");
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    May I PM either of you my code? I don't want to post it all online, its not very long but it is for a project and my teacher does not want us making our things publicly available online.

    But pretty much, I've gathered that I do not want to free() my pointers since I use them throughout the program, so thank you for that information.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Sure, whatever.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    Quote Originally Posted by itsme86 View Post
    Sure, whatever.
    Sorry, just sent it over, let me know what you come up with.

    Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc calloc and free
    By -EquinoX- in forum C Programming
    Replies: 27
    Last Post: 03-26-2009, 10:59 AM
  2. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  3. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  4. Ask about free funtion using with malloc
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2002, 04:43 PM
  5. Malloc and Free.....
    By heljy in forum C Programming
    Replies: 5
    Last Post: 04-14-2002, 09:17 PM