C/C++ÓïÑÔÖÐÖ¸ÕëµÄÒâÒå
¿´µ½Õâ¸öÃæÊÔÌâ,¹À¼ÆºÜ¶à¸ÕѧCÖ¸ÕëµÄͬѧ¶¼»á¸ã´í.
void GetMemory(char *p)
{
// ´«µÝ½øÀ´µÄµÄֵʹµÃ*p==NULL
// ÕâÀïÐ޸ĵÄÊÇÕâ¸öº¯ÊýÄÚµÄÁÙʱָÕë±äÁ¿pµÄÖµ
p = (char *)malloc(100);
}
void Test(void)
{
char *str = NULL;
// ´«µÝ½øÈ¥µÄÊÇstrµÄÖµNULL
GetMemory(str);
// ÕâÀïstrÖ¸Õ벢δ¸³Öµ,ÈÔÈ»ÊÇNULL,ËùÒÔÕâÀ︳ֵ»á³ö´í
strcpy(str, "Hello World");
printf("%s\n", str);
free(str);
}
ÎÒÃÇÖªµÀ¼ÆËã»úÖ»ÈÏʶ0ºÍ1,ËùÓеÄÊý¾Ý,°üÀ¨Í¼Æ¬,ÎÄ×ÖµÈ×îÖÕ¶¼ÊÇת»»³É0ºÍ1À´±íʾ.
ΪÁË·½±ã˵Ã÷,ÎÒÃÇÓÃÊ®Áù¸ö¶þ½øÖÆÊý×é³ÉµÄÒ»¸öÊ®Áù½øÖÆÊýÀ´±íʾ.
ÔÚwin32ϵͳÖÐ,ÄÚ´æµØÖ·ÊÇ´Ó0µ½2^32bit(Ï൱ÓÚ0µ½4GB),win32ÖÐVC6.0ÖÐÖ¸Õë±¾ÖʾÍÊÇÒ»¸öÕ¼4¸ö×Ö½ÚµÄÎÞ·ûºÅÕûÐÎ(unsigned int),ÄÜÈ¡0µ½2^32ÖÐËùÓеÄÖµµÄÈÎÒ»¸öÖµ,¼´Ö¸ÕëÄÜÏò¼ÆËã»úÄÚ´æÖÐÈκÎÒ»¸öµØÖ·.
// pCharÖ¸ÏòµÄÒ»¸öcharÐͱäÁ¿(×¢Òâ×Ö·ûºÍ×Ö·û´®¸ÅÄîÇø·Ö¿ª)
char *pChar = NULL;
// pIntÖ¸ÏòµÄÊÇÒ»¸öintÐͱäÁ¿
int *pInt = NULL;
// pDoubleÖ¸ÏòµÄÊÇÒ»¸öpDoubleÐͱäÁ¿
double *pDouble = NULL;
struct node
{
int i, j, k;
};
// pNodeÖ¸ÏòµÄÊÇÒ»¸ö×Ô¶¨Òå½á¹¹Ìå±äÁ¿
struct node *pNode = NULL;
class TestClass
{
TestClass()
{
}
~TestClass()
{
}
};
// pClassÖ¸ÏòµÄÊÇÒ»¸ö×Ô¶¨ÒåÀà±äÁ¿
TestClass *pClass = NULL;
void Test(void)
{
printf("This is test function!\n");
}
typedef void (*pFunction)(void);
// pFunÖ¸ÏòµÄÊÇÒ»¸öTestÐͺ¯Êý
pFunction pFun = Test;
void Test1(void)
{
printf("sizeof(pChar)=%d\n", sizeof(pChar));
printf("sizeof(pInt)=%d\n", sizeof(pInt));
printf("sizeof(pDouble)=%d\n", sizeof(pDouble));
printf("sizeof(pNode)=%d\n", sizeof(pNode));
printf("sizeof(pClass)=%d\n", sizeof(pClass));
printf("sizeof(pFun)=%d\n", sizeof(pFun));
}
´òÓ¡µÄ½á¹ûÈçÏÂ:
sizeof(pChar)=4
sizeof(pInt)=4
sizeof(pDouble)=4
sizeof(pNode)=4
sizeof(pClass)=4
sizeof(pFun)=4
Press any key to continue
ÉÏÃ涨ÒåµÄÖ¸ÕëºÍÆäËüϵͳÄܶ¨ÒåµÄËùÓÐÖ¸ÕëÔÚwin32ϵͳÖÐÓÃsizeof²âÊÔ¶¼ÊÇ4.
Ö¸ÕëÓÐʲôÇø±ðÄØ?
±ÈÈçchar *pCharºÍint *pInt:
pCharÖ»ÊǸæËß³ÌÐòÎ
Ïà¹ØÎĵµ£º
ϵͳ»·¾³£ºWindows 7
Èí¼þ»·¾³£ºVisual C++ 2008 SP1 +SQL Server 2005
±¾´ÎÄ¿µÄ£º±àдһ¸öº½¿Õ¹ÜÀíϵͳ
ÕâÊÇÊý¾Ý¿â¿Î³ÌÉè¼ÆµÄ³É¹û£¬ËäÈ»³É¼¨²»¼Ñ£¬µ«ÊÇ×÷ΪÎÒÓÃVC++ ÒÔÀ´±àдµÄ×î´ó³ÌÐò»¹ÊÇ´«µ½ÍøÉÏ£¬ÒÔ¹©²Î¿¼¡£ÓÃVC++ ×öÊý¾Ý¿âÉè¼Æ²¢²»ÈÝÒ×£¬µ«Ò²²»ÊDz»¿ÉÄÜ¡£ÒÔÏÂÊÇÎҵijÌÐò½çÃ棬ºóÃæ ......
Ö¸ÕëÊÇ£ÃÓïÑÔÖй㷺ʹÓõÄÒ»ÖÖÊý¾ÝÀàÐÍ¡£ÔËÓÃÖ¸Õë±à³ÌÊÇ£ÃÓïÑÔ×îÖ÷ÒªµÄ·ç¸ñÖ®Ò»¡£ÀûÓÃÖ¸Õë±äÁ¿¿ÉÒÔ±íʾ¸÷ÖÖÊý¾Ý½á¹¹£»Äܷܺ½±ãµØʹÓÃÊý×éºÍ×Ö·û´®£»²¢ÄÜÏó»ã±àÓïÑÔÒ»Ñù´¦ÀíÄÚ´æµØÖ·£¬´Ó¶ø±à³ö¾«Á·¶ø¸ßЧµÄ³ÌÐò¡£Ö¸Õ뼫´óµØ·á¸»ÁË£ÃÓïÑԵŦÄÜ¡£Ñ§Ï°Ö¸ÕëÊÇѧϰ£ÃÓïÑÔÖÐ×îÖØÒªµÄÒ»»·£¬ÄÜ·ñÕýÈ·Àí½âºÍʹÓÃÖ¸ÕëÊÇÎÒÃÇÊÇ·ñÕÆÎÕ ......
Ç°Ãæ½éÉܵĸ÷ÖÖÔËË㶼ÊÇÒÔ×Ö½Ú×÷Ϊ×î»ù±¾Î»½øÐеġ£ µ«Ôںܶàϵͳ³ÌÐòÖг£ÒªÇóÔÚλ(bit)Ò»¼¶½øÐÐÔËËã»ò´¦Àí¡££ÃÓïÑÔÌṩÁËλÔËËãµÄ¹¦ÄÜ£¬ÕâʹµÃ£ÃÓïÑÔÒ²ÄÜÏñ»ã±àÓïÑÔÒ»ÑùÓÃÀ´±àдϵͳ³ÌÐò¡£
12.1 λÔËËã·û£ÃÓïÑÔÌṩÁËÁùÖÖλÔËËã·û£º
& °´ ......
Ò»¡¢´ÓCÓëC++±àÒëÁ¬½Ó·½Ê½ÉîÈëÀí½âextern "C"
<1>.CºÍC++¶Ôº¯ÊýµÄ´¦Àí·½Ê½ÊDz»Í¬µÄ.±»extern "C"ÐÞÊεıäÁ¿ºÍº¯ÊýÊÇ°´ÕÕCÓïÑÔ·½Ê½±àÒëºÍÁ¬½ÓµÄ.extern "C"ÓÃÓÚʵÏÖC++ÓëC¼°ÆäËüÓïÑԵĻìºÏ±à³Ì¡£
<2>.δ¼Óextern “C”ÉùÃ÷ʱµÄ±àÒ뷽ʽ
Ê×ÏÈ¿´¿´C++ÖжÔÀàËÆCµÄº¯ÊýÊÇÔõÑù±àÒëµÄ¡£
×÷ΪһÖÖÃæÏò ......
±¾³ÌÐò¿ÉÒÔ¶ÁÈ¡.wavÎļþ£¬È»ºó½øÐв¥·Å¡£
È·ÈÏ·½·¨£ºcat /etc/sndstat£¬Èç¹ûÏÔʾÎÞ´ËÉ豸£¬ÔòûÓа²×°Çý¶¯¡£
#include <unistd.h>#include <fcntl.h>#include <sys/types.h>#include <sys/ioctl.h>#include <stdlib.h>#include <stdio.h>#include&nbs ......