C++ interview C/C++µÄ¶ÑÕ»£¬ÄÚ´æ·ÖÅä
C/C++µÄ¶ÑÕ»£¬ÄÚ´æ·ÖÅä
2008Äê12ÔÂ02ÈÕ ÐÇÆÚ¶þ 11:51
Ò»¡¢Ò»¸ö¾¹ý±àÒëµÄC/C++µÄ³ÌÐòÕ¼ÓõÄÄÚ´æ·Ö³ÉÒÔϼ¸¸ö²¿·Ö£º
1¡¢Õ»Çø£¨stack£©£ºÓɱàÒëÆ÷×Ô¶¯·ÖÅäºÍÊÍ·Å £¬´æ·Åº¯ÊýµÄ²ÎÊýÖµ¡¢¾Ö²¿±äÁ¿µÄÖµµÈ£¬ÉõÖÁº¯ÊýµÄµ÷Óùý³Ì¶¼ÊÇÓÃÕ»À´Íê³É¡£Æä²Ù×÷·½Ê½ÀàËÆÓÚÊý¾Ý½á¹¹ÖеÄÕ»¡£
2¡¢¶ÑÇø£¨heap£© £ºÒ»°ãÓɳÌÐòÔ±ÊÖ¶¯ÉêÇëÒÔ¼°ÊÍ·Å£¬ Èô³ÌÐòÔ±²»ÊÍ·Å£¬³ÌÐò½áÊøʱ¿ÉÄÜÓÉOS»ØÊÕ ¡£×¢ÒâËüÓëÊý¾Ý½á¹¹ÖеĶÑÊÇÁ½»ØÊ£¬·ÖÅ䷽ʽÀàËÆÓÚÁ´±í¡£
3¡¢È«¾ÖÇø£¨¾²Ì¬Çø£©£¨static£©£ºÈ«¾Ö±äÁ¿ºÍ¾²Ì¬±äÁ¿µÄ´æ´¢ÊÇ·ÅÔÚÒ»¿éµÄ£¬³õʼ»¯µÄÈ«¾Ö±äÁ¿ºÍ¾²Ì¬±äÁ¿ÔÚÒ»¿éÇøÓò£¬ δ³õʼ»¯µÄÈ«¾Ö±äÁ¿ºÍδ³õʼ»¯µÄ¾²Ì¬±äÁ¿ÔÚÏàÁÚµÄÁíÒ»¿éÇøÓò¡£³ÌÐò½áÊøºóÓÉϵͳÊͷſռ䡣
4¡¢ÎÄ×Ö³£Á¿Çø£º³£Á¿×Ö·û´®¾ÍÊÇ·ÅÔÚÕâÀïµÄ¡£ ³ÌÐò½áÊøºóÓÉϵͳÊͷſռ䡣
5¡¢³ÌÐò´úÂëÇø£º´æ·Åº¯ÊýÌåµÄ¶þ½øÖÆ´úÂë¡£
ÏÂÃæµÄÀý×Ó¿ÉÒÔÍêȫչʾ²»Í¬µÄ±äÁ¿ËùÕ¼µÄÄÚ´æÇøÓò£º
//main.cpp
int a = 0; È«¾Ö³õʼ»¯Çø
char *p1; È«¾Öδ³õʼ»¯Çø
main()
{
int b; //Õ»ÖÐ
char s[] = "abc"; //Õ»ÖÐ
char *p2; //Õ»ÖÐ
char *p3 = "123456"; //123456\0ÔÚ³£Á¿Çø£¬p3ÔÚÕ»ÉÏ
static int c =0£» //È«¾Ö£¨¾²Ì¬£©³õʼ»¯Çø
//ÒÔÏ·ÖÅäµÃµ½µÄ10ºÍ20×Ö½ÚµÄÇøÓò¾ÍÔÚ¶ÑÇø
p1 = (char *)malloc(10);
p2 = new char[20];//(char *)malloc(20);
strcpy(p1, "123456"); //123456\0·ÅÔÚ³£Á¿Çø£¬±àÒëÆ÷¿ÉÄܻὫËüÓëp3ËùÖ¸ÏòµÄ"123456"ÓÅ»¯³ÉÒ»¸öµØ·½¡£
}
¶þ¡¢Õ»£¨stack£©ºÍ¶Ñ£¨heap£©¾ßÌåµÄÇø±ð¡£
1¡¢ÔÚÉêÇ뷽ʽÉÏ
Õ»£¨stack£©: ÏÖÔںܶàÈ˶¼³Æ֮Ϊ¶ÑÕ»£¬Õâ¸öʱºòʵ¼ÊÉÏ»¹ÊÇÖ¸µÄÕ»¡£ËüÓɱàÒëÆ÷×Ô¶¯¹ÜÀí£¬ÎÞÐèÎÒÃÇÊÖ¹¤¿ØÖÆ¡£ ÀýÈ磬ÉùÃ÷º¯ÊýÖеÄÒ»¸ö¾Ö²¿±äÁ¿ int b ϵͳ×Ô¶¯ÔÚÕ»ÖÐΪb¿ª±Ù¿Õ¼ä£»ÔÚµ÷ÓÃÒ»¸öº¯Êýʱ£¬ÏµÍ³×Ô¶¯µÄ¸øº¯ÊýµÄÐβαäÁ¿ÔÚÕ»Öпª±Ù¿Õ¼ä¡£
¶Ñ£¨heap£©: ÉêÇëºÍÊÍ·ÅÓɳÌÐòÔ±¿ØÖÆ£¬²¢Ö¸Ã÷´óС¡£ÈÝÒײúÉúmemory leak¡£
ÔÚCÖÐʹÓÃmallocº¯Êý¡£
È磺p1 = (char *)malloc(10);
ÔÚC++ÖÐÓÃnewÔËËã·û¡£
È磺p2 = new char[20];//(char *)malloc(10);
µ«ÊÇ×¢Òâp1±¾ÉíÔÚÈ«¾ÖÇø£¬¶øp2±¾ÉíÊÇÔÚÕ»Öеģ¬Ö»ÊÇËüÃÇÖ¸ÏòµÄ¿Õ¼äÊÇÔÚ¶ÑÖС£
2¡¢ÉêÇëºóϵͳµÄÏìÓ¦ÉÏ
Õ»£¨stack£©:Ö»ÒªÕ»µÄÊ£Óà¿Õ¼ä´óÓÚËùÉêÇë¿Õ¼ä£¬ÏµÍ³½«Îª³ÌÐòÌṩÄڴ棬·ñÔò½«±¨Òì³£ÌáʾջÒç³ö¡£
¶Ñ£¨heap£©:
Ê×ÏÈÓ¦¸ÃÖªµÀ²Ù×÷ϵͳÓÐÒ»¸ö¼Ç¼¿ÕÏÐÄÚ´æµØÖ·µÄÁ´±í£¬µ±ÏµÍ³ÊÕµ½³ÌÐòµÄÉêÇëʱ£¬»á±éÀú¸ÃÁ´±í£¬Ñ°
Ïà¹ØÎĵµ£º
ÕâƪÎÄÕÂÊÇʹÓÃSQLite C/C++½Ó¿ÚµÄÒ»¸ö¸ÅÒª½éÉܺÍÈëÃÅÖ¸ÄÏ¡£
ÓÉÓÚÔçÆÚµÄSQLiteÖ»Ö§³Ö5¸öC/C++½Ó¿Ú£¬Òò¶ø·Ç³£ÈÝÒ×ѧϰºÍʹÓ㬵«ÊÇËæ×ÅSQLite¹¦ÄܵÄÔöÇ¿£¬ÐµÄC/C++½Ó¿Ú²»¶ÏµÄÔö¼Ó½øÀ´£¬µ½ÏÖÔÚÓг¬¹ý150¸ö²»Í¬µÄAPI½Ó¿Ú¡£ÕâÍùÍùʹ³õѧÕßÍû¶øÈ´²½¡£ÐÒÔ˵ÄÊÇ£¬´ó¶àÊýSQLiteÖеÄC/C++½Ó¿ÚÊÇרÓõģ¬Òò¶øºÜÉÙ±»Ê¹Óõ½¡£¾¡¹ÜÓÐÕâ ......
µ±ÔÚC Öж¨ÒåÁËÒ»¸ö½á¹¹ÀàÐÍʱ£¬ËüµÄ´óСÊÇ·ñµÈÓÚ¸÷×Ö¶Î(field) ´óС֮ºÍ£¿±àÒëÆ÷½«ÈçºÎÔÚÄÚ´æÖзÅÖÃÕâЩ×ֶΣ¿ANSI C¶Ô½á¹¹ÌåµÄÄÚ´æ²¼¾ÖÓÐʲôҪÇ󣿶øÎÒÃǵijÌÐòÓÖÄÜ·ñÒÀÀµÕâÖÖ²¼¾Ö£¿ÕâЩÎÊÌâ»òÐí¶Ô²»ÉÙÅóÓÑÀ´Ëµ»¹ÓеãÄ£ºý£¬ÄÇô±¾ÎľÍÊÔ×Å̽¾¿ËüÃDZ³ºóµÄÃØÃÜ¡£
Ê×ÏÈ£¬ÖÁÉÙÓÐÒ»µã¿ÉÒԿ϶¨£¬ÄǾÍÊÇANSI C ±£Ö¤½á¹¹ÌåÖи÷× ......
ÓÉÓÚ³ÌÐòÔËÐÐʱռÓõÄÄÚ´æ¹ý´ó£¬ËùÒÔÏë°ì·¨¸ø³ÌÐòÊÝÉí¡£
ÔÚµ÷ÊÔÖз¢ÏֽṹÌåÕ¼ÓõÄsize¾¹È»ºÍÔ¤ÏëµÄ²»Ò»Ñù£¬ÔÀ´……
¿´¿´ÏÂÃæ½²µÄ°É£¬¿Ï¶¨»á²»Í÷´Ë¿´Å¶£¡
1,±ÈÈç:
struct{
short a1;
short a2;
short a3;
}A;
struct{
long a1;
short a2;
}B;
sizeof(A)=6, sizeof(B)=8,Ϊʲô£¿
×¢£ºsizeof(sho ......
1.1.1 ¸ñʽ»¯ÊäÈëÊä³öº¯Êý
Turbo C2.0 ±ê×¼¿âÌṩÁËÁ½¸ö¿ØÖÆ̨¸ñʽ»¯ÊäÈë¡¢ Êä³öº¯Êýprintf( ) ºÍ
scanf(), ÕâÁ½¸öº¯Êý¿ÉÒÔÔÚ±ê×¼ÊäÈëÊä³öÉ豸ÉÏÒÔ¸÷ÖÖ²»Í¬µÄ¸ñʽ¶ÁдÊý¾Ý¡£
printf()º¯ÊýÓÃÀ´Ïò±ê×¼Êä³öÉ豸(ÆÁÄ»)дÊý¾Ý; scanf() º¯ÊýÓÃÀ´´Ó±ê×¼ÊäÈë
É豸(¼üÅÌ)É϶ÁÊý¾Ý¡£ÏÂÃæÏêϸ½éÉÜÕâÁ½¸öº¯ÊýµÄÓ÷¨¡£
Ò»¡¢pr ......
ÎÒÃÇÒѾÁ˽âÈçºÎ¶¨ÒåÏß³ÌÈë¿Úµãº¯Êý¡¢µ÷ÓÃϵͳAPI´´½¨Ö´ÐÐÖ¸¶¨º¯ÊýµÄÏ̡߳£±¾½Ú½«½ÒʾÕâÒ»ÇÐÔÚϵͳÄÚ²¿ÊÇÈçºÎÍê³ÉµÄ¡£
ͼ6-1ÃèÊöÁËÏ̴߳´½¨²¢Íê³É³õʼ»¯ºóµÄ״̬¡£µ÷ÓÃCreateThread»áʹϵͳ²úÉúÒ»¸öÏß³ÌÄں˶ÔÏó£¬ÆäÒýÓüÆÊý£¨Usage count£©±»³õʼ»¯Îª2£¨´´½¨Ï̵߳Ľø³ÌºÍÏ̱߳¾Éí¶¼ÒýÓÃÁ˸ÃÄں˶ÔÏ󣩣¬ÆäËüÊôÐÔÒ²Íê³ÉÁË ......