malloc问题,第2部分 [英] A malloc question, part 2

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

问题描述

感谢那些回答我原来问题的人。我以为我理解答案并开始编写一些代码来证明我的理解。编写的代码没有任何错误检查。


---

#include< stdio.h>

#include < stdlib.h>

#include< string.h>


typedef struct {

char s [23] ;

int n;

} SomeStruct;


typedef struct {

char user [32 ];

int age;

char其他[100];

} UserStruct;


void my_test_func(int cnt)

{

SomeStruct * p,* q;

UserStruct * x,* y;


printf(SomeStruct的大小= 0x%X \ n,sizeof(SomeStruct));


p = malloc(cnt * sizeof(SomeStruct) );

q = p + 1;

printf(" p = 0x%X,q = 0x%X.qp = 0x%X \ n,p ,q,(int)q-(int)p);


x =(UserStruct *)(p);

y = x + 1;

printf(" x = 0x%X,y = 0x%X。yx = 0x%X \ n",x,y,(int)y-(int)x);


免费(p);

}


int main (int argc,char argv [])

{

printf(" UserStruct的大小= 0x%X \ n,sizeof(UserStruct));

my_test_func(sizeof(UserStruct));

返回1;

}

---


输出为:

UserStruct的大小= 0x88

SomeStruct的大小= 0x1C

p = 0x20F10,q = 0x20F2C。 q-p = 0x1C

x = 0x20F10,y = 0x20F98。 y-x = 0x88


我的问题是,似乎我能够指针指向

正确开始第二个结构。我使用p,q代表SomeStruct

,然后是UserStruct; x,y表示UserStruct,后跟SomeStruct。在

两种情况下,指针都提高了由sizeof()计算的确切字节数




我想我可以强制转换(p + 1)或(x + 1)然后正确访问第二个结构

。这显然与我之前收到的所有答案相矛盾。这次我哪里错了?


/为什么茶

解决方案

" ;为何选择茶叶< yt **** @ gmail.comwrote in message

news:11 ********************* @ a3g2000cwd.googlegrou ps .com ...


感谢那些回答了原始问题的人。我以为我理解答案并开始编写一些代码来证明我的理解。编写的代码没有任何错误检查。


---

#include< stdio.h>

#include < stdlib.h>

#include< string.h>


typedef struct {

char s [23] ;

int n;

} SomeStruct;


typedef struct {

char user [32 ];

int age;

char其他[100];

} UserStruct;


void my_test_func(int cnt)

{

SomeStruct * p,* q;

UserStruct * x,* y;


printf(SomeStruct的大小= 0x%X \ n,sizeof(SomeStruct));


p = malloc(cnt * sizeof(SomeStruct) );

q = p + 1;

printf(" p = 0x%X,q = 0x%X.qp = 0x%X \ n,p ,q,(int)q-(int)p);


x =(UserStruct *)(p);

y = x + 1;

printf(" x = 0x%X,y = 0x%X。yx = 0x%X \ n",x,y,(int)y-(int)x);


free(p);

}


int main(int argc,char argv [])

{

printf(" ; UserStruct的大小= 0x%X \ n",sizeof(UserStruct));

my_test_func(sizeof(UserStruct));

返回1;

}

---


输出结果为:

UserStruct的大小= 0x88

SomeStruct的大小= 0x1C

p = 0x20F10,q = 0x20F2C。 q-p = 0x1C

x = 0x20F10,y = 0x20F98。 y-x = 0x88


我的问题是,似乎我能够指针指向

正确开始第二个结构。我使用p,q代表SomeStruct

,然后是UserStruct; x,y表示UserStruct,后跟SomeStruct。在

两种情况下,指针都提高了由sizeof()计算的确切字节数




我想我可以强制转换(p + 1)或(x + 1)然后正确访问第二个结构

。这显然与我之前收到的所有答案相矛盾。这次我哪里弄错了?


/为什么茶



我不明白你的问题。你已经证明你可以将一个

指针类型转换为另一个,然后编译器将处理新的

指针,因为它应该对它进行处理。 />

当你使用如下声明时:


指针++;





指针+ = integer_offset;


编译器必须考虑指针类型(它指向什么)

决定如何偏移指针。因此,在机器算术级别,指向苹果的指针与指向梨的指针的处理方式不同。


更具体地说:


Pear * ppear;

Apple * papple;


ppear =(Pear *)(papple = malloc(sizeof(Apple *) 100)));


/ * ppear和papple现在相等* /


ppear ++;

papple ++ ;


/ * ppear和papple现在可能不相等,如果Pear和Apple不相等

等于大小* /


将创建不等的指针(通常)。


您发布的内容并不令人意外。这就是编译器应该用b $ b行为的方式。


在你的下一篇文章中(电话是第3部分),请清楚地说明你的问题。

你已经把一个苹果扔到了空中,它又回来了。牛顿知道

这个。我知道这个。我们都知道这个。


你的问题是什么?你已经证明了编译器的行为就像

那样。


你的问题好吗?


< blockquote>


您发布的内容并不令人意外。这就是编译器应该用b $ b行为的方式。


在你的下一篇文章中(电话是第3部分),请清楚地说明你的问题。

你已经把一个苹果扔到了空中,它又回来了。牛顿知道

这个。我知道这个。我们都知道这个。


你的问题是什么?你已经证明了编译器的行为就像

应该的那样。


你的问题好吗?



对不起大卫。这与我之前发布的有关同一主题的问题有关。我从Eric Sosman那里得到了一些很好的答案,

理查德希思菲尔德,等等。但是还有一些东西我不完全清楚,这就是我发布的原因第二部分问题。我应该可能已经就这个话题对埃里克和理查德进行了抨击。


" Why Tea" < yt **** @ gmail.comwrote in message

news:11 ********************** @ n51g2000cwc.googlegr oups.com ...


>


>你发布的内容并不令人意外。这是编译器应该表现的方式。

在你的下一篇文章中(电话是第3部分),请清楚地确定你的问题。
你有把苹果扔到空中,它又回来了。牛顿知道这个。我知道这个。我们都知道这个。

你的问题是什么?你已经证明了编译器的行为应该是


你的问题好吗?



对不起大卫。这与我之前发布的有关同一主题的问题有关。我从Eric Sosman那里得到了一些很好的答案,

理查德希思菲尔德,等等。但是还有一些东西我不完全清楚,这就是我发布的原因第二部分问题。我可能应该在这个主题上对埃里克和理查德进行抨击。



我不是很生气。我只是好奇。不需要道歉。


编译器行为的哪些方面令你感到惊讶?在我看来

,根据你发布的内容,它的行为应该如此。


Thanks to those who have answered my original question. I thought I
understood the answer and set out to write some code to prove my
understanding. The code was written without any error checking.

---
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
char s[23];
int n;
} SomeStruct;

typedef struct {
char user[32];
int age;
char others[100];
} UserStruct;

void my_test_func(int cnt)
{
SomeStruct *p, *q;
UserStruct *x, *y;

printf("Size of SomeStruct=0x%X\n", sizeof(SomeStruct));

p = malloc(cnt*sizeof(SomeStruct));
q = p + 1;
printf("p=0x%X, q=0x%X. q-p=0x%X\n", p, q, (int)q-(int)p);

x = (UserStruct *)(p);
y = x + 1;
printf("x=0x%X, y=0x%X. y-x=0x%X\n", x, y, (int)y-(int)x);

free(p);
}

int main(int argc, char argv[])
{
printf("Size of UserStruct=0x%X\n", sizeof(UserStruct));
my_test_func(sizeof(UserStruct));
return 1;
}
---

The outputs are:
Size of UserStruct=0x88
Size of SomeStruct=0x1C
p=0x20F10, q=0x20F2C. q-p=0x1C
x=0x20F10, y=0x20F98. y-x=0x88

My question is, it seems that I was able to get the pointer to point to
the start of the second struct correctly. I used p, q for SomeStruct
followed by UserStruct; x, y for UserStruct followed by SomeStruct. In
both cases, the pointer has advanced the exact number of bytes
calculated by sizeof().

I think I can cast (p+1) or (x+1) and then access the second struct
correctly. This obviously contradicts with all the answers I received
previously. Where did I get it wrong this time?

/Why Tea

解决方案

"Why Tea" <yt****@gmail.comwrote in message
news:11*********************@a3g2000cwd.googlegrou ps.com...

Thanks to those who have answered my original question. I thought I
understood the answer and set out to write some code to prove my
understanding. The code was written without any error checking.

---
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
char s[23];
int n;
} SomeStruct;

