奇怪的malloc行为 [英] Weird malloc behaviour

查看:55
本文介绍了奇怪的malloc行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,


我正在使用80188处理器编程PLC,因此我使用的是旧版本的Borland C ++(3.10)。在完成这项工作的时候,我已经遇到了奇怪的行为,而且我已经解决了

似乎与malloc函数有关的问题。我写了一小段

代码来重现这种情况:


#include< stdlib.h>

#include < stdio.h>


typedef struct {

int a;

int b;

} BEAM_PROFILE;

int main(无效){


BEAM_PROFILE * bpp;

int i;


bpp =(BEAM_PROFILE *)malloc(sizeof(BEAM_PROFILE));

printf(内存分配地址%.8X \ n,bpp);


bpp =(BEAM_PROFILE *)malloc(sizeof(BEAM_PROFILE));

printf(内存分配地址为%.8X \ n, bpp);

}


当我使用小内存模型时,一切运行顺畅。

问题是我使用大内存模型的时候。在这种情况下,程序

输出如下:


C:\ BCTEST> tstmall

内存分配在地址00000004

内存分配地址00000004


每次调用malloc都返回完全相同的地址(非空)。我已经验证了sizeof(BEAM_PROFILE *)是4字节,内存模型很大,而内存模型是2字节,所以我真的很好$>
无法弄清问题在哪里...


希望你对它有一些线索...


问候


Nicola

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...

Regards

Nicola

推荐答案

le ***** @ gmail.com 写道:
le*****@gmail.com wrote:

亲爱的,

我正在使用80188处理器编程PLC,因此我使用的是旧版本的Borland C ++(3.10)。在完成这项工作的时候,我已经遇到了奇怪的行为,而且我已经解决了

似乎与malloc函数有关的问题。我写了一小段

代码来重现这种情况:


#include< stdlib.h>

#include < stdio.h>


typedef struct {

int a;

int b;

} BEAM_PROFILE;


int main(无效){


BEAM_PROFILE * bpp;

int i;


bpp =(BEAM_PROFILE *)malloc(sizeof(BEAM_PROFILE));
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));



无需从malloc转换返回值。

No need to cast the return value from malloc.


printf(" Memory is assigned地址%。8X \ n",bpp);
printf("Memory was allocated at address %.8X\n", bpp);



将格式说明符从X更改为p并将bpp转换为void *


printf(内存已分配于地址%p \ n",(void *)bpp);

Change the format specifier from X to p and cast bpp to void*

printf("Memory was allocated at address %p\n", (void*)bpp);


bpp =(BEAM_PROFILE *)malloc(sizeof(BEAM_PROFILE));

printf(内存分配地址%.8X \ n,bpp);
bpp=(BEAM_PROFILE *)malloc(sizeof(BEAM_PROFILE));
printf("Memory was allocated at address %.8X\n", bpp);



如上所述。

As above.


}


当我使用小内存模型,一切运行顺畅。

问题是我使用大内存模型的时候。在这种情况下,程序

输出如下:


C:\ BCTEST> tstmall

内存分配在地址00000004

内存分配地址00000004
}

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



建议的更改后你仍然得到相同的输出吗?

< snip>

Do you still get the same output after the suggested changes?

<snip>


le ** ***@gmail.com 写道:

亲爱的,


我用PLC编程一个80188处理器,因此我使用了一个

旧版本的Borland C ++(3.10)。在完成这项工作的时候,我已经遇到了奇怪的行为,而且我已经解决了

似乎与malloc函数有关的问题。我写了一小段

代码来重现这种情况:


#include< stdlib.h>

#include < stdio.h>


typedef struct {

int a;

int b;

} BEAM_PROFILE;


int main(无效){


BEAM_PROFILE * bpp;

int i;


bpp =(BEAM_PROFILE *)malloc(sizeof(BEAM_PROFILE));

printf(内存分配地址为%.8X \ n, bpp);


bpp =(BEAM_PROFILE *)malloc(sizeof(BEAM_PROFILE));

printf("内存分配地址为%.8X \\ \\ n",bpp);

}


当我使用小内存模型时,一切运行顺畅。

问题是我使用大内存模型的时候。在这种情况下,程序

输出如下:


C:\ BCTEST> tstmall

内存分配在地址00000004

内存分配地址00000004


