C - C++³ÌÐòԱӦƸ³£¼ûÃæÊÔÌâÉîÈëÆÊÎö ÏÂÔر¾ÎÄ

ÊÔÌâ6µÄTestº¯ÊýÖÐҲδ¶ÔmallocµÄÄÚ´æ½øÐÐÊÍ·Å¡£

ÆÊÎö£º

ÊÔÌâ4¡«7¿¼²éÃæÊÔÕ߶ÔÄÚ´æ²Ù×÷µÄÀí½â³Ì¶È£¬»ù±¾¹¦ÔúʵµÄÃæÊÔÕßÒ»°ã¶¼ÄÜÕýÈ·µÄ»Ø´ðÆäÖÐ50~60µÄ´íÎó¡£µ«ÊÇÒªÍêÈ«½â´ðÕýÈ·£¬È´Ò²¾ø·ÇÒ×Ê¡£

¶ÔÄÚ´æ²Ù×÷µÄ¿¼²éÖ÷Òª¼¯ÖÐÔÚ£º

£¨1£©Ö¸ÕëµÄÀí½â£»

£¨2£©±äÁ¿µÄÉú´æÆÚ¼°×÷Ó÷¶Î§£»

£¨3£©Á¼ºÃµÄ¶¯Ì¬ÄÚ´æÉêÇëºÍÊÍ·ÅÏ°¹ß¡£

ÔÙ¿´¿´ÏÂÃæµÄÒ»¶Î³ÌÐòÓÐʲô´íÎó£º

swap( int* p1,int* p2 ) {

int *p; *p = *p1; *p1 = *p2; *p2 = *p; }

ÔÚswapº¯ÊýÖУ¬pÊÇÒ»¸ö¡°Ò°¡±Ö¸Õ룬ÓпÉÄÜÖ¸ÏòϵͳÇø£¬µ¼Ö³ÌÐòÔËÐеıÀÀ£¡£ÔÚVC++ÖÐDEBUGÔËÐÐʱÌáʾ´íÎó¡°Access Violation¡±¡£¸Ã³ÌÐòÓ¦¸Ã¸ÄΪ£º

swap( int* p1,int* p2 ) {

int p; p = *p1; *p1 = *p2; *p2 = p; }

3.ÄÚ¹¦Ìâ

ÊÔÌâ1£º·Ö±ð¸ø³öBOOL£¬int£¬float£¬Ö¸Õë±äÁ¿ Óë¡°ÁãÖµ¡±±È½ÏµÄ if Óï¾ä£¨¼ÙÉè±äÁ¿ÃûΪvar£©

½â´ð£º

BOOLÐͱäÁ¿£ºif(!var)

intÐͱäÁ¿£º if(var==0)

floatÐͱäÁ¿£º

const float EPSINON = 0.00001;