typedef struct {
char user[32];
int age;
char others[100];
} UserStruct;

void my_test_func(int cnt)
{
SomeStruct *p, *q;
UserStruct *x, *y;

printf("Size of SomeStruct=0x%X\n", sizeof(SomeStruct));

p = malloc(cnt*sizeof(SomeStruct));
q = p + 1;
printf("p=0x%X, q=0x%X. q-p=0x%X\n", p, q, (int)q-(int)p);

x = (UserStruct *)(p);
y = x + 1;
printf("x=0x%X, y=0x%X. y-x=0x%X\n", x, y, (int)y-(int)x);

free(p);
}

int main(int argc, char argv[])
{
printf("Size of UserStruct=0x%X\n", sizeof(UserStruct));
my_test_func(sizeof(UserStruct));
return 1;
}
---

The outputs are:
Size of UserStruct=0x88
Size of SomeStruct=0x1C
p=0x20F10, q=0x20F2C. q-p=0x1C
x=0x20F10, y=0x20F98. y-x=0x88

My question is, it seems that I was able to get the pointer to point to
the start of the second struct correctly. I used p, q for SomeStruct
followed by UserStruct; x, y for UserStruct followed by SomeStruct. In
both cases, the pointer has advanced the exact number of bytes
calculated by sizeof().

