[問題] 有時候記憶體會找不到位置

看板C_and_CPP作者 (水表)時間8年前 (2016/07/01 15:27), 編輯推噓4(5116)
留言22則, 4人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Xcode 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) std 問題(Question): 我的問題在於,呼叫printmatrix函式時,有時候記憶體會無法讀到位置,有時候卻可以 找到並執行,但明明Input都是一模一樣的,只需要告訴我原因即可,謝謝 餵入的資料(Input): 一樣的矩陣跟資料輸入數次,卻是不同的結果 2 2 3 4 5 3 5 6 8 預期的正確結果(Expected Output): A = 2 3 4 5 B = 3 5 6 8 A+B = 5 8 10 13 A-B = -1 -2 -2 -3 A*B = 24 34 42 60 (這是有時候跑出來的執行結果,有時候卻跑不出來) 錯誤結果(Wrong Output): 有時候跑不出來,我完全沒頭緒@@ 程式碼(Code):(請善用置底文網頁, 記得排版) // // Matrix.h // L4 HW // // Created by 葉柏宏 on 2016/7/1. // Copyright 2016年 Dannel Apple. All rights reserved. // #ifndef Matrix_h #define Matrix_h template<class _Tx> void swap(_Tx &a, _Tx &b) { _Tx t = a; a = b; b = t; } class Matrix{ size_t n; double *mtxptr=new double;//a obj with algo idx public: void assignDimension(int dim){ this->n=dim; } void assignElements(){ for(size_t i=0;i<(n*n);i++) cin>>*(mtxptr+i); } void printMatrix(){ for(size_t i=0;i<n*n;i++){ if(i%n==0) cout<<"\n"; std::cout<<*(mtxptr+i)<<" "; } std::cout<<std::endl; } void assignMatrix(Matrix A){ this->n=A.n; for(size_t i=0;i<n*n;i++) *((this->mtxptr)+i)=*((A.mtxptr)+i); } void transposeMatrix(){ for(size_t i=0;i<n*n;i++)if((i%n)>(i/n)) swap(*((this->mtxptr)+i), *((this->mtxptr)+(i%n)*n+i/n));} void addMatrix(Matrix A,Matrix B){setzero(); for(size_t i=0;i<n*n;i++) *(mtxptr+i)=*(A.mtxptr+i)+*(B.mtxptr+i); } void subtractMatrix(Matrix A,Matrix B){setzero(); for(size_t i=0;i<n*n;i++) *(mtxptr+i)=*(A.mtxptr+i)-*(B.mtxptr+i); } void multiplyMatrix(Matrix A,Matrix B){setzero(); for(size_t i=0;i<n*n;i++){ for(size_t j=0;j<n;j++){ *((this->mtxptr)+i)+=*((A.mtxptr)+i/n*n+j)**((B.mtxptr)+n*j+i%n); } } } void setzero(){ for(size_t i=0;i<n*n;i++) *((this->mtxptr)+i)=0; } }; #endif /* Matrix_h */ 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.114.72.205 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1467358056.A.BDE.html

07/01 15:28, , 1F
我使用了一些矩陣的性質
07/01 15:28, 1F

07/01 15:54, , 2F
先把你的語法整理 連cin都沒有std::
07/01 15:54, 2F

07/01 15:55, , 3F
還有你的main.cpp也要放上來才知道是哪個函式的問題
07/01 15:55, 3F

07/01 15:55, , 4F
很想噓
07/01 15:55, 4F

07/01 15:59, , 5F
可能我C++沒學好 那個swap好像怪怪的
07/01 15:59, 5F

07/01 16:02, , 6F
程序導向和物件導向不要混在一起 不好閱讀
07/01 16:02, 6F

07/01 16:06, , 7F
不好意思剛剛才看懂原來是自己寫一個swap
07/01 16:06, 7F

07/01 16:08, , 8F
include了哪些東西也放上來一下 不是每個編譯器都預設
07/01 16:08, 8F

07/01 16:18, , 9F
你的mtxptr只給了1個 double 空間?
07/01 16:18, 9F

07/02 11:34, , 10F
謝謝各位的指教,如果使用pointer的話,會有什麼問題
07/02 11:34, 10F

07/02 13:13, , 11F
所以你堅持要放我Compiler不能過的程式碼?
07/02 13:13, 11F

07/03 00:24, , 12F
什麼意思是程序導向跟物件導向放一起 看不太懂
07/03 00:24, 12F

07/03 00:27, , 13F
你只new 一個double 嗎還是我漏掉了 陣列應該是new dou
07/03 00:27, 13F

07/03 00:27, , 14F
ble [x]吧
07/03 00:27, 14F

07/03 06:08, , 15F
1.assign很明顯是物件導向 卻沒有建構解構
07/03 06:08, 15F

07/03 06:09, , 16F
2.add sub這種明顯是程序導向 丟在class裡很怪
07/03 06:09, 16F

07/03 06:09, , 17F
要怎麼用? 大概就是A.sub(A,B) 這樣很怪
07/03 06:09, 17F

07/03 06:10, , 18F
3.很帥氣的說函式庫用std 一開場宣告Template
07/03 06:10, 18F

07/03 06:11, , 19F
我還以為要用樣板做matrix運算 結果是自定義swap
07/03 06:11, 19F

07/03 06:12, , 20F
這三點都沒關係 我是很度爛語法有錯 程式碼那麼長
07/03 06:12, 20F

07/03 06:13, , 21F
不使用網頁存就算 複製貼上到IDE就有紅點
07/03 06:13, 21F

07/03 06:13, , 22F
cpp也不貼上來 這種一丟上來開IDE debugger比較快
07/03 06:13, 22F
文章代碼(AID): #1NTXjelU (C_and_CPP)