I am implementing an Ant Colony Optimization for the Set Covering Problem in C. In my code, I have found a function that causes a memory leak. I am pretty sure this function is the cause of the memory leak, since I have ruled out other functions by testing. Only, I don't understand why this function introduces a memory leak.
To understand this function, I'll describe the Ant struct first. The Ant struct looks like this:
struct Ant {
int* x;
int* y;
int fx;
int** col_cover;
int* ncol_cover;
int un_rows;
double* pheromone;
}
typedef struct Ant ant_t;
The pointers in this struct (such as x, y, col_cover, etc.) are initialized using malloc and freed at the end of the program. Now, the function causing the memory leak is the following:
void localSearch(ant_t* ant) {
int improvement = 1;
ant_t* antcpy = (ant_t*) malloc(sizeof(ant_t));
initAnt(antcpy);
copyAnt(ant, antcpy);
while (improvement) {
improvement = 0;
for (int i = 0; i < inst->n; i++) {
if (antcpy->x[i]) {
removeSet(inst, antcpy, i);
while (!isSolution(antcpy)) {
constructSolution(antcpy);
}
if (antcpy->fx < ant->fx) {
copyAnt(antcpy, ant);
improvement = 1;
eliminate(ant);
} else {
copyAnt(ant, antcpy);
}
}
}
}
free((void*) antcpy);
}
First, I create another instance of the Ant struct (antcpy), using the initAnt function. The copyAnt function does a deep copy of one Ant struct to another Ant struct. My reason for doing a deep copy is the following; I am changing the antcpy and then comparing it to ant. If it turns out better (antcpy->fx < ant->fx), ant is replaced by antcpy. If it turns out worse, antcpy is restored to the values of ant.
These functions are given below:
void initAnt(ant_t* ant) {
ant->x = (int*) malloc(inst->n * sizeof(int));
ant->y = (int*) malloc(inst->m * sizeof(int));
ant->col_cover = (int**) malloc(inst->m * sizeof(int*));
ant->ncol_cover = (int*) malloc(inst->m * sizeof(int));
ant->pheromone = (double*) malloc(inst->n * sizeof(double));
for (int i = 0; i < inst->m; i++) {
ant->col_cover[i] = (int*) malloc(inst->ncol[i] * sizeof(int));
}
}
void copyAnt(ant_t* from, ant_t* to) {
to->fx = from->fx;
to->un_rows = from->un_rows;
for (int i = 0; i < inst->n; i++) {
to->x[i] = from->x[i];
to->pheromone[i] = from->pheromone[i];
}
for (int i = 0; i < inst->m; i++) {
to->y[i] = from->y[i];
to->ncol_cover[i] = from->ncol_cover[i];
for (int j = 0; j < inst->ncol[i]; j++) {
to->col_cover[i][j] = from->col_cover[i][j];
}
}
}
I don't really see why this code causes a memory leak, since I free antcpy at the end of the localSearch function. So, why does this code introduce a memory leak and how can I fix it?
You will have to implement a function freeAnt that before free((void*) antcpy); will release all memory that was allocated in initAnt.
void freeAnt(ant_t* ant) {
for (int i = 0; i < inst->m; i++) {
free(ant->col_cover[i]);
}
free(ant->pheromone);
free(ant->ncol_cover);
free(ant->col_cover);
free(ant->y);
free(ant->x);
}
Related
I am working on a project that I have made use of Calloc and I am trying to free the memory at the end of my main{} function. However, after the program finishes running and I click on the stop, I get this "proj.exe has triggered a breakpoint."
at this set of codes:
while (freeSpace != NULL) {
free(freeSpace++);
}
Here are my codes for allocating memory:
scanf("%d", &SEG);
BLOCKS = 128 / SEG;
for (int k = 0; k < BLOCKS; k++)
{
memory = (int *)calloc(BLOCKS, sizeof(int));
// handle memory allocation failure
}
for (int i = 0; i < BLOCKS; i++)
{
memory[i] = (int *)calloc(SEG, sizeof(int));
// handle memory allocation failure
}
for (int l = 0; l < BLOCKS+5; l++)
{
//freeSpace = (int*)malloc(l * sizeof(int));
freeSpace = (int *)calloc( BLOCKS + 5, sizeof(int));
// handle memory allocation failure
}
for (int o = 0; o < BLOCKS; o++)
{
memorySpace = (int *)calloc(BLOCKS, sizeof(int));
// handle memory allocation failure
}
`
This is the part where I free my memory:
while (freeSpace != NULL) {
free(freeSpace++);
}
Can someone please assist me?
Its really hard to fix your issue because it seems like that these are only a part of your code or it is a prototype?
The following code only based on some 'guess':
int main()
{
scanf("%d", &SEG);
BLOCKS = 128 / SEG;
int* memory = (int *)calloc(BLOCKS+1, sizeof(int*)); // the last is a 'NULL' pointer
memset(memory, 0, sizeof(int*)*BLOCKS+1);
for (int i = 0; i < BLOCKS; i++)
{
memory[i] = (int *)calloc(SEG, sizeof(int));
// handle memory allocation failure
}
while (memory != NULL) {
free(memory++);
}
}
I think you are misunderstanding how pointers work when allocating arrays. Your very first loop has a memory leak in it because you are changing what I am assuming the "memory" pointer is pointing to.
To allocate an array in C would be like below.
int *bigspace;
bigspace = malloc(20 * sizeof(int));
Here we made the pointer "bigspace" point to a new chunk of memory 20 times the size of an integer which is an array. If you did something like below..
int *bigspace;
bigspace = malloc(20 * sizeof(int));
bigspace = malloc(20 * sizeof(int));
This would be no bueno. We are first making "bigspace" pointer point to a chunk of memory (array of ints) and then we are changing what "bigspace" is pointing to by making it point to a new chunk of memory. This means your first chunk of memory does not have a pointer to it anymore and that memory is leaked!
I think the problem is that the value of freeSpace++ is garbage at the end of your allocated memory.
Free a garbage pointer causes the problem.
I am using these lines to create variable size matrices:
Temp_Mat_0 = (double **)malloc((M)*sizeof(double ));
for (i=0;i<M;i++)
Temp_Mat_0[i] = (double *)malloc((N)*sizeof(double ));
They are working fine but I keep using them repeatedly in my code. I need to convert them to a function where I pass the pointer and the size. I was not able to do it due to pointers mess.
matrixAllocate(Matrix Pointer,rows,colms)
Can you help!
I think you'd be better off with a very simple scheme that allocates your matrix as a single contiguous block.
double **matrix_alloc(int rows, int cols)
{
/* Allocate array of row pointers */
double ** m = malloc(rows * sizeof(double*));
if (!m) return NULL;
/* Allocate block for data */
m[0] = malloc(rows * cols * sizeof(double));
if (!m[0]) {
free(m);
return NULL;
}
/* Assign row pointers */
for(int r = 1; r < rows; r++) {
m[r] = m[r-1]+cols;
}
return m;
}
This has the added bonus that when you free the matrix you don't need to remember how big it was:
matrix_free( double** m )
{
if (m) free(m[0]);
free(m);
}
As an extension to this, you might declare a struct that also keeps track of the number of rows and columns it has. e.g.
struct matrix {
int rows, cols;
double **m;
};
That makes your matrix functions look a little nicer (i.e. you can pass around struct matrix* instead of double**). It has the added bonus that the matrix dimension travels around with the associated data.
Using one contiguous block for your matrix data is generally preferable unless you have huge matrices. And it's very nice if your matrices are small, because you'll get the benefit of better memory locality in your CPU cache -- that means potential for faster code.
they are working fine
They do not seem really fine. Code should be changed to :
Temp_Mat_0 = malloc((M)*sizeof(double*)); //double* instead of double
if (Temp_Mat_0 == NULL)
return;
for (i = 0; i < M; i++){
Temp_Mat_0[i] = malloc((N)*sizeof(double));
if (Temp_Mat_0[i] == NULL){
free(Temp_Mat_0);
return;
}
}
Then, you can use a function like this :
double ** matrix_pointer = matrixAllocate(rows,colms);
where function matrixAllocate returns the pointer it allocated. For example :
matrixAllocate(rows,colms){
Temp_Mat_0 = malloc((rows)*sizeof(double*));
if (Temp_Mat_0 == NULL)
return NULL;
for (i = 0; i < rows; i++){
Temp_Mat_0[i] = malloc((colms)*sizeof(double));
if (Temp_Mat_0[i] == NULL){
free(Temp_Mat_0);
return NULL;
}
}
return Temp_Mat_0;
}
and call it like :
double **matrix pointer;
matrix pointer = matrixAllocate(rows, colms);
Don't forget to free the malloced memory afterwards.
for (i = 0; i < M; i++){
free(Temp_Mat_0[i]);
}
free(Temp_Mat_0);
Note that you should not cast the result of malloc, and you should also check if malloc was successful.
I am trying to create a 2d array of struct grid_t, and am getting memory leak warnings via address sanitiser, and eventually a seg fault in certain conditions.
There may be various points in my code causing this, but I thought knowing what was going wrong here would point me in the right direction to fixing the rest.
I am new to C and thus to memory management, so all feedback is welcome and appreciated!
void createGridArray(atom_t* ATOM) {
ATOM -> grid = (grid_t**) malloc(WIDTH * sizeof(grid_t*));
grid_t *nullGrid = malloc(sizeof(grid_t));
grid_t temp = {NULL, 0};
*nullGrid = temp;
for (int i = 0; i < WIDTH; i++) {
(ATOM -> grid)[i] = malloc(HEIGHT * sizeof(grid_t));
for (int j = 0; j < HEIGHT; j++) {
(ATOM -> grid)[i][j] = *nullGrid;
}
}
//free(nullGrid); <- do I do this now?
return;
}
Firstly, don't cast the return from malloc(). It is not required in C, and can obscure serious errors.
Second, don't hard-code the type into the malloc() call. For example,
ATOM->grid = (grid_t**) malloc(WIDTH * sizeof(grid_t*));
would be replaced by
ATOM->grid = malloc(WIDTH * sizeof(*(ATOM->grid)));
This ensures the memory allocated is of the required size, regardless of what ATOM->grid is a pointer to.
To answer your question, to release all memory, you need to pass every nonNULL pointer returned by malloc() to free(). Exactly once.
So, if you allocate like this
ATOM->grid = malloc(WIDTH * sizeof(*(ATOM->grid)));
grid_t *nullGrid = malloc(sizeof(*nullGrid));
grid_t temp = {NULL, 0};
*nullGrid = temp;
for (int i = 0; i < WIDTH; i++)
{
(ATOM -> grid)[i] = malloc(HEIGHT * sizeof(*((ATOM->grid)[i])));
for (int j = 0; j < HEIGHT; j++) {
(ATOM -> grid)[i][j] = *nullGrid;
}
the one way of deallocating would be
for (int i = 0; i < WIDTH; i++)
{
free((ATOM -> grid)[i]);
}
free(ATOM->grid);
free(nullGrid);
In this case, you cannot safely free(ATOM->grid) before any free((ATOM -> grid)[i]) (unless you store all of the (ATOM->grid)[i] somewhere else, which sort of defeats the point). The individual (ATOM->grid)[i] may be freed in any order (as long as each is released exactly once).
Lastly, check the pointers returned by malloc(). It returns NULL when it fails, and dereferencing a NULL pointer gives undefined behaviour.
Yes, you need to free(nullGrid); here to avoid a memory leak.
But actually you can simplify your code to this:
void createGridArray(atom_t* ATOM) {
ATOM -> grid = (grid_t**) malloc(WIDTH * sizeof(grid_t*));
grid_t nullGrid = {NULL, 0};
for (int i = 0; i < WIDTH; i++) {
(ATOM -> grid)[i] = malloc(HEIGHT * sizeof(grid_t));
for (int j = 0; j < HEIGHT; j++) {
(ATOM -> grid)[i][j] = nullGrid;
}
}
}
There is need for mallocing nullGrid here at all. BTW the return at the end of a void function is implicit.
You wouldn't do this either:
void Foo()
{
...
int *temp = malloc(sizeof(int));
*temp = ...;
...
bar = *temp;
...
free(temp);
}
but rather:
void Foo()
{
...
int temp
temp = ...;
...
bar = temp;
...
}
The C memory allocation is easier when you think it as a "loan".
Somehow, the system loan you some memory when you call malloc(), and you have to give it back with free().
I'm a student and I'm trying to complete a task in C right now.
I've written an algorithm, which works fine so far. It gets a file to detect if the included logical expression is satisfiable (SAT-Solver). The problem is, that I get a Segementation Fault after a while, when I'm trying to use it with bigger files (about 60MB). I think it is, because I use malloc a couple times, but I don't free that memory yet, because I don't know where to put the free() expresseions.
There's one method, which calls itself twice everytime. The code looks like this:
int DPLL (int *pointer, int variablen, int anzahlklauseln)
{
int newliteral;
int neueklauseln;
int *neuphi;
int *neuphi2;
int *unitclauses;
int *pureliterals;
int offsetzaehler;
int rek1, rek2;
klauseln = anzahlklauseln;
unitclauses = (int *) malloc(klauseln * sizeof(int));
pureliterals = (int *) malloc(variablen * sizeof(int));
if (isEmpty())
{
return 1;
}
if (ContainsEmptyClause(pointer, variablen))
{
return 0;
}
unitClauses(pointer, variablen, unitclauses);
if (unsatisfiable)
{
unsatisfiable = 0;
return 0;
}
int iunitclauses = 0;
while (unitclauses[iunitclauses] != 0)
{
unitPropagate(pointer, variablen, unitclauses[iunitclauses++]);
}
if (quicksati(pointer, variablen))
{
return 1;
}
pureLiterals(pointer, variablen, pureliterals);
int ipureliterals = 0;
while (pureliterals[ipureliterals] != 0)
{
pureLiteralAssign(pointer, variablen, pureliterals[ipureliterals++]);
}
if (quicksati(pointer, variablen))
{
return 1;
}
newliteral = chooseliteral(pointer, variablen);
neuphi = (int*) malloc(variablen * (klauseln + 1) * sizeof(int));
neuphi2 = (int*) malloc(variablen * (klauseln + 1) * sizeof(int));
int *hilfspointer = pointer;
offsetzaehler = 0;
for (int i = 0; i < (klauseln +1); ++i)
{
for (int j = 0; j < variablen; ++j)
{
if (i == klauseln)
{
neuphi[offsetzaehler] = 0;
neuphi2[offsetzaehler] = 0;
}
else
{
neuphi[offsetzaehler] = *hilfspointer;
neuphi2[offsetzaehler++] = *hilfspointer++;
}
}
}
neueklauseln = klauseln + 1;
addClause(neuphi, variablen, newliteral);
addClause(neuphi2, variablen, (-newliteral));
if (DPLL(neuphi, variablen, neueklauseln))
{
return 1;
}
else
{
return DPLL(neuphi2, variablen, neueklauseln);
}
}
Now I thought I should edit the last lines like this:
rek1 = DPLL(neuphi, variablen, neueklauseln);
rek2 = DPLL(neuphi2, variablen, neueklauseln);
free(neuphi);
free(neuphi2);
free(unitclauses);
free(pureliterals);
return rek1 || rek2;
But then I'll get segmentation fault errors for files, which worked before.
Then I tried it like this:
if (DPLL(neuphi, variablen, neueklauseln))
{
return 1;
}
else
{
free(neuphi);
free(unitclauses);
free(pureliterals);
rek1 = DPLL(neuphi2, variablen, neueklauseln);
free(neuphi2);
return rek1;
}
But then I'd get even more Segmentation Faults.
So any C experts here, who'd like to help me? Thanks alot! :)
Your recursion doesn't have an exit condition, so the program will encounter segmentation fault when it hits stack overflow or when your malloc call returns null pointer as consequence of running out of heap memory, whatever happens first.
I have traced an EXC_BAD_ACCESS to the following allocation and deallocation of memory. It involves the accelerate framework in Xcode. The main issue is that this code is in a loop. If i force the loop to only iterate once then it works fine. But when it loops (7 times) it causes an error on the second iteration. Does any of this look incorrect?
EDIT: *added actual code. This segment runs if I remove certain parts and such but seems to have poor memory management which results in issues
#import <Foundation/Foundation.h>
#include <math.h>
#include <Accelerate/Accelerate.h>
for(int i = 0; i < 8; i++)
{
int XX[M][m]; //M and m are just 2 ints
for(int kk = 0; kk < M; kk++)
{
for (int kk1 = 0; kk1 < m; kk1++)
{
XX[kk][kk1] = [[x objectAtIndex: (kk + kk1 * J)] intValue]; //x is a NSMutableArray of NSNumber objects
}
}
double FreqRes = (double) freqSamp/n;
NSMutableArray *freqs = [[NSMutableArray alloc] initWithCapacity: round((freqSamp/2 - FreqRes) - 1)];
int freqSum = 0;
for(double i = -1 * freqSamp/2; i < (freqSamp/2 - FreqRes); i+= FreqRes)
{
[freqs addObject: [NSNumber numberWithInt: i]];
if(i == 0)
{
freqSum++;
}
}
int num = [x count];
int log2n = (int) log2f(num);
int nOver2 = n / 2;
FFTSetupD fftSetup = vDSP_create_fftsetupD (log2n, kFFTRadix2);
double ffx[num];
DSPDoubleSplitComplex fft_data;
fft_data.realp = malloc(nOver2 * sizeof(double)); //Error usually thrown on this line in the second iteration. Regardless of what I put there. If I add an NSLog here it throws the error on that NSLog
fft_data.imagp = malloc(nOver2 * sizeof(double));
for (int i = 0; i < n; ++i)
{
ffx[i] = [[x objectAtIndex:i] doubleValue];
}
vDSP_ctozD((DSPDoubleComplex *) ffx, 2, &fft_data, 1, nOver2);
vDSP_fft_zripD (fftSetup, &fft_data, 1, log2n, kFFTDirection_Forward);
for (int i = 0; i < nOver2; ++i)
{
fft_data.realp[i] *= 0.5;
fft_data.imagp[i] *= 0.5;
}
int temp = 1;
ffx[0] = abs(fft_data.realp[0]);
for(int i = 1; i < nOver2; i++)
ffx[i] = sqrt((fft_data.realp[i] * fft_data.realp[i]) + (fft_data.imagp[i] * fft_data.imagp[i]));
ffx[nOver2] = abs(fft_data.imagp[0]);
for(int i = nOver2-1; i > 0; i--)
{
ffx[nOver2 + temp] = sqrt((fft_data.realp[i] * fft_data.realp[i]) + (fft_data.imagp[i] * fft_data.imagp[i]));
temp++;
}
//clear Fxx and freqs data
vDSP_destroy_fftsetupD(fftSetup);
free(fft_data.imagp);
free(fft_data.realp);
[freqs release];
}
Your problem could be that you are casting malloc to a value. As you're tagging this c, I'm assuming that you are compiling in c in which case you should see this answer to a previous question as to why casting with malloc is bad:
https://stackoverflow.com/a/1565552/1515720
you can get an unpredictable runtime error when using the cast without including stdlib.h.
So the error on your side is not the cast, but forgetting to include stdlib.h. Compilers may assume that malloc is a function returning int, therefore converting the void* pointer actually returned by malloc to int and then to your your pointer type due to the explicit cast. On some platforms, int and pointers may take up different numbers of bytes, so the type conversions may lead to data corruption.
Regardless though, as the answer says, YOU SHOULD NOT BE CASTING MALLOC RETURNS, because void*'s are safely implicitly converted to whatever you are assigning it to.
As another answerer stated:
vDSP_destroy_fftsetupD(fftSetup);
Could be also free'ing the memory you allocated on accident.
Any chance the destructor of DSPDoubleSplitComplex is freeing up those two allocated blocks?
It could also be that you are only allowed to call vDSP_create_fftsetupD and vDSP_destroy_fftsetupD once during your process's lifetime