How to create Pointer Array

B

Bryan Parkoff

I want to allocate pointer array into memory so pointer array contains
ten pointers. It would be 4 bytes per pointer to be total 40 bytes. Looks
like below for example.

unsigned char* A = new unsigned char [1000];

It has only one pointer contains 1,000 bytes. How can I do this to
create pointer list like below.

unsigned char** B = new (unsigned char*) [10]; // Pointer List contains
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.

Thanks...

Bryan Parkoff
 
M

Mike Wahler

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.
It has only one pointer

Right. The pointer object named 'A'.
contains 1,000 bytes.

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
 
O

Old Wolf

Bryan said:
Please correct "unsigned char** B = new (unsigned char*) [10];"
because it does not work.

The correct syntax is:

unsigned char **b = new unsigned char * [10];

You can't just chuck brackets around type names, like you
can with expressions.
 
M

Mike Wahler

Old Wolf said:
Bryan said:
Please correct "unsigned char** B = new (unsigned char*) [10];"
because it does not work.

The correct syntax is:

unsigned char **b = new unsigned char * [10];

You can't just chuck brackets around type names, like you
can with expressions.

I didn't even see that. :)

-Mike
 
V

Varun Sud

Hi,
I have written an article declaring and using multi-dimensional
pointers in C++. This article should exactly clarify the doubts that
have been raised in this question. Chapter 11 of the article should
solve *why* you need to modify your memory allocation step (seee bottom
of message) . It should also resolve other common issues related to
pointers that you may have, some of which are mentioned at the bottom
of the article.

Article link:
http://www.geocities.com/varunhostel/TechnicalArticles/PointerArticle/PointerArticle_Intro.html

Some of the questions answered in the article are:

Chapter 9: How do I dynamically allocate memory for a 3-dimensional
pointer equivalent to an array a[2][3][4]?
Chapter 6: To what extent can I inter-change pointer and array syntax
in C++?
Chapter 11: How do I decipher very long function pointer declarations?
Is there a standard approach for it?
Chapter 11: What is the difference between the two declarations: int
*ptr[5] and int (*ptr) [5]?


Thanks,
Varun Sud
http://www.geocities.com/varunhostel/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top