dz̸CÄÚ´æ·ÖÅä2
ÄÚ´æ·ÖÅ䷽ʽÓÐÈýÖÖ£º
£¨1£© ´Ó¾²Ì¬´æ´¢ÇøÓò·ÖÅä¡£ÄÚ´æÔÚ³ÌÐò±àÒëµÄʱºò¾ÍÒѾ·ÖÅäºÃ£¬Õâ¿éÄÚ´æÔÚ³ÌÐòµÄ
Õû¸öÔËÐÐÆڼ䶼´æÔÚ¡£ÀýÈçÈ«¾Ö±äÁ¿£¬static ±äÁ¿¡£
£¨2£© ÔÚÕ»ÉÏ´´½¨¡£ÔÚÖ´Ðк¯Êýʱ£¬º¯ÊýÄÚ¾Ö²¿±äÁ¿µÄ´æ´¢µ¥Ôª¶¼¿ÉÒÔÔÚÕ»ÉÏ´´½¨£¬º¯
ÊýÖ´ÐнáÊøʱÕâЩ´æ´¢µ¥Ôª×Ô¶¯±»ÊÍ·Å¡£Õ»ÄÚ´æ·ÖÅäÔËËãÄÚÖÃÓÚ´¦ÀíÆ÷µÄÖ¸Á
ÖУ¬Ð§Âʺܸߣ¬µ«ÊÇ·ÖÅäµÄÄÚ´æÈÝÁ¿ÓÐÏÞ¡£
£¨3£© ´Ó¶ÑÉÏ·ÖÅ䣬Òà³Æ¶¯Ì¬ÄÚ´æ·ÖÅä¡£³ÌÐòÔÚÔËÐеÄʱºòÓÃmalloc »ònew ÉêÇëÈÎÒâ¶à
ÉÙµÄÄڴ棬³ÌÐòÔ±×Ô¼º¸ºÔðÔÚºÎʱÓÃfree »òdelete ÊÍ·ÅÄÚ´æ¡£¶¯Ì¬ÄÚ´æµÄÉú´æÆÚ
ÓÉÎÒÃǾö¶¨£¬Ê¹Ó÷dz£Áé»î£¬µ«ÎÊÌâÒ²×î¶à¡£
void GetMemory(char *p)
{
p = (char *)malloc(100);
}
void Test(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str£¬ "hello world");
printf(str);
}
ÇëÎÊÔËÐÐTest º¯Êý»áÓÐʲôÑùµÄ½á¹û£¿
´ð£º³ÌÐò±ÀÀ£¡£
ÒòΪGetMemory ²¢²»ÄÜ´«µÝ¶¯Ì¬Äڴ棬
Test º¯ÊýÖÐµÄ str Ò»Ö±¶¼ÊÇ NULL¡£
strcpy(str£¬ "hello world");½«Ê¹³ÌÐò±À
À£¡£
char *GetMemory(void)
{
char p[] = "hello world";
return p;
}
void Test(void)
{
char *str = NULL;
str = GetMemory();
printf(str);
}
ÇëÎÊÔËÐÐTest º¯Êý»áÓÐʲôÑùµÄ½á¹û£¿
´ð£º¿ÉÄÜÊÇÂÒÂë¡£
ÒòΪGetMemory ·µ»ØµÄÊÇÖ¸Ïò“Õ»ÄÚ´æ”
µÄÖ¸Õ룬¸ÃÖ¸ÕëµÄµØÖ·²»ÊÇ NULL£¬µ«ÆäÔ
ÏÖµÄÄÚÈÝÒѾ±»Çå³ý£¬ÐÂÄÚÈݲ»¿ÉÖª¡£
void GetMemory2(char **p£¬ int num)
{
*p = (char *)malloc(num);
}
void Test(void)
{
char *str = NULL;
GetMemory(&str£¬ 100);
strcpy(str£¬ "hello");
printf(str);
}
ÇëÎÊÔËÐÐTest º¯Êý»áÓÐʲôÑùµÄ½á¹û£¿
´ð£º
£¨1£©Äܹ»Êä³öhello
£¨2£©ÄÚ´æй©
void Test(void)
{
char *str = (char *) malloc(100);
strcpy(str£¬ “hello”);
free(str);
if(str £¡= NULL)
{
strcpy(str£¬ “world”);
printf(str);
}
}
ÇëÎÊÔËÐÐTest º¯Êý»áÓÐʲôÑùµÄ½á¹û£¿
´ð£º´Û¸Ä¶¯Ì¬ÄÚ´æÇøµÄÄÚÈÝ£¬ºó¹ûÄÑÒÔÔ¤
ÁÏ£¬·Ç³£Î£ÏÕ¡£
ÒòΪfree(str);Ö®ºó£¬str ³ÉΪҰָÕ룬
if(str £¡= NULL)Óï¾ä²»Æð×÷Óá£
******************
Ïà¹ØÎĵµ£º
Delphi Óë C/C++ Êý¾ÝÀàÐͶÔÕÕ±í
DelphiÊý¾ÝÀàÐÍC/C++
ShorInt
8λÓзûºÅÕûÊý
char
Byte
8λÎÞ·ûºÅÕûÊý
BYTE,unsigned short
SmallInt
16λÓзûºÅÕûÊý
short
Word
16λÎÞ·ûºÅÕûÊý
unsigned short
Integer,LongInt
32λÓзûºÅÕûÊý
int,long
Cardinal,LongWord/DWORD
32λÎÞ·ûºÅÕûÊý
unsigned long
Int6 ......
C±ê×¼ÖеÄI/O¿âÊÇÒ»¸ö±È½ÏÅÓ´óµÄ¿â£¬ÊµÏÖÒ²±È½Ï¸´ÔÓ¡£ÏÔÈ»I/O¿âµÄʵÏÖÊÇÒÀÀµÓÚ²Ù×÷ϵͳµÄ£¬²»Í¬µÄϵͳÉÏI/O¿âµÄʵÏÖ»úÀíÊDz»Ò»ÑùµÄ¡£glibcÖУ¬I/O¿âµÄºËÐÄʵÏÖÔÚlibioĿ¼Ï¡£ÓÐ4¸öÍ·Îļþlibio.h, iolibio.h, libioP.h, strfile.h, stdio.h£¬ÆäÖÐstdio.hÊǵ¼³öµÄ±ê×¼CÍ·Îļþ£¬ÆäÓ඼ÊÇÄÚ²¿ÊµÏÖ¡£ÕâÀï²¢² ......
9)LinuxÏÂC¿ª·¢¹¤¾ß½éÉÜ
LinuxµÄ·¢ÐаæÖаüº¬Á˺ܶàÈí¼þ¿ª·¢¹¤¾ß. ËüÃÇÖеĺܶàÊÇÓÃÓÚ C ºÍ C++Ó¦ÓóÌÐò¿ª·¢
µÄ. ±¾ÎĽéÉÜÁËÔÚ Linux ÏÂÄÜÓÃÓÚ C Ó¦ÓóÌÐò¿ª·¢ºÍµ÷ÊԵŤ¾ß. ±¾ÎĵÄÖ÷Ö¼ÊǽéÉÜÈç
ºÎÔÚ Linux ÏÂʹÓà C ±àÒëÆ÷ºÍÆäËû C ±à³Ì¹¤¾ß, ¶ø·Ç C ÓïÑÔ±à³ÌµÄ½Ì³Ì.
GNU C ±àÒëÆ÷
GNU C ±àÒëÆ÷(GCC)ÊÇÒ»¸öÈ«¹¦Ä ......
ÅÅÐòËã·¨ÊÇÒ»ÖÖ»ù±¾²¢ÇÒ³£ÓõÄËã·¨¡£ÓÉÓÚʵ¼Ê¹¤×÷Öд¦ÀíµÄÊýÁ¿¾Þ´ó£¬ËùÒÔÅÅÐòËã·¨¶ÔËã·¨±¾ÉíµÄËÙ¶ÈÒªÇóºÜ¸ß¡£
¶øÒ»°ãÎÒÃÇËùνµÄËã·¨µÄÐÔÄÜÖ÷ÒªÊÇÖ¸Ëã·¨µÄ¸´ÔӶȣ¬Ò»°ãÓÃO·½·¨À´±íʾ¡£ÔÚºóÃæÎÒ½«¸ø³öÏêϸµÄ˵Ã÷¡£
¶ÔÓÚÅÅÐòµÄËã·¨ÎÒÏëÏÈ×öÒ»µã¼òµ¥µÄ½éÉÜ£¬Ò²ÊǸøÕâƪÎÄÕÂÀíÒ»¸öÌá¸Ù ......
LinuxÏ»ñµÃϵͳʱ¼äµÄCÓïÑÔµÄʵÏÖ·½·¨
#include<time.h> //CÓïÑÔµÄÍ·Îļþ
#include<stdio.h> //C﵀I/O
void main()
{
time_t now; //ʵÀý»¯time_t½á¹¹
struct tm *timenow; //ʵÀý»¯tm½á¹¹Ö¸Õë
time(&now);
//timeº¯Êý¶ÁÈ¡ÏÖÔÚµ ......