Hi,
I installed dmalloc via RPM package
and I'm trying to compile a C program with dmalloc
I have added this line :
to my .profile
I included ‘dmalloc.h’ in my C file
I tried to linke the dmalloc library via this command:
but it gives this error:
Can any one help
Thanks in advance
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
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.
Hi Bakunin, thanks for your reply
Here is the source code:
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
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:
gives you ten array elements of type int:
Alas, this isn't the case. In fact, there are ten integer elements, but they are:
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.
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:
when I tried to compile using dmalloc with this command:
I got these errors:
Any ideas ? I think there is a problem in configuration : I only added this line to my .profile after installation:
is there any other thing to do before using it ? or something to make sure it's working?
Thanks in advance
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)