|
Hi i know this is probably a stupid question but whats the proper syntax for for allocating memory for a 2d array?
i tried
int **array;
array = new int[2][2]
even tried the second line with *array
this wont even compile
so wahts the correct way to do this?
thanks
|
|
|
|
|
The only way I know to do it is to create an array of int* and then assign each element to point to an array.
e.g.
int ** array = new int*[2];
for (int i = 0; i < 2; i++)
{
array[i] = new int[2];
}
|
|
|
|
|
this is definitly the most silly way. for every new an entry in the heap will be created which will decrese the speed of your app dramatically if there are many fields for the array.
Don't try it, just do it!
|
|
|
|
|
Pft. Allocation of the array is not typically the source of speed overhead. Accessing array positions repeatedly is, and your way is no faster than mine. Mine is at least intuitive.
|
|
|
|
|
Why don't you use a vector instead? They are much safer and easier to handle...
A student knows little about a lot.
A professor knows a lot about little.
I know everything about nothing.
|
|
|
|
|
... and also one dimensional.
|
|
|
|
|
vector < vector < int > > Matrix;
and now you have a 2d array...
A student knows little about a lot.
A professor knows a lot about little.
I know everything about nothing.
|
|
|
|
|
I suppose that works, but ick. I guess if it does the job for you, though . . .
|
|
|
|
|
there is a much better and faster way!
int **array;
int i, j;
array = new int[4]; 2*2=4
for( i = 0; i < 2; i++ )
{
for( j = 0; j < 2; j++ )
{
array[i*2+j] = i;
}
}
this is the fastest way
Don't try it, just do it!
|
|
|
|
|
Alexander M. wrote:
this is the fastest way
Unless you have some metrics to support this, speed is probably going to be irrelevant.
Your approach has a major flaw; namely that it is not intuitive. Read my response here as to why array[i][j] is much preferred over array[i*2+j] .
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
Here's what I do :
<br />
<br />
void **Alloc2DArray( int xdim, int xitmsize, int ydim, int yitmsize )<br />
{<br />
int i;<br />
int x;<br />
void ** data;<br />
<br />
data = (void **)calloc( xdim, xitmsize );<br />
if( data == NULL )<br />
return NULL;<br />
for( x = 0; x < xdim; x++ )<br />
{<br />
data[x] = calloc( ydim, yitmsize );<br />
if( data[x] == NULL )<br />
{<br />
for( i = 0; i < x; i++ )
free( data[i] );<br />
free( data );<br />
return NULL;<br />
}<br />
}<br />
<br />
return data;<br />
}<br />
<br />
<br />
<br />
void **Free2DArray( void **array, int xdim )<br />
{<br />
int x;<br />
<br />
if( array == NULL )<br />
return NULL;
<br />
for( x = xdim - 1; x >= 0; x-- )<br />
{<br />
free( array[x] );<br />
array[x] = NULL;<br />
}<br />
free( array );<br />
return NULL;<br />
}<br />
<br />
<br />
<br />
float **AllocFloatMatrix( int xdim, int ydim )<br />
{<br />
float **data = (float **)Alloc2DArray( xdim, sizeof( float * ),<br />
ydim, sizeof( float ) );<br />
return data;<br />
}<br />
<br />
<br />
<br />
float **FreeFloatMatrix( float **array, int xdim )<br />
{<br />
Free2DArray( (void **)array, xdim );<br />
return NULL;<br />
}<br />
Note that this is for old, straight C-style allocation and deallocation. You can change things to use new/delete fairly easily. This could also be adapted to a template implementation if one were so inclined.
Using the "type **" mechanism for allocation allows one to write variable[y][x] to access data which I find that to be reasonably clean.
__________________________________________
a two cent stamp short of going postal.
|
|
|
|
|
im very new to c++ and i cant find anything on adding just the date to a statusbar i dont want a datetimepicker i want just the date like sun may 23 2004i have
CTime t1;
t1=CTime::GetCurrentTime();
m_bar.SetPaneText(1,t1.Format("%H:%M:%S"));
for the time can someone help?
|
|
|
|
|
i want just the date like sun may 23 2004
>
CTime Tm;
Tm = Tm.GetCurrentTime();
CString strDate = Tm.Format( "%A, %B %d, %Y" );
|
|
|
|
|
thanks *Dreamz
|
|
|
|
|
Try using
   t1.Format("%c")
for the date and time with local formatting or
   t1.Format("%a %b %d %Y)
for "Sun May 23 2004"
For other options see documentation for the strftime function
|
|
|
|
|
thanks Martin Turnidge
|
|
|
|
|
Hi,
I want to apply Bitmap::ConvertFormat () method for my project. But above method is not in the GDI+ version 1.0 .So I have to download GDI+ version 1.1. From where can I download it? ,I search it .But I didn't get suitable URL for that. please help
or Is there other way to apply the ConvertFormat() Method?
|
|
|
|
|
You need to install latest version of platform SDK.
I'll write a suicide note on a hundred dollar bill - Dire Straits
|
|
|
|
|
ya ,Accually I installed the Latest Version of SDK(updated 2003 October). But I get the Same error Message in My Program .
error C2039: 'ConvertFormat' : is not a member of 'Bitmap'
E:\PROGRAM FILES\MICROSOFT SDK\INCLUDE\GdiplusHeaders.h(488) : see declaration of 'Bitmap'
What to do now?.
|
|
|
|
|
This is what i got when i read the doc.
This is preliminary documentation and is subject to change.
I'll write a suicide note on a hundred dollar bill - Dire Straits
|
|
|
|
|
GDI+ 1.1 is not available despite the preliminary documentation in MSDN which apparently was a mistake. There is no word on when it will actually ship.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
How exactly can I create a zip file programmaticaly in C++. I know that I have to use a CRC table. A link to a website that contains source code for a program that takes a file and creates a zip file out of it would be great!
|
|
|
|
|
Check out sawzip here in codeproject
I'll write a suicide note on a hundred dollar bill - Dire Straits
|
|
|
|
|
In addition to the other link, info-zip has a lot of source code, and other information. (They claim that winZip is based on their code)
|
|
|
|
|
I'm not sure what I did there...
Anyway, info-zip at http://www.info-zip.org/pub/infozip/ has some more source.
|
|
|
|