C++ 讓函數(shù)返回?cái)?shù)組的方法
這個(gè)問題屬于非常初級(jí)的問題,但是對(duì)于初學(xué)不知道的人可能會(huì)比較頭疼。C++ 中函數(shù)是不能直接返回一個(gè)數(shù)組的,但是數(shù)組其實(shí)就是指針,所以可以讓函數(shù)返回指針來實(shí)現(xiàn)。比如一個(gè)矩陣相乘的函數(shù),很容易地我們就寫成:
#include <iostream>
using namespace std;
float* MultMatrix(float A[4], float B[4])
{
float M[4];
M[0] = A[0]*B[0] + A[1]*B[2];
M[1] = A[0]*B[1] + A[1]*B[3];
M[2] = A[2]*B[0] + A[3]*B[2];
M[3] = A[2]*B[1] + A[3]*B[3];
return M;
}
int main()
{
float A[4] = { 1.75, 0.66, 0, 1.75 };
float B[4] = {1, 1, 0, 0};
float *M = MultMatrix(A, B);
cout << M[0] << " " << M[1] << endl;
cout << M[2] << " " << M[3] << endl;
return 0;
}
但是運(yùn)行后發(fā)現(xiàn)結(jié)果是:
1.75 1.75
6.51468e-039 3.76489e-039
本不是想要的結(jié)果。于是我們?cè)诤瘮?shù)中也加上顯示代碼,看看是不是計(jì)算的問題,得到結(jié)果:
1.75 1.75
0 0
1.75 1.75
1.96875 1.75
發(fā)現(xiàn)計(jì)算的結(jié)果是正確的,但返回后就變了,而且跟上次的結(jié)果不一樣。這是為什么呢?
因?yàn)樵诤瘮?shù)中定義的數(shù)組M在函數(shù)執(zhí)行完后已經(jīng)被系統(tǒng)釋放掉了,所以在調(diào)用函數(shù)中得到的結(jié)果當(dāng)然不是計(jì)算后的結(jié)果。有一個(gè)解決辦法就是動(dòng)態(tài)分配內(nèi)存,在函數(shù)中 new 一個(gè)數(shù)組,這樣就不會(huì)被釋放掉了。
于是就應(yīng)該將:
float M[4];
改為:
float *M = new float[4];
修改運(yùn)行后得到結(jié)果:
1.75 1.75
0 0
1.75 1.75
0 0
正確。但是我們這樣并沒有將自己申請(qǐng)的空間釋放掉,如果我們?cè)诤瘮?shù)內(nèi)釋放的話結(jié)果就會(huì)跟開始時(shí)的一樣了。
看看我們的調(diào)用代碼:
float *M = MultMatrix(A, B);
這樣其實(shí)是將M指針指向了函數(shù)中M數(shù)組的首地址,我們可以將M指針釋放,效果和釋放申請(qǐng)的M數(shù)組是一樣的,因?yàn)樗鼈冎赶虻氖峭黄瑑?nèi)存空間。于是代碼就修改為:
#include <iostream>
using namespace std;
float* MultMatrix(float A[4], float B[4])
{
float *M = new float[4];
M[0] = A[0]*B[0] + A[1]*B[2];
M[1] = A[0]*B[1] + A[1]*B[3];
M[2] = A[2]*B[0] + A[3]*B[2];
M[3] = A[2]*B[1] + A[3]*B[3];
cout << M[0] << " " << M[1] << endl;
cout << M[2] << " " << M[3] << endl;
return M;
}
int main()
{
float A[4] = { 1.75, 0.66, 0, 1.75 };
float B[4] = {1, 1, 0, 0};
float *M = MultMatrix(A, B);
cout << M[0] << " " << M[1] << endl;
cout << M[2] << " " << M[3] << endl;
delete[] M;
return 0;
}
運(yùn)行結(jié)果:
1.75 1.75
0 0
1.75 1.75
0 0
沒有問題,new的空間也delete掉了。
鑒于下面大牛們的建議,我將程序修改如下,大家看可否:
#include <iostream>
using namespace std;
void MultMatrix(float M[4], float A[4], float B[4])
{
M[0] = A[0]*B[0] + A[1]*B[2];
M[1] = A[0]*B[1] + A[1]*B[3];
M[2] = A[2]*B[0] + A[3]*B[2];
M[3] = A[2]*B[1] + A[3]*B[3];
cout << M[0] << " " << M[1] << endl;
cout << M[2] << " " << M[3] << endl;
}
int main()
{
float A[4] = { 1.75, 0.66, 0, 1.75 };
float B[4] = {1, 1, 0, 0};
float *M = new float[4];
MultMatrix(M, A, B);
cout << M[0] << " " << M[1] << endl;
cout << M[2] << " " << M[3] << endl;
delete[] M;
return 0;
}
點(diǎn)評(píng)內(nèi)容:
首先,數(shù)組的 delete 是 delete[]。
其次,C++ 里面手動(dòng)內(nèi)存分配的一個(gè)重要原則是誰分配誰釋放。
所以,不應(yīng)該在MultMatrix里new數(shù)組,而應(yīng)該在外面new好了之后傳進(jìn)去修改。
要想返回一個(gè)數(shù)組,使用智能指針之類的東西才是正途。
以上就是C++ 讓函數(shù)返回?cái)?shù)組的方法的詳細(xì)內(nèi)容,更多關(guān)于C++ 讓函數(shù)返回?cái)?shù)組的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Qt實(shí)現(xiàn)SqlRelationalTable關(guān)聯(lián)表組件
在Qt中我們可以通過拖拽的方式將不同組件放到指定的位置,實(shí)現(xiàn)圖形化開發(fā)極大的方便了開發(fā)效率,本章將重點(diǎn)介紹SqlRelationalTable關(guān)聯(lián)表組件的常用方法及靈活運(yùn)用,感興趣的可以了解一下2023-12-12
C++ vector模擬實(shí)現(xiàn)的代碼詳解
vector是表示可變大小數(shù)組的序列容器,就像數(shù)組一樣,vector也采用的連續(xù)存儲(chǔ)空間來存儲(chǔ)元素,本質(zhì)講,vector使用動(dòng)態(tài)分配數(shù)組來存儲(chǔ)它的元素,本文將給大家詳細(xì)介紹一下C++ vector模擬實(shí)現(xiàn),需要的朋友可以參考下2023-07-07
c++ vector對(duì)象相關(guān)總結(jié)
這篇文章主要介紹了c++ vector對(duì)象的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c++,感興趣的朋友可以了解下2021-02-02