if ((x >= - EPSINON) && (x <= EPSINON)

Ö¸Õë±äÁ¿£º if(var==NULL)

ÆÊÎö£º

¿¼²é¶Ô0ÖµÅжϵġ°ÄÚ¹¦¡±£¬BOOLÐͱäÁ¿µÄ0ÅжÏÍêÈ«¿ÉÒÔд³Éif(var==0)£¬¶øintÐͱäÁ¿Ò²¿ÉÒÔд³Éif(!var)£¬Ö¸Õë±äÁ¿µÄÅжÏÒ²¿ÉÒÔд³Éif(!var)£¬ÉÏÊöд·¨ËäÈ»³ÌÐò¶¼ÄÜÕýÈ·ÔËÐУ¬µ«ÊÇδÄÜÇåÎúµØ±í´ï³ÌÐòµÄÒâ˼¡£

Ò»°ãµÄ£¬Èç¹ûÏëÈÃifÅжÏÒ»¸ö±äÁ¿µÄ¡°Õ桱¡¢¡°¼Ù¡±£¬Ó¦Ö±½ÓʹÓÃif(var)¡¢if(!var)£¬±íÃ÷ÆäΪ¡°Âß¼­¡±Åжϣ»Èç¹ûÓÃifÅжÏÒ»¸öÊýÖµÐͱä Á¿(short¡¢int¡¢longµÈ)£¬Ó¦¸ÃÓÃif(var==0)£¬±íÃ÷ÊÇÓë0½øÐС°ÊýÖµ¡±ÉϵıȽϣ»¶øÅжÏÖ¸ÕëÔòÊÊÒËÓÃif(var==NULL)£¬ ÕâÊÇÒ»Öֺܺõıà³ÌÏ°¹ß¡£

¸¡µãÐͱäÁ¿²¢²»¾«È·£¬ËùÒÔ²»¿É½«float±äÁ¿Óá°==¡±»ò¡°£¡=¡±ÓëÊý×ֱȽϣ¬Ó¦¸ÃÉ跨ת»¯³É¡°>=¡±»ò¡°<=¡±ÐÎʽ¡£Èç¹ûд³Éif (x == 0.0)£¬ÔòÅÐΪ´í£¬µÃ0·Ö¡£

ÊÔÌâ2£ºÒÔÏÂΪWindows NTϵÄ32λC++³ÌÐò£¬Çë¼ÆËãsizeofµÄÖµ

void Func ( char str[100] ) {

sizeof( str ) = ? }

void *p = malloc( 100 ); sizeof ( p ) = ?

½â´ð£º

sizeof( str ) = 4 sizeof ( p ) = 4

ÆÊÎö£º

Func ( char str[100] )º¯ÊýÖÐÊý×éÃû×÷Ϊº¯ÊýÐβÎʱ£¬ÔÚº¯ÊýÌåÄÚ£¬Êý×éÃûʧȥÁ˱¾ÉíµÄÄÚº­£¬½ö½öÖ»ÊÇÒ»¸öÖ¸Õ룻ÔÚʧȥÆäÄÚº­µÄͬʱ£¬Ëü»¹Ê§È¥ÁËÆä³£Á¿ÌØÐÔ£¬¿ÉÒÔ

×÷×ÔÔö¡¢×Ô¼õµÈ²Ù×÷£¬¿ÉÒÔ±»Ð޸ġ£

Êý×éÃûµÄ±¾ÖÊÈçÏ£º

£¨1£©Êý×éÃûÖ¸´úÒ»ÖÖÊý¾Ý½á¹¹£¬ÕâÖÖÊý¾Ý½á¹¹¾ÍÊÇÊý×飻

ÀýÈ磺

char str[10];

cout << sizeof(str) << endl;

Êä³ö½á¹ûΪ10£¬strÖ¸´úÊý¾Ý½á¹¹char[10]¡£

£¨2£©Êý×éÃû¿ÉÒÔת»»ÎªÖ¸ÏòÆäÖ¸´úʵÌåµÄÖ¸Õ룬¶øÇÒÊÇÒ»¸öÖ¸Õë³£Á¿£¬²»ÄÜ×÷×ÔÔö¡¢×Ô¼õµÈ²Ù×÷£¬²»Äܱ»Ð޸ģ»

char str[10];

str++; //±àÒë³ö´í£¬Ìáʾstr²»ÊÇ×óÖµ

£¨3£©Êý×éÃû×÷Ϊº¯ÊýÐβÎʱ£¬ÂÙΪÆÕָͨÕë¡£

Windows NT 32λƽ̨Ï£¬Ö¸ÕëµÄ³¤¶È£¨Õ¼ÓÃÄÚ´æµÄ´óС£©Îª4×Ö½Ú£¬¹Êsizeof( str ) ¡¢sizeof ( p ) ¶¼Îª4¡£

ÊÔÌâ3£ºÐ´Ò»¸ö¡°±ê×¼¡±ºêMIN£¬Õâ¸öºêÊäÈëÁ½¸ö²ÎÊý²¢·µ»Ø½ÏСµÄÒ»¸ö¡£ÁíÍ⣬µ±ÄãдÏÂÃæµÄ´úÂëʱ»á·¢ÉúʲôÊ£¿

least = MIN(*p++, b);

½â´ð£º

#define MIN(A,B) ((A) <= (B) ? (A) : (B))

MIN(*p++, b)»á²úÉúºêµÄ¸±×÷ÓÃ

ÆÊÎö£º

Õâ¸öÃæÊÔÌâÖ÷Òª¿¼²éÃæÊÔÕ߶Ժ궨ÒåµÄʹÓ㬺궨Òå¿ÉÒÔʵÏÖÀàËÆÓÚº¯ÊýµÄ¹¦ÄÜ£¬µ«ÊÇËüÖչ鲻ÊǺ¯Êý£¬¶øºê¶¨ÒåÖÐÀ¨»¡Öеġ°²ÎÊý¡±Ò²²»ÊÇÕæµÄ²ÎÊý£¬ÔÚºêÕ¹¿ªµÄʱºò¶Ô¡°²ÎÊý¡±½øÐеÄÊÇÒ»¶ÔÒ»µÄÌæ»»¡£

³ÌÐòÔ±¶Ôºê¶¨ÒåµÄʹÓÃÒª·Ç³£Ð¡ÐÄ£¬ÌرðҪעÒâÁ½¸öÎÊÌ⣺

£¨1£©½÷É÷µØ½«ºê¶¨ÒåÖеġ°²ÎÊý¡±ºÍÕû¸öºêÓÃÓÃÀ¨»¡À¨ÆðÀ´¡£ËùÒÔ£¬ÑϸñµØ½²£¬ÏÂ

Êö½â´ð£º

#define MIN(A,B) (A) <= (B) ? (A) : (B) #define MIN(A,B) (A <= B ? A : B )

¶¼Ó¦ÅÐ0·Ö£»

£¨2£©·ÀÖ¹ºêµÄ¸±×÷Óá£

ºê¶¨Òå#define MIN(A,B) ((A) <= (B) ? (A) : (B))¶ÔMIN(*p++, b)µÄ×÷Óýá¹ûÊÇ£º

((*p++) <= (b) ? (*p++) : (*p++))

Õâ¸ö±í´ïʽ»á²úÉú¸±×÷Óã¬Ö¸Õëp»á×÷Èý´Î++×ÔÔö²Ù×÷¡£

³ý´ËÖ®Í⣬ÁíÒ»¸öÓ¦¸ÃÅÐ0·ÖµÄ½â´ðÊÇ£º

#define MIN(A,B) ((A) <= (B) ? (A) : (B));

Õâ¸ö½â´ðÔں궨ÒåµÄºóÃæ¼Ó¡°;¡±£¬ÏÔʾ±àдÕ߶ԺêµÄ¸ÅÄîÄ£ºý²»Ç壬ֻÄܱ»ÎÞÇéµØÅÐ0·Ö²¢±»ÃæÊÔ¹ÙÌÔÌ­¡£

ÊÔÌâ4£ºÎªÊ²Ã´±ê׼ͷÎļþ¶¼ÓÐÀàËÆÒÔϵĽṹ£¿

#ifndef __INCvxWorksh #define __INCvxWorksh #ifdef __cplusplus

extern \#endif /*...*/

#ifdef __cplusplus }

#endif

#endif /* __INCvxWorksh */

½â´ð£º

Í·ÎļþÖеıàÒëºê

#ifndef __INCvxWorksh #define __INCvxWorksh #endif

µÄ×÷ÓÃÊÇ·ÀÖ¹±»Öظ´ÒýÓá£

×÷ΪһÖÖÃæÏò¶ÔÏóµÄÓïÑÔ£¬C++Ö§³Öº¯ÊýÖØÔØ£¬¶ø¹ý³ÌʽÓïÑÔCÔò²»Ö§³Ö¡£º¯Êý±»C++±àÒëºóÔÚsymbol¿âÖеÄÃû×ÖÓëCÓïÑԵIJ»Í¬¡£ÀýÈ磬¼ÙÉèij¸öº¯ÊýµÄÔ­ÐÍΪ£º

void foo(int x, int y);

¸Ãº¯Êý±»C±àÒëÆ÷±àÒëºóÔÚsymbol¿âÖеÄÃû×ÖΪ_foo£¬¶øC++±àÒëÆ÷Ôò»á²úÉúÏñ_foo_int_intÖ®ÀàµÄÃû×Ö¡£_foo_int_intÕâÑùµÄÃû×Ö°üº¬Á˺¯ÊýÃûºÍº¯Êý²ÎÊýÊýÁ¿¼°ÀàÐÍÐÅÏ¢£¬C++¾ÍÊÇ¿¼ÕâÖÖ»úÖÆÀ´ÊµÏÖº¯ÊýÖØÔصġ£

ΪÁËʵÏÖCºÍC++µÄ»ìºÏ±à³Ì£¬C++ÌṩÁËCÁ¬½Ó½»»»Ö¸¶¨·ûºÅextern \À´½â¾öÃû×ÖÆ¥ÅäÎÊÌ⣬º¯ÊýÉùÃ÷Ç°¼ÓÉÏextern \ºó£¬Ôò±àÒëÆ÷¾Í»á°´ÕÕCÓïÑԵķ½Ê½½«¸Ãº¯Êý±àÒëΪ_foo£¬ÕâÑùCÓïÑÔÖоͿÉÒÔµ÷ÓÃC++µÄº¯ÊýÁË¡£

ÊÔÌâ5£º±àдһ¸öº¯Êý£¬×÷ÓÃÊÇ°ÑÒ»¸öchar×é³ÉµÄ×Ö·û´®Ñ­»·ÓÒÒÆn¸ö¡£±ÈÈçÔ­À´ÊÇ¡°abcdefghi¡±Èç¹ûn=2£¬ÒÆλºóÓ¦¸ÃÊÇ¡°hiabcdefgh¡±

º¯ÊýÍ·ÊÇÕâÑùµÄ£º

//pStrÊÇÖ¸ÏòÒÔ'\\0'½áβµÄ×Ö·û´®µÄÖ¸Õë //stepsÊÇÒªÇóÒƶ¯µÄn

void LoopMove ( char * pStr, int steps ) {

//ÇëÌî³ä... }

½â´ð£º

ÕýÈ·½â´ð1£º

void LoopMove ( char *pStr, int steps ) {

int n = strlen( pStr ) - steps; char tmp[MAX_LEN]; strcpy ( tmp, pStr + n ); strcpy ( tmp + steps, pStr); *( tmp + strlen ( pStr ) ) = '\\0'; strcpy( pStr, tmp ); }

ÕýÈ·½â´ð2£º