多个malloc在同一个指针中 [英] Multiple malloc in the same pointer

查看:86
本文介绍了多个malloc在同一个指针中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我在同一个指针中多次使用malloc,例如:


int i;

my_structure_t * my_structure


while(true)

{

my_structure = malloc(sizeof(my_structure_t) );

printf("%d \ n",my_structure);

}


但是我是发现这一点,无论我多少次用指针调用malloc,每次我的指针指向相同的内存位置。


这是正确的或者我在这里胡说八道?如果我是正确的,我可以怎么做每次通话分配新内存?


我在linux上使用gcc。 />

问候,

$ b $bAndré

解决方案

8月21日14:14,AndréWagner< andre .... @ gmail.com写道:


你好,


我在同一个指针中多次使用malloc,例如:


int i;

my_structure_t * my_structure


while(true)

{

my_structure = malloc(sizeof(my_structure_t));

printf("% d \ n",my_structure);


}


但我发现这一点,无论我打多少次malloc in

指针,每当我的指针指向相同的记忆位置时。


这是正确的还是我在胡说八道?如果我是正确的,我可以怎么做每次通话分配新内存?


我在linux上使用gcc。 />

问候,

$ b $bAndré



你承诺printf()是一个整数,但是

你实际上是在向它传递一个指针。


所有指针都是不同的,但是无论如何都是合理的其中一部分是printf()

解释为整数是相同的。

-


< blockquote>AndréWagner写道:


你好,


我在同一个指针中多次使用malloc,例如as:



不要告诉我们比如,向我们展示最小的自包含程序

这表明问题。


int i;

my_structure_t * my_structure


while(true)

{

my_structure = malloc(sizeof(my_structure_t));

printf("%d \ n",my_structure);

}



%d不是指针的正确printf掩码(我非常确定

它应该是一个传递给printf的void指针)。忽略

void指针问题,例如,如果你有64位指针和32位

指针的位...


修正掩码(并传递void *)作为开始,然后向我们显示

最小程序演示问题,如果它仍然是错误的。


但是我发现了,无论我多少次调用malloc在


指针,每次我的指针指向相同的内存位置。



你承诺printf()是一个整数,但

实际上你传给它一个指针。


所有指针都是不同的,这是非常合理的,但是无论它们是printf()

解释为整数都是相同的。



这是一个很好的猜测(我没有想到)但是我将%d

更改为%p,并且仍然显示相同的地址。

$ b $bAndré


Hello,

I''m using malloc several times in the same pointer, such as:

int i;
my_structure_t* my_structure

while(true)
{
my_structure = malloc(sizeof(my_structure_t));
printf("%d\n", my_structure);
}

But I''m finding out that, no matter how many times I call malloc in
the pointer, every time my pointer points to the same memory position.

Is this correct or am I speaking nonsense here? If I''m correct, how
can I do to allocate new memory on each call?

I''m using gcc on linux.

Regards,

André

解决方案

On 21 Aug, 14:14, André Wagner <andre....@gmail.comwrote:

Hello,

I''m using malloc several times in the same pointer, such as:

int i;
my_structure_t* my_structure

while(true)
{
my_structure = malloc(sizeof(my_structure_t));
printf("%d\n", my_structure);

}

But I''m finding out that, no matter how many times I call malloc in
the pointer, every time my pointer points to the same memory position.

Is this correct or am I speaking nonsense here? If I''m correct, how
can I do to allocate new memory on each call?

I''m using gcc on linux.

Regards,

André

You have promised printf() an integer, but
you are actually passing it a pointer.

It is quite plausible that all the pointers are
different, but whatever part of them printf()
is interpreting as an integer is the same.
--


André Wagner wrote:

Hello,

I''m using malloc several times in the same pointer, such as:

Don''t tell us "such as", show us the smallest self-contained program
which demonstrates the problem.

int i;
my_structure_t* my_structure

while(true)
{
my_structure = malloc(sizeof(my_structure_t));
printf("%d\n", my_structure);
}

%d is not the correct printf mask for a pointer (and I''m fairly sure
that it should be a void pointer that you pass to printf). Ignoring the
void pointer issue, if, for example, you have 64-bit pointers and 32-bit
ints, this would probably show the same value for the 32-bits of the
pointer that happened to be printed...

Correct the mask (and pass void *) for a start and then show us a
minimal program demonstrating the problem if it still seems wrong.


But I''m finding out that, no matter how many times I call malloc in

the pointer, every time my pointer points to the same memory position.


You have promised printf() an integer, but
you are actually passing it a pointer.

It is quite plausible that all the pointers are
different, but whatever part of them printf()
is interpreting as an integer is the same.

That was a good guess (I haven''t thought of that) but I changed the %d
to %p, and still shows the same address.

André


这篇关于多个malloc在同一个指针中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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