|
|
|
|
|||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Weird malloc behaviour
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_PRFILE; int main(void){ BEAM_PRFILE * bpp; int i; bpp=(BEAM_PRFILE *)malloc(sizeof(BEAM_PRFILE)); printf("Memory was allocated at address %.8X\n", bpp); bpp=(BEAM_PRFILE *)malloc(sizeof(BEAM_PRFILE)); 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_PRFILE * ) 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 Regards Nicola |
|
#2
|
|||
|
|||
|
Weird malloc behaviour
, I followed your suggestions and edited the code like this:
/////////////////////////////////////////////////////////////// #include <stdlib.h> #include <stdio.h> typedef struct { int a; int b; int c; } BEAM_PRFILE; void main(void){ BEAM_PRFILE * bpp; unsigned long lpp; printf("sizeof(*BEAM_PRFILE) is %d\n", sizeof(BEAM_PRFILE*)); printf("sizeof(lpp) is %d\n", sizeof(lpp)); bpp=malloc(sizeof(BEAM_PRFILE)); lpp=(unsigned long)bpp; printf("Memory was allocated at address(X mod.) %.8X\n", bpp); printf("Memory was allocated at address(p mod.) %p\n", bpp); bpp=malloc(sizeof(BEAM_PRFILE)); printf("Size of the pointer is %d\n", sizeof(bpp)); lpp=(unsigned long)bpp; printf("Memory was allocated at address %.8X\n", bpp); printf("Memory was allocated at address(p mod.) %p\n", bpp); } /////////////////////////////////////////////////////////////////////////////////// The result, using the Large memory model, is the following: C:\PACIFIC\SRC>tstmall sizeof(*BEAM_PRFILE) is 4 sizeof(lpp) is 4 Memory was allocated at address(X mod.) 00000004 Memory was allocated at address(p mod.) 1DD5:0004 Size of the pointer is 4 Memory was allocated at address 00000004 Memory was allocated at address(p mod.) 1DD6:0004 With the small one, instead, I have the following C:\PACIFIC\SRC>tstmall sizeof(*BEAM_PRFILE) is 2 sizeof(lpp) is 4 Memory was allocated at address(X mod.) 00000672 Memory was allocated at address(p mod.) 0672 Size of the pointer is 2 Memory was allocated at address 0000067C Memory was allocated at address(p mod.) 067C So now things seem to work fine for the both of them. Now I am just wondering why it takes to different segments to allocate a structure which is only four bytes long, but i guess this has something to do with the memory management model of the compiler By the way, thanks to everyone for your precious help!!! Regards Nicola |
![]() |
| Viewing: Web Development Archives > FAQs > C/C++ > Weird malloc behaviour |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|