malloc

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • farshid
    New Member
    • Oct 2006
    • 9

    malloc

    I have written a long program with c, and am using dynamic memory allocation. This program is supposed to be run over and over (300 times) for a long simulation. But the program stops after 120 cycles due to memory leackage. I am not very expert in programing but it seems that my free() function does not do anything to my program. One example of how I do memory allocation and free is this:

    int *D;
    D= (int *) malloc(size).

    void myfunction{

    I use D here;
    free(D);
    ...
    }

    void main()
    {
    ...

    myfunction();

    }

    When I disable "free();" there is no difference in memory consumption, and it stops at cycle 120 again. Is there anything wrong with the way I allocate memory to D or free it.

    Thanks and looking forward to hearing from you soon.

    Farshid
  • farshid
    New Member
    • Oct 2006
    • 9

    #2
    malloc

    Hello,

    I have written a long program with c, and am using dynamic memory allocation. This program is supposed to be run over and over (300 times) for a long simulation. But the program stops after 120 cycles due to memory leackage. I am not very expert in programing but it seems that my free() function does not do anything to my program. One example of how I do memory allocation and free is this:

    int *D;
    D= (int *) malloc(size); //D is a global pointer for some functions

    void myfunction()
    {

    //I use "D" here and some other fucntions which are called here.

    //then I free D.


    free(D);
    ...
    }

    void main()
    {
    ...

    myfunction();

    }

    When I disable "free();" there is no difference in memory consumption, and it stops at cycle 120 again. Is there anything wrong with the way I allocate memory to D or free it?

    Thanks and looking forward to hearing from you soon.

    Farshid

    Comment

    • dtimes6
      New Member
      • Oct 2006
      • 73

      #3
      what compiler are you using ? gcc can not work in that way.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Please don't double post.

        This line

        D= (int *) malloc(size); //D is a global pointer for some functions

        is invalid outside a function, this wont compile.

        Comment

        • farshid
          New Member
          • Oct 2006
          • 9

          #5
          Sorry,
          D= (int *) malloc(size); is in a function called init(). But the pointer declaration "int *D" is not in a fucntion. The program works, but it stops sooner than I need to.
          Originally posted by Banfa
          Please don't double post.

          This line

          D= (int *) malloc(size); //D is a global pointer for some functions

          is invalid outside a function, this wont compile.

          Comment

          • farshid
            New Member
            • Oct 2006
            • 9

            #6
            I use gcc. There was a mistake in that example. actually the line D= (int *) malloc(size); is inside a function called init(). the rest are ok. I don't know why my program eats up the memory. whether or not I free the memory it uses all of it very fast.

            Originally posted by dtimes6
            what compiler are you using ? gcc can not work in that way.

            Comment

            • tyreld
              New Member
              • Sep 2006
              • 144

              #7
              Can you please post the chunck of code from this init function? In particular I'm curious where you define the size value you are passing to malloc.

              Comment

              • farshid
                New Member
                • Oct 2006
                • 9

                #8
                Originally posted by tyreld
                Can you please post the chunck of code from this init function? In particular I'm curious where you define the size value you are passing to malloc.
                here is a copy paste of some part of my code

                int *D;

                void init()
                {
                int sizealloc, sch_size,node_s ize, i;
                sizealloc = (2*nodes+2)*(si zeof(int));
                node_size = nodes*nodes *(sizeof(int));
                D = (int *) malloc(node_siz e);

                ....
                }

                Comment

                • tyreld
                  New Member
                  • Sep 2006
                  • 144

                  #9
                  What platform are you compiling running this on? There are various malloc debuggers available for tracing memory leaks and various other memory management errors. Valgrind and ElectricFence are two such examples available on Linux.

                  Comment

                  • farshid
                    New Member
                    • Oct 2006
                    • 9

                    #10
                    Originally posted by tyreld
                    What platform are you compiling running this on? There are various malloc debuggers available for tracing memory leaks and various other memory management errors. Valgrind and ElectricFence are two such examples available on Linux.
                    oh, I don't know. I am using a c/c++ based software for simulating networks. The software is installed on windows xp, and I do not know how to trace memory leaks.

                    Comment

                    • tyreld
                      New Member
                      • Sep 2006
                      • 144

                      #11
                      You can start by simply adding some debugging printf's. Insert one before each call to malloc printing out the node_size being requested. Insert one before every free operation making sure you have a matching number of free calls for every malloc. The goal is to see how much memory you are requesting at each call, and to ensure you are properly releasing the memory before you allocate new memory.

                      Also, how is the program failing? Is malloc eventually returning a NULL value, or is the program crashing?

                      Comment

                      • farshid
                        New Member
                        • Oct 2006
                        • 9

                        #12
                        Originally posted by tyreld
                        You can start by simply adding some debugging printf's. Insert one before each call to malloc printing out the node_size being requested. Insert one before every free operation making sure you have a matching number of free calls for every malloc. The goal is to see how much memory you are requesting at each call, and to ensure you are properly releasing the memory before you allocate new memory.

                        Also, how is the program failing? Is malloc eventually returning a NULL value, or is the program crashing?
                        Thanks for the tips. The program returns a message which I think was generated by the software:

                        Allocation of memory failed;

                        Comment

                        • tyreld
                          New Member
                          • Sep 2006
                          • 144

                          #13
                          Are you using the stdlib version of malloc, or are you using a malloc implementation from some sort of seperate c/c++ framework?

                          Comment

                          • farshid
                            New Member
                            • Oct 2006
                            • 9

                            #14
                            Originally posted by tyreld
                            Are you using the stdlib version of malloc, or are you using a malloc implementation from some sort of seperate c/c++ framework?
                            I have tried malloc from stdlib before, eventhough I use the malloc implementation of the software now, but there is no difference when I convert them to malloc of stdlib.

                            by the way, I counted the allocation and free calls and they were both equal.

                            Comment

                            • tyreld
                              New Member
                              • Sep 2006
                              • 144

                              #15
                              Are the size of your memory allocations constant over time? Is this the only dynamic memory allocation taking place? Are you using any recursion?

                              Comment

                              Working...