I think I can cast (p+1) or (x+1) and then access the second struct
correctly. This obviously contradicts with all the answers I received
previously. Where did I get it wrong this time?

/Why Tea

I don''t understand your question. You''ve demonstrated that you can cast one
pointer type to another and then that the compiler will treat the new
pointer as it is supposed to treat it.

When you use a statement such as:

pointer++;

or

pointer += integer_offset;

the compiler has to consider the pointer type (what it points to) when
deciding how to offset the pointer. So, at the machine arithmetic level, a
pointer to apples will get treated differently than a pointer to pears.

More specifically:

Pear *ppear;
Apple *papple;

ppear = (Pear *)(papple = malloc(sizeof(Apple * 100)));

/* ppear and papple are now equal */

ppear++;
papple++;

/* ppear and papple are now probably not equal if Pear and Apple are not of
equal size */

will create unequal pointers (in general).

What you have posted is no surprise. This is the way the compiler should
behave.

In your next post (call is Part 3), please identify your question clearly.
You''ve thrown an apple up in the air, and it came back down. Newton knew
this. I know this. We all know this.

What is your question? You''ve demonstrated that the compiler behaves as it
should.

Your question please?


What you have posted is no surprise. This is the way the compiler should
behave.

In your next post (call is Part 3), please identify your question clearly.
You''ve thrown an apple up in the air, and it came back down. Newton knew
this. I know this. We all know this.

What is your question? You''ve demonstrated that the compiler behaves as it
should.

Your question please?

I am sorry David. It was related to a question that I posted previously
with the same topic. I got some excellent answers from Eric Sosman,
Richard Heathfield, etc. But there is still something I''m not totally
clear, that''s why I posted part two of the question. I should probably
have ping''ed Eric and Richard on the topic.


"Why Tea" <yt****@gmail.comwrote in message
news:11**********************@n51g2000cwc.googlegr oups.com...

>

>What you have posted is no surprise. This is the way the compiler should
behave.

In your next post (call is Part 3), please identify your question
clearly.
You''ve thrown an apple up in the air, and it came back down. Newton knew
this. I know this. We all know this.

What is your question? You''ve demonstrated that the compiler behaves as
it
should.

Your question please?


I am sorry David. It was related to a question that I posted previously
with the same topic. I got some excellent answers from Eric Sosman,
Richard Heathfield, etc. But there is still something I''m not totally
clear, that''s why I posted part two of the question. I should probably
have ping''ed Eric and Richard on the topic.

I''m not annoyed. I''m just curious. No apology necessary.

What aspect of the compiler''s behavior has surprised you? It seems to me
that, based on what you posted, it is behaving as it should.


这篇关于malloc问题,第2部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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