Tuesday, July 29, 2008

glibc detected double free or corruption

While segmentation fault is the most common error that C/C++ face another one that has been harassing me for some time is as follows ::

glibc detected double free or corruption


This occurs when one tries to release memory that has already been deallocated. One quick and dirty way to get rid of it is as follows :

$export MALLOC_CHECK_=0

( thats a zero at the right side of the equal to sign )

and now run the executable, it should run without a problem.

However I am not too sure if this will cause unreliable results or any other sort of problems later. I guess its best to check the code to find out and remedy the error causing this.

Could the following be of any use ?
http://valgrind.org/

See this page to understand how valgrind can help you.

Friday, July 25, 2008

#define main

I have two files and the contents are as follows :

file :: main.h
-------------------------------------
#ifndef main
#define main

#endif
--------------------------------------

and

file :: main.cpp
-------------------------------------
#include "main.h"

int main(void)
{
return 0 ;

}

------------------------------------

This program does not compile and the error message that I get is ::

expected unqualified-id before ‘void’
expected `)' before ‘void’

After some digging I have established that the problem lies in the
lines
#ifndef main
#define main
If I change it to
#ifndef Main
#define Main
or
#ifndef MAIN
#define MAIN
etc.
the program compiles.

Turns out that the line
#define main
actually defines the macro ``main" with an empty value. When the preprocessor works on the code it replaces all instances of the word main with an empty string resulting in the compile time error as posted above.

To find out the error just see the effect of preprocessor on the code before and after changing the macro name from main to Main or MAIN or whatever.

The effect of the preprocessor can be seen with the following command :
g++ -E main.cpp

Mind you that the above command will produce lots and lots of code ( in my case about 30000 lines of code). So this is what you can do

First with the erroneous header file ::
g++ -E main.cpp > outputA

and then with the fixed header file ::
g++ -E main.cpp > outputB

now compare the files outputA and outputB with the diff utility.
diff outputA outputB
You will find that the preprocessor working on the erroneous code produces
int (void)
while the preprocessor working on the correct code produces
int main(void)


Thanks to the gurus at gnu.g++.help I could understand the error I was making !
The thread can be found here.

p.s. I work on GNU/Linux with the gcc compiler.

Thursday, July 3, 2008

Handling matrices in C++

Most of my programming work in C++ involves numerical computation based work. I have to deal with a lot of 2-dimensional matrices and the approach that I stuck to earlier was to use a pointer to a pointer.

double** matrix;

This allows for dynamic allocation of matrices. However I can't pass a pointer to a pointer and use const protection at the same time.
Say I have a function that just takes the 2-dimensional matrix and displays it. Now I want to protect the matrix from being inadvertently changed in the called function. So I want to declare the function as follows:

void function(const double** const matrix); // WRONG

The above can't be done :(

I am not even aware if a 2-dimensional array can be passed by reference in C++.

So this is what I am trying instead.

Say I have a 2-D matrix A as follows:
A = [1, 2,3]
[4,5,6]
[7,8,9]

then instead of defining this as :
A [3][3] = { {1,2,3}, {4,5,6}, {7,8,9}};

I define it as a single dimension array as follows :
A[9] = { 1,2,3,4,5,6,7,8,9} ;

That way one can easily pass the pointer as follows

void function(const double * const A );

-----------------------------------------------------------------

If you know a better solution please DO let me know.




Tuesday, July 1, 2008

http://www.scholarshipnet.info/

I might need this after the completion of my thesis.

http://www.scholarshipnet.info/