473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Oct 14 '06 #1
17 2482
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
Oct 15 '06 #2
dtimes6
73
what compiler are you using ? gcc can not work in that way.
Oct 16 '06 #3
Banfa
9,065 Expert Mod 8TB
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.
Oct 16 '06 #4
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.
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.
Oct 16 '06 #5
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.

what compiler are you using ? gcc can not work in that way.
Oct 16 '06 #6
tyreld
144 100+
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.
Oct 16 '06 #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.
here is a copy paste of some part of my code

int *D;

void init()
{
int sizealloc, sch_size,node_size, i;
sizealloc = (2*nodes+2)*(sizeof(int));
node_size = nodes*nodes *(sizeof(int));
D = (int *) malloc(node_size);

....
}
Oct 16 '06 #8
tyreld
144 100+
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.
Oct 16 '06 #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.
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.
Oct 16 '06 #10
tyreld
144 100+
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?
Oct 16 '06 #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?
Thanks for the tips. The program returns a message which I think was generated by the software:

Allocation of memory failed;
Oct 16 '06 #12
tyreld
144 100+
Are you using the stdlib version of malloc, or are you using a malloc implementation from some sort of seperate c/c++ framework?
Oct 16 '06 #13
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.
Oct 16 '06 #14
tyreld
144 100+
Are the size of your memory allocations constant over time? Is this the only dynamic memory allocation taking place? Are you using any recursion?
Oct 16 '06 #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?
Hi again,

Yes, some of the functions are using recursion. This is not the only memory allocation and this one uses a constant memory over time, but some other memory allocations vary over time.
Oct 17 '06 #16
tyreld
144 100+
Hi again,

Yes, some of the functions are using recursion. This is not the only memory allocation and this one uses a constant memory over time, but some other memory allocations vary over time.
I would suggest looking at your other memory allocations, and making sure that they are not leaking memory. It sounds like the one you suspect is not the cause. You also should try and determine how deep your recursion is going. Are there large data types local to your recursive functions? The problem with recursion is that deep recursion with large local data can exhaust stack memory as each recursive call requires a stack frame as well as local data being pushed on to the stack, and this data stays on the stack until the return call.
Oct 17 '06 #17
Hi,

Although ur post still doesnt clear the exact nature of your problem, but according to the inadequate threads that hve been goin on, i would like to note the following points:
1. If ur Pointer D is a global one then to free off the pointer inside a local function is a bad practice for u should always free memory in "its scope".
2. Have u tried off with simulataneous alloc and free pipars inside a local function itself? Say u alloc ur pointer thru a function that gets the size passed, and use ur pointer and then free it in the same scope. although i aint sure that tis would work, but an effective way to avoid future troubles. :)
Oct 20 '06 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: john smith | last post by:
Can someone please explain to me what is happening when I do a malloc(0). This is what I did. int* p = (int*)malloc(0); Then I printed the value of p and of course it was non-null. But...
34
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
20
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
15
by: Martin Jørgensen | last post by:
Hi, I have a (bigger) program with about 15-30 malloc's in it (too big to post it here)... The last thing I tried today was to add yet another malloc **two_dimensional_data. But I found out that...
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
40
by: Why Tea | last post by:
What happens to the pointer below? SomeStruct *p; p = malloc(100*sizeof(SomeStruct)); /* without a cast */ return((void *)(p+1)); /* will the returned pointer point to the 2nd...
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
23
by: raphfrk | last post by:
I am having an issue with malloc and gcc. Is there something wrong with my code or is this a compiler bug ? I am running this program: #include <stdio.h> #include <stdlib.h> typedef...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.