每次调用malloc都返回完全相同的地址(非空)。我已经验证了sizeof(BEAM_PROFILE *)是4字节,内存模型很大,而内存模型是2字节,所以我真的很好$>
无法弄清问题在哪里...


希望你有一些线索...
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...



对应于X的printf()参数。转换

说明符必须是unsigned int,但bpp根本不是任何类型的

int,有符号,无符号或已取消。 bpp是一个指针,

和printf()只有一个转换说明符用于指针,而

仅用于void *指针:


printf(" Memory at%p \ nn",(void *)bpp);


你提到你使用的是旧编译器,如果它是

确实很老了它的附带库可能不支持

p说明符,在二十年前标准化。

如果p不行,我建议你试试


printf(Memory at%.08lX\ n,(unsigned long)bpp);

作为一个相当可怜的替代品。 (注意

之前的字符X是一个字母,而不是一个数字。)


-
呃********* @ sun.com


< a href =mailto:le ***** @ gmail.com> le ***** @ gmail.com 写道:

我正在使用80188处理器编程PLC,因此我使用的是旧版本的Borland C ++(3.10)。在完成这项工作的时候,我已经遇到了奇怪的行为,而且我已经解决了

似乎与malloc函数有关的问题。我写了一小段

代码来重现这种情况:
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>
#include <stdlib.h>
#include <stdio.h>


typedef struct {

int a;

int b;

} BEAM_PROFILE;
typedef struct {
int a;
int b;
} BEAM_PROFILE;


int main(void){
int main(void){


BEAM_PROFILE * bpp;

int i;
BEAM_PROFILE * bpp;
int i;


bpp =(BEAM_PROFILE *)malloc(sizeof(BEAM_PROFILE));
bpp=(BEAM_PROFILE *)malloc(sizeof(BEAM_PROFILE));



如果您没有忘记包含< stdlib.h>的
,则不需要返回值的演员表。如果你忘了它可能会让编译器继续标记可能是一个严重错误的内容。

The cast of the return value is not needed if you didn''t forgot
to include <stdlib.h>. If you forgot it may keep the compiler
from flagging what might be a serious bug.


printf("内存分配在地址%。8X \ n",bpp);
printf("Memory was allocated at address %.8X\n", bpp);



%X格式说明符需要一个unsigned int参数。

你传给它一个指针。你有没有试过


printf(内存分配在地址%p \ n,(无效)bpp);


这是打印地址的正确格式说明符?

或者,如果你坚持使用%X,你是否至少尝试将

地址转换为unsigned int?这不是我要为便携式程序推荐的b
或者有任何保证

结果值有意义,但至少它会

确保printf()会收到一个unsigned int,其中<​​br />
需要一个。

The "%X" format specifier expects an unsigned int argument.
You pass it a pointer instead. Did you try instead

printf("Memory was allocated at address %p\n", (void) bpp);

which is the correct format specifier for printing out addresses?
Or, if you insist on "%X", did you at least try to cast the
address to an unsigned int? Not that this is something I would
recommend for a portable program or that there is any guarantee
that the resulting value makes any sense, but at least it would
make sure that printf() would receive an unsigned int where it
expects one.


bpp =(BEAM_PROFILE * )malloc(sizeof(BEAM_PROFILE));

printf(内存分配地址%.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

内存分配地址00000004

内存分配地址00000004
C:\BCTEST>tstmall
Memory was allocated at address 00000004
Memory was allocated at address 00000004


每次调用malloc都会返回相同的地址(非空)。我已经验证了sizeof(BEAM_PROFILE *)是4字节,内存模型很大,而内存模型是2字节,所以我真的很好/>
无法找出问题所在... ...
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...



很可能unsigned int只有两个字节

在您的机器上,无论内存模型是什么。 (一个术语C

标准不使用,因此它是C的扩展)。如果对于

,则是小内存模型。地址也有2个字节使用

%X格式说明符可能会生成看起来像某个合理地址的内容,而使用大内存模型,

obvioulsy只会打印出垃圾。 printf()和其他变量/>
adic函数不喜欢它,如果它们被骗 - 你传递它们的争论 -

更好地适合格式说明符。


问候,Jens

-

\ Jens Thoms Toerring ___ jt@toerring.de

\ __________________________ http:/ /toerring.de


这篇关于奇怪的malloc行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