473,396 Members | 2,076 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,396 software developers and data experts.

How to create Pointer Array

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
Aug 29 '05 #1
4 16439
"Bryan Parkoff" <no****@nospam.com> wrote in message
news:fP***************@newssvr11.news.prodigy.com. ..
I want to allocate pointer array into memory so pointer array contains
ten pointers.
OK, so far so good.
It would
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
Aug 29 '05 #2
Bryan Parkoff wrote:

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.

Aug 30 '05 #3

"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Bryan Parkoff wrote:

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
Aug 30 '05 #4
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...cle_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/

Aug 31 '05 #5

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

Similar topics

9
by: iceColdFire | last post by:
HI, I have a function as void f(int p) { return p++; } now I have created a function pointer as
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
1
by: Tomás | last post by:
Some programmers treat arrays just like pointers (and some even think that they're exactly equivalent). I'm going to demonstrate the differences. Firstly, let's assume that we're working on a...
8
by: ptek | last post by:
Hi all, I'm quite new to pointers, so this might be a silly question :S I need to allocate an array of pointers to unsigned char type... So, if I needed instead to allocate an array of...
42
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
14
by: Lambda | last post by:
I'd like to create separate character pointers, pass them to a function to assign each one different value, and assign the pointers to an array. But when I try: #include <stdio.h> #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.