Bryan Parkoff said:
I want to allocate pointer array into memory so pointer array contains
ten pointers.
OK, so far so good.
it might
be 4 bytes per pointer to be total 40 bytes.
It might be 10 bytes per pointer, giving a total of 100 bytes.
Or some other size. The size of a pointer depends upon the
implementation and is not specified by the language. Also note
that different pointer types need not have the same size, e.g.
sizeof(int*) could be different from sizeof(char*).
Looks like below for example.
unsigned char* A = new unsigned char [1000];
This is not an array of pointers. It's a single pointer
containing the address of the first of 1000 allocated bytes.
Right. The pointer object named 'A'.
No. The pointer 'contains' sizeof(char*) bytes. If your remark above
is correct for your implementation, then this would be four bytes.
How can I do this to create pointer list like below.
unsigned char** B = new (unsigned char*) [10]; // Pointer List contains
ten pointers
Yes, you've allocated an array of ten pointers.
B[0] = A;
B[1] = A + 0x10;
B[2] = A + 0x20;
A is 0x00440068.
B is 0x00500068
B[0] is 0x00440068
B[1] is 0x00440078
B[2] is 0x00440088
Please correct "unsigned char** B = new (unsigned char*) [10];" because
it does not work.
The syntax is correct. Please define "does not work".
#include <iostream>
int main()
{
unsigned char *p1 = new unsigned char[10];
unsigned char **p2 = new unsigned char*[10];
p2[0] = p1;
p2[1] = p1 + 0x10;
p2[2] = p1 + 0x20;
std::cout << "p1 == " << static_cast<void*>(p1) << '\n';
std::cout << "p2[0] == " << static_cast<void*>(p2[0]) << '\n';
std::cout << "p2[1] == " << static_cast<void*>(p2[1]) << '\n';
std::cout << "p2[2] == " << static_cast<void*>(p2[2]) << '\n';
return EXIT_SUCCESS;
}
Output:
p1 == 00321D10
p2[0] == 00321D10
p2[1] == 00321D20
p2[2] == 00321D30
Perhaps you're forgetting (or never knew) that (signed or unsigned) 'char*'
argument to ostream << operator is interpreted as a "C-style" string.
I.e.
const char *p = "hello"
std::cout << p << '\n';
.... will output the characters in the string "hello", and
*not* the value of the pointer 'p'. See my example above for
how to output your char* pointer values.
Also beware: Attempts to dereference those 'made up' pointer
values (p1 + 0x10, etc.) will yield undefined behavior, since
all their values lie outside the bounds of the array pointed
to by 'p1'.
Which leads me to ask: specifically what are you trying to do?
Why do you feel the need to use operator 'new'? Why not define
your arrays directly, e.g.:
unsigned char p1[10];
unsigned char *p2[10];
p2[0] = p1;
p2[1] = p1 + 0x10;
// etc.
It seems to me that a large number of folks learning C++ somehow
believe they need 'new' when they really don't, and it only serves
to complicate things needlessly.
Which C++ book(s) are you studying?
-Mike