Re: Weird malloc behaviour



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
.



Relevant Pages

  • Re: Weird malloc behaviour
    ... I am programming a PLC with an 80188 processor, ... Change the format specifier from X to p and cast bpp to void* ... When I use the small memory model, ...
    (comp.lang.c)
  • Weird malloc behaviour
    ... I am programming a PLC with an 80188 processor, ... BEAM_PROFILE * bpp; ... When I use the small memory model, ...
    (comp.lang.c)
  • Re: Array objects
    ... Our memory model formalizes values as trees, ... numbered 4 and 6 to exhibit undefined behavior. ... i from 0 to 5 are objects of type 'int'. ... I have long felt that the standard should be more specific about what ...
    (comp.std.c)
  • Re: display array in a frame wnd
    ... Some display are corresponding to several cameras (global ... void CDisplayImg::DrawToHDC ... void CDisplayImg::FillBitmapInfo(BITMAPINFO* bmi, int width, int height, ... int bpp, int origin) ...
    (microsoft.public.vc.mfc)
  • Re: display array in a frame wnd
    ... document/view architecture and use ... Some display are corresponding to several cameras (global ... void CDisplayImg::FillBitmapInfo(BITMAPINFO* bmi, int width, int height, ... int bpp, int origin) ...
    (microsoft.public.vc.mfc)