How to use dmalloc?


 
Thread Tools Search this Thread
Operating Systems AIX How to use dmalloc?
# 1  
Old 05-22-2014
How to use dmalloc?

Hi,
I installed dmalloc via RPM package
and I'm trying to compile a C program with dmalloc
I have added this line :
Code:
function dmalloc { eval ‘command dmalloc -b $*‘; }

to my .profile
I included ‘dmalloc.h’ in my C file
I tried to linke the dmalloc library via this command:
Code:
% gcc  -o myprog test.c dmalloc.o

but it gives this error:
Code:
ld: 0711-224 WARNING: Duplicate symbol: .main
ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information

Can any one help
Thanks in advance
# 2  
Old 06-07-2014
First off: how do you think we can find the error in your program without even looking at the code? If you want us to debug it, then, by all means, post it.

Quote:
Originally Posted by SteAlma
Code:
ld: 0711-224 WARNING: Duplicate symbol: .main
ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information

This looks like it has nothing at all to do with any library you may or may not use. I suppose you have called a second function "main()". Rename it and recompile.

I hope this helps.

bakunin
# 3  
Old 06-09-2014
Hi Bakunin, thanks for your reply
Here is the source code:
Code:
         #ifdef DMALLOC
         #include "dmalloc.h"
         #endif
		int a[10];
		main()
		{
			int i, tot=0;
	
			for(i=1; i<=10; i++)
				tot += a[i]; 
			return (0);
		}

This code attempts to access an illegal array element due to an incorrect loop range.And I'm trying to detect this error using dmalloc
Thanks in advance
# 4  
Old 06-09-2014
Quote:
Originally Posted by SteAlma
This code attempts to access an illegal array element due to an incorrect loop range.And I'm trying to detect this error using dmalloc
I one word: you can't. ;-))

malloc() is a system call to allocate memory dynamically. If you define a variable like you did define "tot", or the array "a" the compiler will set aside a certain amount of memory to provide the necessary space at runtime. This is why you need a type for every variable - only this lets the compiler know how much to set aside.

In some cases, though, it makes more sense to allocate memory during runtime, because at compile time the programmer doesn't knwo how much is needed. Say, you want to write a text editor. You probably want to hold all the lines of text in an array of char[], but you do not know at compile time how many lines there will be or how long they are. This is where malloc() enters the picture: it allocates during runtime a certain amount of memory and gives back a pointer to it. To stick with the example, you would start with an array of pointers, all pointing to NIL (your lines, all empty). As you read each line, you allocate some space for it and add the pointer you get from malloc() to the respective element of the array.

If you need the third character in the fifth line now, you go to array element 2 (arrays start with 0), take this pointer, go to the memory address it points to, go 4 bytes forward (to be precise, you move forward 4 times the sizeof() of whatever datatype your array element is, but presumably it is char[] and characters are usually equal to bytes) and there you are.

In your case all this doesn't apply. You simply implied that arrays are one-based, therefore:
Code:
int a[10];

gives you ten array elements of type int:

Code:
a[1]
a[2]
a[3]
a[4]
a[5]
a[6]
a[7]
a[8]
a[9]
a[10]

Alas, this isn't the case. In fact, there are ten integer elements, but they are:

Code:
a[0]
a[1]
a[2]
a[3]
a[4]
a[5]
a[6]
a[7]
a[8]
a[9]

The reason is that array elements are basically offsets to a common root address. The runtime library knows "at address X are ten consecutive integers" and if you reference "a[3]" this means: go to address X (=element 0), then move forward 3 times the length (=sizeof()) of datatype "int" and then start reading. This, as you can easily retrace, will land you on the beginning of the fourth element.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 5  
Old 06-09-2014
Hello Bakunin, and thank you for your reply which makes always things more clear for me ...
I can see now that this kind of error is not catched by dmalloc, but even if I tried to test an expression using dangling pointer, which is normally detected by dmalloc (according to its documentation) it gave errors, an example will be more clear:
The following code fragment shows a block of memory being allocated and then freed. After the memory is de-allocated, the pointer to it is used again, even though it no longer points to valid memory:
Code:
/* 
	 * Fichier: expdangl.c 
	 */ 
	 # include <stdlib.h> 
	 #ifdef DMALLOC
         #include "dmalloc.h"
         #endif
		 
	 main () 
	 { 
	 char * a = (char *) malloc (10); 
	 char b [10]; 
	 
	 free(a); 
	 if (a> b) 
	 a = b; 
	 return (0); 
	}

when I tried to compile using dmalloc with this command:
Code:
gcc  -o test2 expdangl.c dmalloc.o

I got these errors:
Code:
ld: 0711-224 WARNING: Duplicate symbol: .main
ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information.

Any ideas ? I think there is a problem in configuration : I only added this line to my .profile after installation:
Code:
function dmalloc { eval ‘command dmalloc -b $*‘; }

is there any other thing to do before using it ? or something to make sure it's working?
Thanks in advance

Last edited by SteAlma; 06-09-2014 at 01:35 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. AIX

Dmalloc install ussues on AIX

Hello, I am trying to install Dmalloc 5.5.2 on AIX 5.3 While installing it both :sh ./configure and make run without errors. Then the command: make install finishes like this: ./mkinstalldirs /usr/local/include ./mkinstalldirs: not found make: The error code from the last command is 1... (8 Replies)
Discussion started by: SteAlma
8 Replies
Login or Register to Ask a Question