|
int *array[SIZE];
for(int i=0;i<SIZE;i++)
array[i] = new int;
Nish
The posting stats are now in PDF:-
http://www.busterboy.org/codeproject/
Feel free to make your comments.
Updated - May 04th, Saturday
|
|
|
|
|
You were close:
int **array = new int*[SIZE];
That will allocate an array of integer pointers that has SIZE number of elements.
Just for everyone's knowledge, to access the pointers in this new array, you will need to do something like this:
int a = 10;
int b = 20;
int c = 30;
array[0] = &a;
array[1] = &b;
array[2] = new int;
*array[2] = 50;
array[3] = new int[10];
array[3][0] = 100;
just remember to delete any memory that you allocate, even in the pointers inside of your allocated array.
Good Luck!
Build a man a fire, and he will be warm for a day Light a man on fire, and he will be warm for the rest of his life!
|
|
|
|
|
Some day I asked Bjarne Stroustrup a question to dynamically allocating square array ( 2 X 2 or 3 X 3 or 10 X 10 ... ), He told me to use vector!
Maxwell Chen
People say "No news is good news". Then, no code is good code!?
|
|
|
|
|
He's a wise man.
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
|
|
|
|
|
His way is nice!
But finally I approached my goal via a template class that wraps a low-level 2d int array.
Maxwell Chen
People say "No news is good news". Then, no code is good code!?
|
|
|
|
|
Hi.
I would like to create a generic class that wraps around, say, an API function. I need a way to pass into a function parameters such as FEATURE1 | FEATURE3. I believe that requires the use of bitwise AND, OR, and NOT similar to MFC programming style.
For example:
-----
// call function to initialize variables
SetVariableA(FEATURE1 | FEATURE 3) // I want feature1 and feature3
-----
Here is what SetVariableA(...) might look like
-----
enum STYLE {FEATURE1 = 0, FEATURE2, FEATURE3}
SetVariableA(...)
{
}
-----
Basically, I am not familiar with the use of enum with bitwise AND, OR, and NOT. Here is a what I would like to accomplish.
- user calls function to setup a feature: function(FEATURE1)
- function determines what feature the user wants
How would you implement some like the above using enum, bitwise AND, OR, and NOT?
Thanks,
Kuphryn
P.S. MFC programmers seem to use the bitwise AND, OR, and NOT
|
|
|
|
|
setVariableA (UINT bitMask)
{
enum STYLE styleIterator;
for (enum STYLE styleIterator=FEATURE1; (styleIterator <= FEATURE3) styleIterator++) {
if (bitMask & styleIterator) {
...
}
}
} /ravi
"There is always one more bug..."
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
Hi Ravi,
Your code doesn't compile, there're several errors. Check your code, compile it, and you will see!
Maxwell Chen
People say "No news is good news". Then, no code is good code!?
|
|
|
|
|
Thanks.
Your sample code helped me understand *almost* everything. I have two questions.
1) I assume enum STYLE is *defined* as global inside whatever class setVariableA(UINT bitMask) belongs to. Right?
-----
enum STYLE {FEATURE1 = 0, FEATURE2, FEATURE3}
class CArbitrary
{
public:
setVariableA(UINT bitMask);
};
-----
If yes: Will the class that declare CArbitrary understand what, for example, FEATURE1 actually is?
For example:
-----
setVariableA(FEATURE2);
-----
// FEATURE2 is defined is enum STYLE
// However, SetVariableA(UINT bitMask)
// UINT == enum STYLE?
2) In the code you posted, I cases (bitMask & FEATURE1), etc., but how about cases such as FEATURE1 | FEATURE3?
Thanks,
Kuphryn
|
|
|
|
|
Hi Kuphryn,
While wrapping such functions in a class, you use this way:
#include <iostreamᡊe9ac73-238b-46be-991d-0f050c37248dusing namespace std;
class MyCtrl
{
public:
enum ST { enType1 = 0x01, enType2 = 0x02, enType3 = 0x04 };
void SetVar(int style)
{
int tt;
for(tt = enType1; tt <= enType3; tt = (tt << 1))
{
if(style & tt)
cout << "Type " << tt / 2 + 1 << " implemented\n";
}
}
};
void main()
{
MyCtrl ctrl;
ctrl.SetVar(MyCtrl::enType1 | MyCtrl::enType3);
}
Maxwell Chen
People say "No news is good news". Then, no code is good code!?
|
|
|
|
|
Ugh. What was I thinking? Of course you have to bit shift (and not sequence thru like I did) a mask. I'm going to hold doff posting until I'm sufficiently awake!
/ravi
"There is always one more bug..."
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
Maxwell Chen
People say "No news is good news". Then, no code is good code!?
|
|
|
|
|
I used to do this way......
#include <iostreamᡊe9ac73-238b-46be-991d-0f050c37248dusing namespace std;
enum ST { enType1 = 0x01, enType2 = 0x02, enType3 = 0x03 };
void SetVar(int style)
{
int tt;
for(tt = enType1; tt <= enType3; tt = (tt << 1))
{
if(style & tt)
cout << "Type " << tt / 2 + 1 << " implemented\n";
}
}
void main()
{
SetVar(enType1 | enType3);
}
Maxwell Chen
People say "No news is good news". Then, no code is good code!?
|
|
|
|
|
Thanks.
Please read my reply above.
Kuphryn
|
|
|
|
|
I would not use an enum, but rather just a set of defines for every flag
#define FEATURE1 0x00000001 // bit one
#define FEATURE2 0x00000002 // bit two
#define FEATURE3 0x00000004 // bit three
#define FEATURE4 0x00000008 // bit four
#define FEATURE5 0x00000010 // bit five
.
.
. This will give you up to 32 flags.
Then, the parameter used to set the flags would be a DWORD
void SetVariableA (DWORD dwflags = 0);
You would call it this way
SetVariableA (FEATURE1 | FEATURE3);
Then, inside SetVariableA() you would check to see which flags have been set by using the bitwise AND ( & ) operator
void SetVariableA (DWORD dwFlags)
{
if (dwFlags & FEATURE1)
{
}
if (dwFlags & FEATURE2)
{
.
.
.
HTH
---
CPUA 0x5041
Sonork 100.11743 Chicken Little
If a man is standing in the middle of the forest speaking and there is no woman around to hear him...is he still wrong?
|
|
|
|
|
Thanks.
You, Ravi Bhavnani, and Maxwell Chen posted three very similar techniques. All seems important. Your technique, however, make use of #define instead of enum and DWORD instead of UINT.
1) Can you explain #define over enum and DWORD of UINT?
2) Can I use #define FEATURE1 1, #define FEATURE2 2, etc.. instead of hex?
3) I asked Maxwell Chen about calling the FEATURE1 *outside* of the class.
For example:
-----
int main()
{
...
SetVariableA(FEATURE1);
-----
How does mean know what FEATURE1, FEATURE2, etc. are?
Last, this question applies to everyone. In the function,
// SetVariableA (FEATURE1 | FEATURE3);
I believe that means you want FEATURE1 and you want FEATURE3. However, in the implementation of SetVariableA(UINT) or SetVariableA(DWORD), there are cases for (dwFlags & FEATURE1), (dwFlags & FEATURE2), etc. How about something like:
// FEATURE1 | FEATURE3
Oh. I think I know what is going on. For every "if cases" the statement sets *each* FEATURE accordingly. For example:
// SetVariableA (FEATURE1 | FEATURE3);
// SetVariableA(UINT flags) or SetVariableA(DWORD flags)
{
if (flags & FEATURE1) // applying to the call above, YES
if (flags & FEATURE2) // NO
if (flags & FEATURE3) // YES
}
Am I on track?
Kuphryn
|
|
|
|
|
kuphryn wrote:
1) Can you explain #define over enum and DWORD of UINT?
with an enum, you can loop over the values, so technically it probably better, but I prefer #defines, They are easier IMO
On 32 bit systems, there is no difference between UINT and DWORD. On 16 bit systems UINT is 16 bits, DWORD is 32 bits
kuphryn wrote:
2) Can I use #define FEATURE1 1, #define FEATURE2 2, etc.. instead of hex?
Yes you can, but remember that you are setting the individual bits, so your values would be 1, 2, 4, 8, 16, 32, 64, 128 etc. using hex is just easier IMO.
just remember that the bitwise OR operator, when used with properly defined flags, acts as a plus operator.
ie :
1 | 2 == 3
1 | 4 == 5
2 | 4 == 6
1 | 2 | 4 == 7
...
kuphryn wrote:
How does mean know what FEATURE1, FEATURE2, etc. are
that is why I put the defines in the header file. Just include the header file in which ever file you use the flags.
kuphryn wrote:
Am I on track?
Yes. Be sure to read up on bitwise operators in MSDN.
HTH
---
CPUA 0x5041
Sonork 100.11743 Chicken Little
If a man is standing in the middle of the forest speaking and there is no woman around to hear him...is he still wrong?
|
|
|
|
|
I wrote something wrong, so I deleted it.
#define preprocessor directive affetcs everywhere after the compiler "sees" it.
Enum is something like you are creating some integral type. Those members of a certain enum type, you may take them as integral number, like int, short.
The compiler optimizes the size, if all members in a enum type are less than maximum value of short, then they are short. Else they are taken as int or long... If beyond maximum of (unsigned) int or long, you get a error compiling msg!
Bjarne recommends C++ programmers less using MACROs.
Maxwell Chen
People say "No news is good news". Then, no code is good code!?
|
|
|
|
|
Okay. Thanks everyone.
I will start out using #define and DWORD.
Kuphryn
|
|
|
|
|
You can see the value of an enum in the debugger. With a define you cannot.
typedef enum
{
FEATURE1 = 0x0001,
FEATURE2 = 0x0002,
FEATURE3 = 0x0004,
FEATURE4 = 0x0008,
FEATURE5 = 0x0010,
} FEATURES;
Todd Smith
|
|
|
|
|
I want to make sure I have the idea behind hex done. Here is the 32 flags using hex.
#define 0x0000001
#define 0x0000002
#define 0x0000004
#define 0x0000008
#define 0x0000010
#define 0x0000020
#define 0x0000040
#define 0x0000060
#define 0x0000080
#define 0x0000100
#define 0x0000200
#define 0x0000400
#define 0x0000600
#define 0x0000800
#define 0x0001000
#define 0x0002000
#define 0x0004000
#define 0x0006000
#define 0x0008000
#define 0x0010000
#define 0x0020000
#define 0x0040000
#define 0x0060000
#define 0x0080000
#define 0x0100000
#define 0x0200000
#define 0x0400000
#define 0x0600000
#define 0x0800000
#define 0x1000000
#define 0x2000000
#define 0x4000000
#define 0x6000000
#define 0x8000000
etc...
Does the above fit the pattern of 1, 2, 4, 6, 8, 16,... that PJ Arends mentioned?
Kuphryn
|
|
|
|
|
Oh boy, that makes your finger tired...
Don't code too much!
Use this:
#define BIT_MASK(n) (0x1 << (n))
Maxwell Chen
People say "No news is good news". Then, no code is good code!?
|
|
|
|
|
Thanks.
// #define BIT_MASK(n) (0x1 << (n))
Ahhh. How many bits does the << operator add to the hex?
-----
#define BIT_MASK(0) (0x1 << (0)) = 0x1
#define BIT_MASK(1) (0x1 << (1)) = 0x2
#define BIT_MASK(2) (0x1 << (2)) = 0x4
#define BIT_MASK(3) (0x1 << (3)) = 0x6
// According to PJ Arends and Michael Dunn, there is no 6 bits
...
Kuphryn
|
|
|
|
|
The bit-wise operator << shifts the amount of bits depending on the number after this operator. Therefore,
0x1 (binary 00000001) shifts 1 position you get 0x2 (binary is 00000010).
0x3 (binary 00000011) shifts 3 position you get 0x18 (binary is 00011000).
kuphryn wrote:
#define BIT_MASK(3) (0x1 << (3)) = 0x6
What you wrote this is wrong! Should be 0x8.
Maxwell Chen
People say "No news is good news". Then, no code is good code!?
|
|
|
|
|
|