我從一個整數數組test[]
開始,它表示一組圖像。 test[]
的前兩個元素是第一個圖像的高度和寬度。以下四個(height*width)
元素是像素值。接下來的六個代表下一個圖像等等。即使指針看起來有效,爲什麼我會得到EXC_BAD_ACCESS?
我的目標是通過圖像對象訪問圖像,而不會複製test[]
陣列中的任何內容。我試圖通過ImageSet::import
來做到這一點。
int main(){
int test[] = {2,2,1,2,3,4,
2,2,4,5,6,7,
2,2,9,8,0,9};
ImageSet set = ImageSet();
set.import(test); //ImageSet::import
return 0;
}
ImageSet::import
使用遊標變量存儲下一個圖像的起始索引。它調用Image::import
將該數據讀入圖像對象。該特定圖像的數據應該從地址&data[cursor]
開始。
void ImageSet::import(int data[]){
int cursor = 0;
for(int i = 0; i < NUM_SRC_IMAGES; i++){
int height = data[cursor];
int width = data[cursor+1];
source_[i].import(&data[cursor]); //this is Image::import
cursor += height * width + 2;
}
}
在image::import
,我試圖通過閱讀data[0]
得到一個壞的訪問。根據調試器,data[]
指向2的正確值,但當我試圖通過data[0]
獲取該值時,仍會拋出EXC_BAD_ACCESS
。
void Image::import(int data[]){
height_ = data[0]; //the problem occurs right here
width_ = data[1];
matrix_ = &data[2];
}
我非常感謝一個解釋我做錯了什麼。我也同樣感謝如何在沒有指針算術的情況下實現我的目標的建議。
謝謝!
你試過把它作爲'source_ [i] .import(data + cursor)'傳遞嗎? – manman
請填寫完整的程序。 http://sscce.org –
在for(int i = 0; i