problem with new and pointers
-
hello I want to create a 2D 170*140 arrey with new . I wrote :
float **safhe;
*safhe=new float[170];
for(int i=0;i<170;i++)
safhe[i]=new float[140];but I dont know what is wrong thank you
V_shr wrote:
*safhe=new float[170];
You're dereferencing a pointer that you haven't assigned anything to yet. This is never what you want.
float **safhe;
safhe=new float[170];
for(int i=0;i<170;i++)
safhe[i]=new float[140];See the difference? Both
*safhe
andsafhe[i]
dereference a pointer, withsafhe[i]
effectively addingi * sizeof(float)
to the address before dereferencing.---- Scripts i’ve known... CPhog 1.0.0.0 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums
-
hello I want to create a 2D 170*140 arrey with new . I wrote :
float **safhe;
*safhe=new float[170];
for(int i=0;i<170;i++)
safhe[i]=new float[140];but I dont know what is wrong thank you
Here is an easy way to 'new' a 2D array of ints.
TCHAR szBuff[32]; const size_t Size = 10; int (*TwoDimensional)[Size][Size] = new int[Size][Size][Size]; for(int i =0; i < Size;i++) { for (int y = 0; y < Size;y++) { *TwoDimensional[i][y] = y; } } for(int i =0; i < Size;i++) { for (int y = 0; y < Size;y++) { _stprintf(szBuff, _T("row %d col %d"), i, *TwoDimensional[i][y]); MessageBox(szBuff, _T("TwoDimension"), MB_OK); } } delete[] TwoDimensional;
-
V_shr wrote:
*safhe=new float[170];
You're dereferencing a pointer that you haven't assigned anything to yet. This is never what you want.
float **safhe;
safhe=new float[170];
for(int i=0;i<170;i++)
safhe[i]=new float[140];See the difference? Both
*safhe
andsafhe[i]
dereference a pointer, withsafhe[i]
effectively addingi * sizeof(float)
to the address before dereferencing.---- Scripts i’ve known... CPhog 1.0.0.0 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums
-
hello thank you for your help BUT when I change that to "safhe=new float[170]" this error apears : cannot convert 'float *' to 'float * *' ???
Sorry, missed that bit. You don't want to allocate an array of 170
float
s, you want170 float*
. So, do something like:safhe = new float*[170];
---- Scripts i’ve known... CPhog 1.0.0.0 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums
-
Sorry, missed that bit. You don't want to allocate an array of 170
float
s, you want170 float*
. So, do something like:safhe = new float*[170];
---- Scripts i’ve known... CPhog 1.0.0.0 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums
-
Here is an easy way to 'new' a 2D array of ints.
TCHAR szBuff[32]; const size_t Size = 10; int (*TwoDimensional)[Size][Size] = new int[Size][Size][Size]; for(int i =0; i < Size;i++) { for (int y = 0; y < Size;y++) { *TwoDimensional[i][y] = y; } } for(int i =0; i < Size;i++) { for (int y = 0; y < Size;y++) { _stprintf(szBuff, _T("row %d col %d"), i, *TwoDimensional[i][y]); MessageBox(szBuff, _T("TwoDimension"), MB_OK); } } delete[] TwoDimensional;
int (*TwoDimensional)[Size][Size] = new int[Size][Size][Size];
Is it 2D or 3D??:confused: Knock out 't' from can't, You can if you think you can :cool: