Re: Weird malloc behaviour
- From: Eric Sosman <Eric.Sosman@xxxxxxx>
- Date: Wed, 30 Jul 2008 12:22:47 -0400
leptone@xxxxxxxxx wrote:
Dear all,
I am programming a PLC with an 80188 processor, hence I am using an
old version of Borland C++ (3.10). While doing the job, I've
encountered strange behaviours and I've isolated the problem that
seems to be related to a malloc function. I wrote a little piece of
code to reproduce the situation:
#include <stdlib.h>
#include <stdio.h>
typedef struct {
int a;
int b;
} BEAM_PROFILE;
int main(void){
BEAM_PROFILE * bpp;
int i;
bpp=(BEAM_PROFILE *)malloc(sizeof(BEAM_PROFILE));
printf("Memory was allocated at address %.8X\n", bpp);
bpp=(BEAM_PROFILE *)malloc(sizeof(BEAM_PROFILE));
printf("Memory was allocated at address %.8X\n", bpp);
}
When I use the small memory model, everything runs smoothly. The
problem is when I use the Large memory model. In this case the program
output is the following:
C:\BCTEST>tstmall
Memory was allocated at address 00000004
Memory was allocated at address 00000004
Every call to malloc returns exactly the same address (not null). I've
verified that the sizeof(BEAM_PROFILE * ) is 4 bytes with the large
memory model and 2 bytes with the small memory model, so I really
cannot figure out where the problem is...
Hope you have some clue about it...
The printf() argument corresponding to the "X" conversion
specifier must be an unsigned int, but bpp is not any kind of
int at all, signed, unsigned, or resigned. bpp is a pointer,
and printf() has only one conversion specifier for pointers, and
only for void* pointers at that:
printf ("Memory at %p\n", (void*)bpp);
You mention that you are using an old compiler, and if it is
very old indeed its accompanying libraries might not support the
"p" specifier, which was standardized less than twenty years ago.
If "p" doesn't work, I suggest you try
printf ("Memory at %.08lX\n", (unsigned long)bpp);
as a rather pathetic substitute. (Note that the character before
the X is a letter, not a digit.)
--
Eric.Sosman@xxxxxxx
.
- References:
- Weird malloc behaviour
- From: leptone
- Weird malloc behaviour
- Prev by Date: Re: Weird malloc behaviour
- Next by Date: Re: Constraint violation - when?
- Previous by thread: Re: Weird malloc behaviour
- Next by thread: Re: Weird malloc behaviour
- Index(es):
Relevant Pages
|