473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

multiarray

How can I declare a array as bool slice[x][y][z];
I want declare this in a class member.
Thanks.

Nov 10 '06 #1
20 1761
Thunder wrote:
How can I declare a array as bool slice[x][y][z];
I want declare this in a class member.
Thanks.

What are x,y,z? Give some detail as to what you want to do. Have you
tried something? What problems did you have? Is this supposed to be a
dynamic array?

Brian
Nov 10 '06 #2
Thunder wrote:
How can I declare a array as bool slice[x][y][z];
I want declare this in a class member.
So, what's the problem with just declaring it as 'bool slice[x][y][z]'?

--
Best regards,
Andrey Tarasevich
Nov 10 '06 #3

Default User ha scritto:
Thunder wrote:
How can I declare a array as bool slice[x][y][z];
I want declare this in a class member.
Thanks.


What are x,y,z? Give some detail as to what you want to do. Have you
tried something? What problems did you have? Is this supposed to be a
dynamic array?

x,y,z are parameters of function member.Yes It's a dynamic array.

I want to do something as this.
void func()
{
int sx=....
int sy=....
int sz=....
slice=new [sx] [sy] [sz];
}

My problem is the following I don't know as I should declare array
slice in member class,
and I don't know as allocate memory for this array.

Nov 10 '06 #4
Thunder wrote:
>
Default User ha scritto:
Thunder wrote:
How can I declare a array as bool slice[x][y][z];
I want declare this in a class member.
Thanks.

What are x,y,z? Give some detail as to what you want to do. Have you
tried something? What problems did you have? Is this supposed to be
a dynamic array?


x,y,z are parameters of function member.Yes It's a dynamic array.

I want to do something as this.
void func()
{
int sx=....
int sy=....
int sz=....
slice=new [sx] [sy] [sz];
}
Well, you can't do that. See the FAQs:

<http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.16>

You could do it like this:

int ***slice;

slice = new int**[sx];

for(int i = 0; i < sx; i++)
{
slice[i] = new int*[sy];
for(int j = 0; j < sy; j++)
slice[i][j] = new int[sz];
}

I recommend nesting std::vectors. Easier and less prone to problems.
I'd also look hard at the design that requires a three-dimensional
array.

My problem is the following I don't know as I should declare array
slice in member class,
That's not your problem, it's a design decision, but won't affect how
things are declared and defined.
and I don't know as allocate memory for this array.
That's your big problem. Figure that out first, then figure out where
it goes.


Brian
Nov 10 '06 #5

Default User wrote in message ...
>Thunder wrote:
>Default User ha scritto:
Thunder wrote:
How can I declare a array as bool slice[x][y][z];

What are x,y,z?

x,y,z are parameters of function member.Yes It's a dynamic array.

I want to do something as this.
void func(){
int sx=....
int sy=....
int sz=....
slice=new [sx] [sy] [sz];
}

Well, you can't do that. See the FAQs:
<http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.16>

You could do it like this:

int ***slice = new int**[sx];
for(int i = 0; i < sx; i++){
slice[i] = new int*[sy];
for(int j = 0; j < sy; j++)
slice[i][j] = new int[sz];
}

I recommend nesting std::vectors. Easier and less prone to problems.
I'd also look hard at the design that requires a three-dimensional
array.
Another idea:

{
cout<<"\n --- VecBit test ---"<<std::endl;
size_t const bits(3);
std::vector< std::bitset<bits VecBit( 7, 4 ); // 3x7 init to 4

VecBit.at(0)[0] = true;
VecBit.at(1)[1] = true;
VecBit.at(2) = 5;
VecBit.at(3) = 7;
VecBit.at(4) >>= 1;
std::bitset<bitsmore;
VecBit.push_back( more ); // add more
for(size_t i(0); i < VecBit.size(); ++i){
cout<<"VecBit.at("<<i<<")="<< VecBit.at(i) <<std::endl;
} //for(i)
size_t x(2), y(1), z(0);
if( VecBit.at(2)[y] ){ // if( VecBit.at(2).at(1) ) // no .at() for bitset
cout<<"true"<<std::endl;
}
else{
cout<<"false"<<std::endl;
}
cout<<" x="<<std::boolalpha<<VecBit.at(2)[x]
<<" z="<<VecBit.at(2)[z]<<std::endl;
cout<<" --- VecBit test ---end\n"<<std::endl;
}
/* --- VecBit test ---
VecBit.at(0)=101
VecBit.at(1)=110
VecBit.at(2)=101
VecBit.at(3)=111
VecBit.at(4)=010
VecBit.at(5)=100
VecBit.at(6)=100
VecBit.at(7)=000
false
x=true z=true
--- VecBit test ---end
*/

--
Bob R
POVrookie
Nov 11 '06 #6
Thunder:
How can I declare a array as bool slice[x][y][z];
I've seen elsethread that you're talking about dynamic allocation. You
could try something like the following:

#include <cstddef>

