当前位置: 技术问答>linux和unix
求关于memset一个特例的原因?
来源: 互联网 发布时间:2017-05-30
本文导语: char name[6]; memset(name,"11",6); 打印出来是0x959595959595 我知道这个是错的,应该用一个‘1’。但是我想知道这个怎么出来的0x95; 球大神解释。。 | The memset() function fills the first n bytes of the memory area...
char name[6];
memset(name,"11",6);
打印出来是0x959595959595
我知道这个是错的,应该用一个‘1’。但是我想知道这个怎么出来的0x95;
球大神解释。。
|
The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c.
if u look at the general memory layout of a c program, constant char literal also has an address in initialized data section,which is read-only.
u mistakenly sent this address to the memset and memset only takes the first byte from this 4 or 8 bytes long address then convert it to a byte long integer, in your case, it's 0x95. The the all 6 bytes of char array name were filled with this integer.
If you add -Wall on gcc commandline, you should be able to see the warning for this invalid type conversion...
if u look at the general memory layout of a c program, constant char literal also has an address in initialized data section,which is read-only.
u mistakenly sent this address to the memset and memset only takes the first byte from this 4 or 8 bytes long address then convert it to a byte long integer, in your case, it's 0x95. The the all 6 bytes of char array name were filled with this integer.
If you add -Wall on gcc commandline, you should be able to see the warning for this invalid type conversion...
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。