C/C++
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   Web Development Archives FAQs C/C++

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Web Development Archives Sponsor:
  #1  
Old July 30th, 2008, 01:00 PM
leptone
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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


Reply With Quote
  #2  
Old July 31st, 2008, 07:39 AM
leptone
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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



Reply With Quote
Reply

Viewing: Web Development Archives FAQs C/C++ > Weird malloc behaviour


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek