分配内存 [英] Allocated memory

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

问题描述

我有类似下面的代码,这里是简化版。我没有编译并运行以下代码,所以不要担心语法。

我的问题是我记得一个更大的数据(10Byte)到较小的一个(1B),

然而我仍然可以访问所有数据(参见代码中的printf)。我想
假设堆栈大小通常足够大(告诉我

否则)。但是这里的潜在问题是什么?

我认为它可能会搞砸指针mStruct.buf。但事实并非如此。这是

合法吗?如果没有,为什么它可以合并?这有什么问题?非常感谢。


char tenChars [10];

struct myStruct

{

int a;

char oneChar [1];

char * buf;

}


main ()

{

struct myStruct mStruct;

char tenChar [10];


memset (tenChar,1,10);

memcpy(mStruct.oneChar,& tenChars,10);

for(i = 0; i< 10; i ++)

printf("%c\ n",buf [i];

}

---------- ------------------------

删除(无 - 垃圾邮件)以回复


I have a similar code to the following, here is the simplified version. I
didn''t compile and run the following code, so don''t worry about the syntax.
My question is I memcpy a larger data (10Byte) to a smaller one (1B),
however i can still access all the data (ref. to printf in the code). I
suppose the stack size is usually big enough for this to happen (tell me
otherwise). But what''s the potential problem here?
I thought it might screw up the pointer, mStruct.buf. But it''s not. Is this
legal? if not, why it''s compliable? any problem here? thanks a lot.

char tenChars[10];
struct myStruct
{
int a;
char oneChar[1];
char * buf;
}

main()
{
struct myStruct mStruct;
char tenChar[10];

memset( tenChar, 1, 10);
memcpy( mStruct.oneChar, &tenChars, 10);
for (i=0; i<10; i++)
printf("%c\n", buf[i];
}
----------------------------------
remove "(n.o---s.p.a.m)" to reply


推荐答案




john写道:


john wrote:
我有下面是类似的代码,这里是简化版本。我没有编译并运行下面的代码,所以不要担心语法。
我的问题是我记得更大数据(10Byte)到较小的(1B),然而我仍然可以访问所有数据(参考。在代码中printf)。我认为堆栈大小通常足以让这种情况发生(告诉我
否则)。但是这里的潜在问题是什么?
我认为这可能会搞砸指针mStruct.buf。但事实并非如此。这是合法的吗?如果没有,为什么它可以合并?这有什么问题?非常感谢。


在下面的例子中,它是合法的(参见附注),但它是非常糟糕的形式,并且可能不适用于所有编译器。


这样做的原因是myStruct在32位机器上的大小通常为12字节,在64位机器上大小为24位。这是

结构填充。通过对齐内存中的字段以匹配机器

字大小,可以获得相当大的(?)性能。

char tenChars [10];
struct myStruct
{
int a;
char oneChar [1];
char * buf;
}

main()
{
struct myStruct mStruct;
char tenChar [10];

memset(tenChar,1,10);


更改为:strncpy(tenChar," 0123456789",10);

memcpy(mStruct.oneChar,& tenChars,10);
for(i = 0; i< 10; i ++)
printf("%c\ n",buf [i];
}


现在,打印mStruct.oneChar [0]应显示4或8,

,具体取决于您的机器字大小。请尝试以下(结果

将根据机器的字节顺序而变化):


printf("%08x \ n",mStruct.a); //应该像30313233

printf("%08x \ n",(unsigned)mStruct.buf); //应该像383900 ??

------ ----------------------------
删除(不 - 垃圾邮件)回复
I have a similar code to the following, here is the simplified version. I
didn''t compile and run the following code, so don''t worry about the syntax.
My question is I memcpy a larger data (10Byte) to a smaller one (1B),
however i can still access all the data (ref. to printf in the code). I
suppose the stack size is usually big enough for this to happen (tell me
otherwise). But what''s the potential problem here?
I thought it might screw up the pointer, mStruct.buf. But it''s not. Is this
legal? if not, why it''s compliable? any problem here? thanks a lot.
In your example below, it is legal (see further note), but it''s
exceedingly bad form and may not work on all compilers.

The reason this works is that myStruct is typically 12 bytes in size on
a 32-bit machine and 24 bits in size on a 64-bit machine. This is
structure padding. By aligning the fields in memory to match the machine
word size, considerable(?) performance is gained.

char tenChars[10];
struct myStruct
{
int a;
char oneChar[1];
char * buf;
}

main()
{
struct myStruct mStruct;
char tenChar[10];

memset( tenChar, 1, 10);
Change this to: strncpy( tenChar, "0123456789", 10);
memcpy( mStruct.oneChar, &tenChars, 10);
for (i=0; i<10; i++)
printf("%c\n", buf[i];
}

Now, printing mStruct.oneChar[0] should show you either 4, or 8,
depending upon your machine word size. Try the following (the results
will vary based upon the endian-ness of your machine):

printf( "%08x\n", mStruct.a ); // Should be something like 30313233
printf( "%08x\n", (unsigned) mStruct.buf ); // Should be like 383900??


----------------------------------
remove "(n.o---s.p.a.m)" to reply






" john"< john2000(no --- spam)@ blueyonder.co.uk>写在留言

新闻:%r ******************* @ front-1.news.blueyonder.co.uk ...
"john" <john2000(n.o---s.p.a.m)@blueyonder.co.uk> wrote in message
news:%r*******************@front-1.news.blueyonder.co.uk...
我有类似的代码,下面是简化版。我没有编译并运行以下代码,所以不要担心
语法。我的问题是我将较大的数据(10Byte)记忆到较小的数据(1B),但是我仍然可以访问所有数据(参见代码中的printf)。我认为堆栈大小通常足以让这种情况发生(告诉我
否则)。但这里的潜在问题是什么?


您的代码写入mStruct.oneChar的末尾,这是未定义的

行为。在某些系统上它可能会起作用,但是在我的系统上它会像我预期的那样转储核心 -



我认为它可能会搞砸指针mStruct.buf。但事实并非如此。这是合法的
吗?如果没有,为什么它可以合并?这有什么问题?非常感谢。


仅仅因为它未定义的行为并不意味着它不会编译。大多数C

的翻译人员会给你足够的绳索,而不需要花费多少b $ b警告;小心不这样做是_your_责任,而不是

翻译'。

下面修正各种错误:

char tenChars [10 ];
struct myStruct
{
int a;
char oneChar [1];
char * buf;
}


};

main()


int main()

{
struct myStruct mStruct ;
char tenChar [10];


int i;

memset(tenChar,1,10);
memcpy(mStruct.oneChar,& tenChars,10);
for(i = 0; i< 10; i ++)
printf("%c\ nn",buf [i];


printf(") ;%c \ n",mStruct.buf [i]);

}
I have a similar code to the following, here is the simplified version. I
didn''t compile and run the following code, so don''t worry about the syntax. My question is I memcpy a larger data (10Byte) to a smaller one (1B),
however i can still access all the data (ref. to printf in the code). I
suppose the stack size is usually big enough for this to happen (tell me
otherwise). But what''s the potential problem here?
Your code writes past the end of mStruct.oneChar, which is undefined
behavior. On some systems it might work, but on my system it dumps core --
just like I expected.
I thought it might screw up the pointer, mStruct.buf. But it''s not. Is this legal? if not, why it''s compliable? any problem here? thanks a lot.
Just because it''s undefined behavior doesn''t mean it won''t compile. Most C
translators will give you enough rope to hang yourself without so much as a
warning; taking care not to do so is _your_ responsibility, not the
translator''s.
Various errors fixed below:
char tenChars[10];
struct myStruct
{
int a;
char oneChar[1];
char * buf;
}
};
main()
int main()
{
struct myStruct mStruct;
char tenChar[10];
int i;
memset( tenChar, 1, 10);
memcpy( mStruct.oneChar, &tenChars, 10);
for (i=0; i<10; i++)
printf("%c\n", buf[i];
printf("%c\n", mStruct.buf[i]);
}



S


-

Stephen Sprunk愚蠢的人用智能环绕自己

CCIE#3723人。聪明的人围着自己用

K5SSS聪明人不同意他们。 - Aaron Sorkin


S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin


john写道:
我有类似的代码如下,这里是简化版。我没有编译并运行以下代码,所以不要担心语法。
我的问题是我将更大的数据(10Byte)记忆到更小的数据中(1B),然而我仍然可以访问所有数据(参见代码中的printf)。我想假设e堆栈大小通常足以让这种情况发生(告诉我
否则)。但是这里的潜在问题是什么?
I have a similar code to the following, here is the simplified version. I
didn''t compile and run the following code, so don''t worry about the syntax.
My question is I memcpy a larger data (10Byte) to a smaller one (1B),
however i can still access all the data (ref. to printf in the code). I
suppose the stack size is usually big enough for this to happen (tell me
otherwise). But what''s the potential problem here?




问题是你要将数据从大容器移到

a非常小的容器。就像现实和物理状态一样,你有/ b $ b创建了未定义的行为。容器可能会溢出,可能会破裂,

它可能会爆炸。


你为什么要这样做?

-

Thomas Matthews


C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo .com /~scs / c-faq / top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

其他网站:
http://www.josuttis。 com - C ++ STL图书馆书



The problem is that you are moving data from a large container to
a very small container. Just as reality and physics state, you have
created undefined behavior. The container may overflow, it may break,
it may explode.

Why are you doing this?
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


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

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