c/c++ ÄÚ´æ¶ÔÆë sizeof
һƪ½²¶ÔÆë±È½ÏºÃµÄÎÄÕ£¬×ªÌùÈçÏÂ:
´óÖ±ê×¢Ò»¶þ:
1.ÄÚ´æ¶ÔÆë:¼ÆËã»úϵͳ¶Ô»ù±¾ÀàÐÍÊý¾ÝÔÚÄÚ´æÖдæ·ÅµÄλÖÃÓÐÏÞÖÆ£¬ËüÃÇ»áÒªÇóÕâЩÊý¾ÝµÄÊ×µØÖ·µÄÖµÊÇij¸öÊýk(ͨ³£ËüΪ4»ò8)µÄ±¶ÊýkÔò±»³ÆΪ¸ÃÊý¾ÝÀàÐ͵ĶÔÆëÄ£Êý(alignment modulus)
2.²»Í¬±àÒëÆ÷ĬÈϵÄ×î´ó¶ÔÆë×Ö½ÚÊýÊDz»Ò»ÑùµÄ,±ÈÈçvc==8,gcc==4,¿ÉÒÔͨ¹ý#progma pack (n)À´ÐÞ¸Ä,·ÖÎö³ÌÐòµÄʱºòҪעÒâ±àÒëÆ÷µÄÇø±ð
3.Ò»¸ö½á¹¹ÌåÀïÃæ,°´ÕÕalignment modulus×î´óµÄÊý¾Ý³ÉÔ±À´½øÐжÔÆë,³¬¹ý±àÒëÆ÷¹æ¶¨×î´óµÄ¶ÔÆë×Ö½Ú¸öÊý,°´±àÒëÆ÷×î´ó¶ÔÆë×Ö½Ú¸öÀ´
4.doubleÀàÐÍÔÚvcÀïÃæalignment modulus == 8,¶øÔÚgccÀïÃæÓÉÓÚĬÈÏ×î´ó¶ÔÆë¸öÊýÊÇ4,²»ÉèÖõĻ°,alignment modulus == 4
5.ÒòΪÊý×é¸÷ÔªËØÖ®¼ä²»ÄÜÓпÕ϶,ËùÒÔ{int a;char b;}
ÕâÖÖÇé¿ö,ĬÈÏÔÚVCÀïÃæÒ²ÐèÒªÕ¼8¸ö×Ö½Ú.
ÕýÎÄÈçÏ£º
µ±ÔÚCÖж¨ÒåÁËÒ»¸ö½á¹¹ÀàÐÍʱ£¬ËüµÄ´óСÊÇ·ñµÈÓÚ¸÷×Ö¶Î(field)´óС֮ºÍ£¿±àÒëÆ÷½«ÈçºÎÔÚÄÚ´æÖзÅÖÃÕâЩ×ֶΣ¿ANSI C¶Ô½á¹¹ÌåµÄÄÚ´æ²¼¾ÖÓÐʲôҪÇ󣿶øÎÒÃǵijÌÐòÓÖÄÜ·ñÒÀÀµÕâÖÖ²¼¾Ö£¿ÕâЩÎÊÌâ»òÐí¶Ô²»ÉÙÅóÓÑÀ´Ëµ»¹ÓеãÄ£ºý£¬ÄÇô±¾ÎľÍÊÔ×Å̽¾¿ËüÃDZ³ºóµÄÃØÃÜ¡£
Ê×ÏÈ£¬ÖÁÉÙÓÐÒ»µã¿ÉÒԿ϶¨£¬ÄǾÍÊÇANSI C±£Ö¤½á¹¹ÌåÖи÷×Ö¶ÎÔÚÄÚ´æÖгöÏÖµÄλÖÃÊÇËæËüÃǵÄÉùÃ÷˳ÐòÒÀ´ÎµÝÔöµÄ£¬²¢ÇÒµÚÒ»¸ö×ֶεÄÊ×µØÖ·µÈÓÚÕû¸ö½á¹¹ÌåʵÀýµÄÊ×µØÖ·¡£±ÈÈçÓÐÕâÑùÒ»¸ö½á¹¹Ì壺
struct vector{int x,y,z;} s;
int *p,*q,*r;
struct vector *ps;
p = &s.x;
q = &s.y;
r = &s.z;
ps = &s;
assert(p < q);
assert(p < r);
assert(q < r);
assert((int*)ps == p);
// ÉÏÊö¶ÏÑÔÒ»¶¨²»»áʧ°Ü
Õâʱ£¬ÓÐÅóÓÑ¿ÉÄÜ»áÎÊ:"±ê×¼ÊÇ·ñ¹æ¶¨ÏàÁÚ×Ö¶ÎÔÚÄÚ´æÖÐÒ²ÏàÁÚ?"¡£ ßí£¬¶Ô²»Æð£¬ANSI CûÓÐ×ö³ö±£Ö¤£¬ÄãµÄ³ÌÐòÔÚÈκÎʱºò¶¼²»Ó¦¸ÃÒÀÀµÕâ¸ö¼ÙÉè¡£ÄÇÕâÊÇ·ñÒâζ×ÅÎÒÃÇÓÀÔ¶ÎÞ·¨¹´ÀÕ³öÒ»·ù¸üÇåÎú¸ü¾«È·µÄ½á¹¹ÌåÄÚ´æ²¼¾Öͼ£¿Å¶£¬µ±È»²»ÊÇ¡£²»¹ýÏÈÈÃÎÒÃÇ´ÓÕâ¸öÎÊÌâÖÐÔÝʱ³éÉí£¬¹Ø×¢Ò»ÏÂÁíÒ»¸öÖØÒªÎÊÌâ————ÄÚ´æ¶ÔÆë¡£
Ðí¶àʵ¼ÊµÄ¼ÆËã»úϵͳ¶Ô»ù±¾ÀàÐÍÊý¾ÝÔÚÄÚ´æÖдæ·ÅµÄλÖÃÓÐÏÞÖÆ£¬ËüÃÇ»áÒªÇóÕâЩÊý¾ÝµÄÊ×µØÖ·µÄÖµÊÇij¸öÊýk(ͨ³£ËüΪ4»ò8)µÄ±¶Êý£¬Õâ¾ÍÊÇËùνµÄÄÚ´æ¶ÔÆ룬¶øÕâ¸ökÔò±»³
Ïà¹ØÎĵµ£º
ÉÏÖÜÀÏ°å·ÖÏÂÀ´6¸öְλÈí¼þ¿ª·¢·½ÃæµÄְλ¸øÎÒ£¬ÒªÎÒ°´Ö°Î»ÒªÇóÑ°ÕÒºÏÊʵÄÈ˲š£¾ÓÈ»ÊÇC/C++!¾ÝÎÒËùÖª£¬ÔÚÈ˲ſâÖУ¬JAVA È˲ŵ¹ÊÇÓ¦Óо¡ÓУ¬Ñ§CµÄ£¬»¹ÊÇǶÈëʽ¿ª·¢µÄ¿ÉÕæµÄºÃÉÙ°¡¡£ÎÒÓÖ²»ÊÇŮ洣¬ÄѵÀÎÒ»áÔìÈ˲Åô£¿ÒªÇóÌõ¼þ»¹Õâô¸ß£¡
ÒÔÏÂÊÇ´óÁ¬Èí¼þÔ°¼¸¼ÒÖªÃûÍâÆóίÍÐÎÒÃÇÕÐƸµÄְλÐÅÏ¢¡£
Position 1 Èí¼þ¿ª·¢¹¤³Ìʦ ......
ͨÐÅ
Server£º
#pragma comment(lib, "ws2_32.lib")
#include <Winsock2.h>
#include <stdio.h>
void main()
{
//°æ±¾ÐÉÌ
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1,1); //0x0101
err = WSAStartup ......
CÖÐÈçºÎµ÷ÓÃC++£¿C++ÖÐÈçºÎµ÷ÓÃC?Õâ¸öÎÊÌâÊÇÐí¶àÈí¼þ¹«Ë¾ÃæÊÔ¹ý³ÌÖлáÎʵ½µÄÒ»¸ö³£¼ûÎÊÌâ¡£»Ø´ðÕâ¸öÎÊÌâ²¢²»À§ÄÑ£¬ÄǾÍÊÇʹÓÃ
extern "C",µ«ÊÇÃæÊÔ¹Ù¿ÉÄÜÏëͨ¹ýÕâ¸öÎÊÌâÁ˽âÄã¶ÔC/C++ÕÆÎյij̶ȡ£ÏÂÃæ×Ðϸ·ÖÎö¸ÃÎÊÌâ¡£
Ê×ÏÈ£¬×÷
ΪexternÊÇC/C++ÓïÑÔÖбíÃ÷º¯ÊýºÍÈ«¾Ö±äÁ¿×÷Ó÷¶Î§£¨¿ ......