/*
* File: main6.cpp
* Author: Vicky
*
* Created on 2011年11月28日, 上午9:20
*/
#include <iostream>
struct Player {
char name[2];
short age;
struct Army {
int count;
} armys[20];
};
/*
*
*/
int main(void) {
std::cout << "sizeof(struct Player)\t" << sizeof(struct Player) << std::endl;
std::cout << "---------------------------" << std::endl;
char *name = "Vicky";
char *name2 = strdup(name);
std::cout << "name2 = " << name2 << std::endl;
free(name2); // 使用strdup可以free
name2 = 0;
// free(name); // ERROR
name = 0; // 首先,name指针并为分配空间,而是指向了堆中"Vicky","Vicky"由系统销毁
std::cout << "---------------------------" << std::endl;
char *name3;
name3 = (char*)malloc(21); // 为name3指针指向的地址分配了空间,需要通过该指针进行销毁
strcpy(name3,"Jack");
free(name3);
name3 = 0;
return 0;
}
sizeof(struct Player) 84
---------------------------
name2 = Vicky
---------------------------
运行成功(总计时间: 141毫秒)