我學習C++,我繼「思考在C++」的書,我做的練習,我不明白爲什麼在這個練習中:在C++中鍛鍊思維
的定義數組
int
。以該陣列的起始地址並使用static_cast
將其轉換爲void*
。編寫一個將void*
,一個數字(表示多個字節)和一個值(表示每個字節應該設置的值)作爲參數的函數。該函數應該將指定範圍內的每個字節設置爲指定的值。嘗試使用int
陣列上的功能。
此解決方案有效
#include<iostream>
using namespace std;
#define PR(EX) cout << #EX << ": " << EX << endl;
void f1(void* v, size_t num_bytes, int val){
unsigned char* p_addr = reinterpret_cast<unsigned char*>(v);
unsigned char byte = static_cast<unsigned char>(val);
for(size_t i = 0; i < num_bytes; i++){
*p_addr = byte++;
p_addr++;
}
PR(num_bytes);
}
int main(){
int a[5] = { 0, 0, 0, 0, 0 };
int value = 67;
void* vp = static_cast<void*>(&a);
f1(vp, sizeof(a), value);
unsigned char* byte = reinterpret_cast<unsigned char*>(vp);
for(int i = 0; i < sizeof(a)/sizeof(a[0]); i++){
cout << "a[" << i << "] = ";
for(int j = 0; j < sizeof(int); j++){
cout << reinterpret_cast<int*>(*byte) << " ";
byte++;
}
cout << endl;
}
return 0;
}
,而這其中並不
#include<iostream>
using namespace std;
#define PR(EX) cout << #EX << ": " << EX << endl;
void f1(void* v, size_t num_bytes, int val){
unsigned char* p_addr = reinterpret_cast<unsigned char*>(v);
unsigned char byte = static_cast<unsigned char>(val);
for(size_t i = 0; i < num_bytes; i++){
*p_addr = byte++;
p_addr++;
}
PR(num_bytes);
}
int main(){
int a[5] = { 0, 0, 0, 0, 0 };
int value = 67;
void* vp = static_cast<void*>(&a);
unsigned char* byte = reinterpret_cast<unsigned char*>(vp);
for(size_t i = 0; i < (sizeof(a)/sizeof(a[0])); i++){
cout << "a[" << i << "] = ";
for(size_t j = 0; j < sizeof(int); j++){
cout << *byte;
byte++;
}
cout << endl;
}
PR(*byte);
f1(vp, sizeof(a), value);
for(int i = 0; i < sizeof(a)/sizeof(a[0]); i++){
cout << "a[" << i << "] = ";
for(int j = 0; j < sizeof(int); j++){
cout << reinterpret_cast<int*>(*byte) << " ";
byte++;
}
cout << endl;
}
return 0;
}
的差別是在我定義指針byte
,在「工作」的版本,它的功能是前在之前的「不工作」版本中調用。這兩個版本的cout
是
a[0] =
a[1] =
a[2] =
a[3] =
a[4] =
*byte:
num_bytes: 20
a[0] = 0 0 0 0
a[1] = 0x3b 0x8 0x40 0
a[2] = 0 0 0 0
a[3] = 0x4 0 0 0
a[4] = 0 0 0 0
和
num_bytes: 20
a[0] = 0x43 0x44 0x45 0x46
a[1] = 0x47 0x48 0x49 0x4a
a[2] = 0x4b 0x4c 0x4d 0x4e
a[3] = 0x4f 0x50 0x51 0x52
a[4] = 0x53 0x54 0x55 0x56
在第二個我東西,字節中以正確的方式改變
,而在第一個,在我看來那東西是怎麼回事錯誤。顯然,問題出在我定義指針的地方,因爲我是初學者,自己學習,所以我希望理解我在想什麼錯。
好吧,我明白我的錯誤感謝您的建議,我已經修改了代碼,現在它打印我所期待的
#include<iostream>
using namespace std;
#define PR(EX) cout << #EX << ": " << EX << endl;
void f1(void* v, size_t num_bytes, int val){
unsigned char* p_addr = reinterpret_cast<unsigned char*>(v);
unsigned char byte = static_cast<unsigned char>(val);
for(size_t i = 0; i < num_bytes; i++){
*p_addr = byte++;
p_addr++;
}
PR(num_bytes);
}
int main(){
int a[5] = { 0, 0, 0, 0, 0 };
int value = 67;
void* vp = static_cast<void*>(a);
unsigned char* byte = reinterpret_cast<unsigned char*>(vp);
for(size_t i = 0; i < (sizeof(a)/sizeof(a[0])); i++){
cout << "a[" << i << "] = ";
for(size_t j = 0; j < sizeof(int); j++){
cout << reinterpret_cast<int*>(*byte);
byte++;
}
cout << endl;
}
byte = reinterpret_cast<unsigned char*>(vp);
f1(vp, sizeof(a), value);
for(int i = 0; i < sizeof(a)/sizeof(a[0]); i++){
cout << "a[" << i << "] = ";
for(int j = 0; j < sizeof(int); j++){
cout << *byte << " ";
byte++;
}
cout << endl;
}
return 0;
}
a[0] = 0000
a[1] = 0000
a[2] = 0000
a[3] = 0000
a[4] = 0000
num_bytes: 20
a[0] = C D E F
a[1] = G H I J
a[2] = K L M N
a[3] = O P Q R
a[4] = S T U V
指出差異。另外,「不工作」是什麼意思?怎麼了? –
你真的期望我們將這兩個程序逐行比較嗎?而且,是的,當你說「它不行」時它意味着什麼? – sbi
沒有一個程序按照規範工作:你在循環中每次迭代增加'byte',而規範說明每個字節應該被設置爲相同的值。 –