printf都做了什么??
為測試stack指針是否由系統管理,從函數中返回后是否繼續可用,寫了一些代碼:
// TestPointer.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
typedef struct Person

{
int iAge;
int iWeight;
}Person;
//Printf都做了什么?
//感覺調用printf時系統對stack進行了清理
char * GetString(void);
Person * GetPerson();
int main(int argc, char* argv[])

{
printf("Hello World!\n");
char * pStr = GetString();
//感覺調用printf時系統對stack進行了清理
printf("%s", pStr); //將這一句去掉后運行試試?
Person * m_pPersion = GetPerson();
printf("doooooo\n"); //將這一句去掉運行試試?
printf("Age = %d, Weight = %d\n", m_pPersion->iAge, m_pPersion->iWeight);
return 0;
}
char * GetString(void)

{
//簡單的可以理解為:
//heap:是由malloc之類函數分配的空間所在地。地址是由低向高增長的。
//stack:是自動分配變量,以及函數調用的時候所使用的一些空間。地址是由高向低減少的。
//棧(stack)內存的情況
char szMessage[100];
strcpy(szMessage, "this is just a test!\n");
printf("%s", szMessage);
return szMessage;
//堆(heap)內存的情況
/**//*char * pRet = (char *)malloc( 100 * sizeof(char));
strcpy(pRet, "This is just a test!\n");
return pRet;*/
}
Person * GetPerson()

{
//stack
Person m_Person;
m_Person.iAge = 24;
m_Person.iWeight = 55;
return &m_Person;
//更換成heap形式的又是怎樣?
}
上述程序運行環境為:WindowsXP sp2 + Visual C++ Enterprise Edition 6.0 + Vs6Sp6

