-
June 11th, 2009, 04:27 AM
#1
intialising array
Dear all,
I am trying to create and initialize a two dimensional and dynamic boolean array with all elements set to zero.
I tride
Code:
bool** myarray;
myarray= new bool[dim1][dim2]
But that doe snot work.
Do you have any suggestions?
Thanks
-
June 11th, 2009, 04:47 AM
#2
Re: intialising array
Code:
Code:
bool myarray[11][12];
memcpy(myarray,0,sizeof(myarray));
With regards
Programartist
-
June 11th, 2009, 04:53 AM
#3
Re: intialising array
dim1 and dim2 are not constants
-
June 11th, 2009, 05:02 AM
#4
Re: intialising array
Hi,
There's not a one-line way of doing it, since C++ requires the first dimension to be constant.
If you don't need the memory to be continguous you could create an array of length dim1, then loop dim1 times and create arrays of size dim2, and store pointers to those in the first array.
If you need the memory to be contiguous, you could create a 1D array of size dim1*dim2, then calculate the indexes yourself when you need to access it.
You can use memcpy as suggested earlier to initialize each array.
However, it's probably best to use vector from the std template library anyway, unless you need to use an array for some specific reason.
Alan
-
June 11th, 2009, 05:07 AM
#5
Re: intialising array
Originally Posted by ProgramArtist
Code:
Code:
bool myarray[11][12];
memcpy(myarray,0,sizeof(myarray));
With regards
Programartist
You shouldn't be using memcpy() on bool arrays. A bool can only be assigned true or false. Using memcpy() bypasses this
check and just blindly writes 0, which may or may not be correct (depending on the implementation).
Regards,
Paul McKenzie
-
June 11th, 2009, 07:38 AM
#6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|