一、C++中的動態內存分配
1. C++中通過new關鍵字進行動態內存申請,C++中的動態內存申請是基於類型進行的;
2. delete關鍵字用於內存釋放;
#include <stdio.h>
int main()
{
int* p = new int;
*p = 5;
*p = *p + 10;
printf("p = %p\n", p);
printf("*p = %d\n", *p);
delete p;
p = new int[10];
for(int i=0; i<10; i++)
{
p[i] = i + 1;
printf("p[%d] = %d\n", i, p[i]);
}
delete[] p;
printf("Press any key to continue...");
getchar();
return 0;
}
運行結果如下所示:
new關鍵字與malloc函數的區別
1. new關鍵字是C++的一部分,malloc是由C庫提供的函數;
2. new以具體類型爲單位進行內存分配,malloc只能以字節爲單位進行內存分配;
3. new在申請單個類型變量時可進行初始化,malloc不具備內存初始化的特性;
new關鍵字的初始化demo如下:
#include <stdio.h>
int main()
{
int* pi = new int(1);
float* pf = new float(2.0f);
char* pc = new char('c');
printf("*pi = %d\n", *pi);
printf("*pf = %f\n", *pf);
printf("*pc = %c\n", *pc);
delete pi;
delete pf;
delete pc;
printf("Press any key to continue...");
getchar();
return 0;
}
運行結果如下所示:
二、C++中的命名空間
在C語言中只有一個全局作用域,C語言中所有的全局標識符共享同一個作用域,標識符之間可能發生衝突。C++中提出了命名空間的概念,命名空間將全局作用域分成不同的部分,不同命名空間中的標識符可以同名而不會發生衝突,命名空間可以相互嵌套,全局作用域也叫默認命名空間。
1. C++命名空間的定義
namespace name { /* … */ }
#include <stdio.h>
namespace First
{
int i = 0;
}
namespace Second
{
int i = 1;
namespace Internal
{
struct P
{
int x;
int y;
};
}
}
int main()
{
printf("Press any key to continue...");
getchar();
return 0;
}
運行結果如下:
通過運行結果可知,不同命名空間中的標識符可以同名而不會發生衝突。
2. C++命名空間的使用
1. 使用整個命名空間:using namespace name;
2. 使用命名空間中的變量:using name::variable;
3. 使用默認命名空間中的變量:::variable
注:默認情況下可以直接使用默認命名空間中的所有標識符。
#include <stdio.h>
namespace First
{
int i = 0;
}
namespace Second
{
int i = 1;
namespace Internal
{
struct P
{
int x;
int y;
};
}
}
int main()
{
using namespace First;
using Second::Internal::P;
printf("i = %d\n", i);
printf("i = %d\n", Second::i);
P p = {2, 3};
printf("p.x = %d\n", p.x);
printf("p.y = %d\n", p.y);
printf("Press any key to continue...");
getchar();
return 0;
}
運行結果如下:
三、強制類型轉換
C方式的強制類型轉換:( Type )( Expression ) or Type( Expression )
#include <stdio.h>
typedef void(PF)(int);
struct Point
{
int x;
int y;
};
int main()
{
int v = 0x12345;
PF* pf = (PF*)v;
char c = char(v);
pf(v);
Point* p = (Point*)v;
printf("p->x = %d\n", p->x);
printf("p->y = %d\n", p->y);
printf("Press any key to continue...");
getchar();
return 0;
}
C方式強制類型轉換存在的問題
過於粗暴:任意類型之間都可以進行轉換,編譯器很難判斷其正確性;
難於定位:在源碼中無法快速定位所有使用強制類型轉換的語句;
C++將強制類型轉換:xxx_cast< Type >( Expression )
static_cast強制類型轉換
1. 用於基本類型間的轉換,但不能用於基本類型指針間的轉換;
2. 用於有繼承關係類對象之間的轉換和類指針之間的轉換;
注:static_cast是編譯期進行轉換的,無法在運行時檢測類型,所以類類型之間的轉換可能存在風險。
#include <stdio.h>
int main()
{
int i = 0x12345;
char c = 'c';
int* pi = &i;
char* pc = &c;
c = static_cast<char>(i);
//pc = static_cast<char*>(pi);
printf("Press any key to continue...");
getchar();
return 0;
}
運行結果如下:
const_cast強制類型轉換
1. 用於去除變量的const屬性;
#include <stdio.h>
int main()
{
const int& j = 1;
int& k = const_cast<int&>(j);
const int x = 2;
int& y = const_cast<int&>(x);
k = 5;
printf("k = %d\n", k);
printf("j = %d\n", j);
y = 8;
printf("x = %d\n", x);
printf("y = %d\n", y);
printf("&x = %p\n", &x);
printf("&y = %p\n", &y);
printf("Press any key to continue...");
getchar();
return 0;
}
運行結果如下:
reinterpret_cast強制類型轉換
1. 用於指針類型間的強制轉換
2. 用於整數和指針類型間的強制轉換
#include <stdio.h>
int main()
{
int i = 0;
char c = 'c';
int* pi = &i;
char* pc = &c;
pc = reinterpret_cast<char*>(pi);
pi = reinterpret_cast<int*>(pc);
//c = reinterpret_cast<char>(i); // Oops!
printf("Press any key to continue...");
getchar();
return 0;
}
運行結果如下:
注:reinterpret_cast直接從二進制位進行復制,是一種極其不安全的轉換。
dynamic_cast強制類型轉換
1. 主要用於類層次間的轉換,還可以用於類之間的交叉轉換;
2. dynamic_cast具有類型檢查的功能,比static_cast更安全;