template<class ArrElemT>
class Arr3D {
private:

typedef std::size_t size_t;

size_t const s1,s2;

ArrElemT *const p;

public:

Arr3D(size_t const a,size_t const b,size_t const c)
: s1(b*c),s2(c),p(new ArrElemT[a*b*c]) {}

~Arr3D() { delete [] p; }

ArrElemT &GetElem(size_t const a,size_t const b,
size_t const c)
{
return *(p + a*s1 + b*s2 + c);
}
};
#include <iostream>
#include <cstdlib>

int main()
{
std::size_t x,y,z;

std::cin >x >y >z;

if (x<5 || y<5 || z<5) return EXIT_FAILURE;

Arr3D<doubleobj(x,y,z);

obj.GetElem(4,4,4) = 89.234;
}

--

Frederick Gotham
Nov 13 '06 #7
Frederick Gotham:

Arr3D(size_t const a,size_t const b,size_t const c)
: s1(b*c),s2(c),p(new ArrElemT[a*b*c]) {}

That would be better as:

new ArrElemT{a*s1)

--

Frederick Gotham
Nov 13 '06 #8
Frederick Gotham:
new ArrElemT{a*s1)

(, not {.

--

Frederick Gotham
Nov 13 '06 #9

Frederick Gotham wrote:
Thunder:
How can I declare a array as bool slice[x][y][z];

I've seen elsethread that you're talking about dynamic allocation. You
could try something like the following:

#include <cstddef>

template<class ArrElemT>
class Arr3D {
private:

typedef std::size_t size_t;

size_t const s1,s2;

ArrElemT *const p;

public:

Arr3D(size_t const a,size_t const b,size_t const c)
: s1(b*c),s2(c),p(new ArrElemT[a*b*c]) {}

~Arr3D() { delete [] p; }

ArrElemT &GetElem(size_t const a,size_t const b,
size_t const c)
{
return *(p + a*s1 + b*s2 + c);
}
};
A copy constructor and assignment operator are also needed, or it those
operations need to be disabled.

Gavin Deane

Nov 13 '06 #10

Frederick Gotham wrote:
Frederick Gotham:

Arr3D(size_t const a,size_t const b,size_t const c)
: s1(b*c),s2(c),p(new ArrElemT[a*b*c]) {}


That would be better as:

new ArrElemT{a*s1)
I think it is clearer as you had it originally.

Gavin Deane

Nov 13 '06 #11
Frederick Gotham wrote:
>
> new ArrElemT{a*s1)


(, not {.
...
That way you'd make your code to depend critically on the order of class
data member declaration. If someone accidentally (or intentionally)
changed the order to

...
ArrElemT *const p;
size_t const s1,s2;
...

the initialization would fail. That's one thing to remember about
initialization lists.

--
Best regards,
Andrey Tarasevich

Nov 13 '06 #12
Gavin Deane:
>That would be better as:

new ArrElemT{a*s1)

I think it is clearer as you had it originally.

Yes, I would agree that it is clearer, but efficiency takes precedence for me
99% of the time (i.e. the code only evaluates "b*c" once rather than twice).

--

Frederick Gotham
Nov 13 '06 #13
Andrey Tarasevich:
That way you'd make your code to depend critically on the order of class
data member declaration. If someone accidentally (or intentionally)
changed the order to

...
ArrElemT *const p;
size_t const s1,s2;
...

the initialization would fail. That's one thing to remember about
initialization lists.

I remember one time a few years back, I was writing some C++ code. I think I
changed compiler, or maybe simply upgraded compiler, but when I re-compiled
my program after the install, I got a new warning telling me that the
initialisation list order didn't match the declaration order. This was the
first I had heard of it! My intuition at the time made me assume that they're
initialised in order of their appearance in the initialisation list.

Which begs the question...

Why is it determined by declaration order rather than initialition list
order? The latter would seem to make _a lot_ more sense.

--

Frederick Gotham
Nov 13 '06 #14

Frederick Gotham wrote:
Why is it determined by declaration order rather than initialition list
order? The latter would seem to make _a lot_ more sense.
Does a class require a constructor? - No
Does a class require a definition? - Yes
Need a member be initialised in the member initialiser list? - No
Need a member be defined to be a member? - Yes.

I suppose there are more reasons, but the above seems to be reasons for
me. I do agree that a good compiler can (perhaps should) warn about
this, especially when there is some form of interdependence.

Another thing to consider is, is it right to provide weak associations
at construction time, or should the be realized after construction? Any
thoughts on this?

My thoughts are, requiring weak associates to exist at construction
time imposes more restrictions on the client. Usually (IME) the setting
of weak associations do not involve the possibility of exceptions. I do
not see why they have to be specified in the member initialiser list.
Giving the client an option is always good, but that could involve
writing many constructors. Ownership, of course is another matter.

Regards,

Werner

Nov 14 '06 #15
Frederick Gotham wrote:
...
Why is it determined by declaration order rather than initialition list
order? The latter would seem to make _a lot_ more sense.
...
There is an important rule strictly followed in most of C++: objects
must be destructed in the order that is reverse of the order of their
construction. This rule applies to class subobjects as well.

If C++ allowed "custom" subobject construction order (determined by
constructor initialization list, for example), compilers would have to
memorize that order at run-time for each object somehow, and then use it
later in each object's destructor in order to satisfy the
reverse-order-destruction requirement. That would take certain amount of
additional housekeeping data in each object and more complicated
housekeeping code in constructors and destructors. In order to avoid all
these extra expenses the order of construction (and destruction) was
made pre-determined at compile-time by the order of subobject declaration.

--
Best regards,
Andrey Tarasevich

Nov 14 '06 #16

Frederick Gotham wrote:
Gavin Deane:
That would be better as:

new ArrElemT{a*s1)
I think it is clearer as you had it originally.


Yes, I would agree that it is clearer, but efficiency takes precedence for me
99% of the time (i.e. the code only evaluates "b*c" once rather than twice).
Fair enough. That's up to you. There is also Andrey Tarasevich's point
that your constructor becomes critically reliant on the order of member
object declarations not changing.

For the benefit of the OP, if you are programming in a collaborative
environment (ie working with other programmers on the same project),
professionally or otherwise, your fellow programmers will not, in
general, thank you for sacrificing readability in this kind of way.
Their ability to do their job (ie develop and maintain code without
introducing bugs) depends on their ability to read all the code they
are working on - theirs and yours. If you are programming on your own,
you can, of course, do whatever you like.

Gavin Deane

Nov 14 '06 #17
Andrey Tarasevich:
If C++ allowed "custom" subobject construction order (determined by
constructor initialization list, for example), compilers would have to
memorize that order at run-time for each object somehow, and then use it
later in each object's destructor in order to satisfy the
reverse-order-destruction requirement.

My first thought was: So why not just use the initialisation list order
rather than the declaration order, when it occurred to me: You can have
more than one constructor, and therefore more than one initialisation list,
and therefore more than one order in which to construct the sub-objects.

Am I right in thinking that this is the reason why they went with
declaration order?

Anyhow, I suppose we'll just have to do with comments:

class Whatever {
private:

/* Don't change the declaration order below! */
int a;
double b;
char *c;
/* Don't change the declaratino order above! */

};

--

Frederick Gotham
Nov 14 '06 #18
Gavin Deane:
For the benefit of the OP, if you are programming in a collaborative
environment (ie working with other programmers on the same project),
professionally or otherwise, your fellow programmers will not, in
general, thank you for sacrificing readability in this kind of way.

If they're that heart broken, we can give them comments:

new ArrElemT[a*s1] /* s1 == b*c */

--

Frederick Gotham
Nov 14 '06 #19

Frederick Gotham wrote:
Gavin Deane:
For the benefit of the OP, if you are programming in a collaborative
environment (ie working with other programmers on the same project),
professionally or otherwise, your fellow programmers will not, in
general, thank you for sacrificing readability in this kind of way.


If they're that heart broken, we can give them comments:

new ArrElemT[a*s1] /* s1 == b*c */
Indeed. Whenever performance considerations require you to obfuscate
your code, the next best thing is documentation to clarify your reasons
and intent.

Gavin Deane

Nov 15 '06 #20
Gavin Deane:
Indeed. Whenever performance considerations require you to obfuscate
your code, the next best thing is documentation to clarify your reasons
and intent.

Agreed.

--

Frederick Gotham
Nov 17 '06 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: benevilent | last post by:
Hey, I have made a Python build with the flag --with-pydebug. When I try to import modules such as NumPy's multiarray.so, I get errors like the following: File...
11
by: fivelitermustang | last post by:
Actually, how would I go about allocating a four-dimensional dynamic array? I only know how to make two dimensional dynamic arrays: double **v; v = new double*; for (int i=0; i<C; i++) { v =...
4
by: bg_ie | last post by:
Hi, I have an array as follows - var MultiArray = new Array(2); MultiArray = new Array(num); MultiArray = new Array(num); I've tried randomizing this array using the following but its...
4
by: Jon Slaughter | last post by:
I wrote some code to read in a file and I'm curious as to is this is a decent way to do it or if there is a much more elegant way. Essentially I want to convert a text file that into a ul in...
4
by: artev | last post by:
if I have one simple array 1 4 2 6 for to order the code is: arrayOrdin=arraySimple.sort(); and I have 1
5
by: Doug Morse | last post by:
Hi, I have an application that runs just fine using the standard Python distro interpreter (v2.5.1 on WinXP) but throws the following exception when run as an executable built with py2exe. My...
0
by: John [H2O] | last post by:
There's a lot of greek for me here ... should I post to numpy-discussions as well??? The backtrace is at the bottom.... Thanks! GNU gdb Fedora (6.8-21.fc9) Copyright (C) 2008 Free...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.