473,799 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16461
"Bryan Parkoff" <no****@nospam. com> wrote in message
news:fP******** *******@newssvr 11.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<voi d*>(p1) << '\n';
std::cout << "p2[0] == " << static_cast<voi d*>(p2[0]) << '\n';
std::cout << "p2[1] == " << static_cast<voi d*>(p2[1]) << '\n';
std::cout << "p2[2] == " << static_cast<voi d*>(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*****@inspir e.net.nz> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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
2526
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
2363
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. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
7
8874
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 want my users to be able to select a report, click on a command button on a form, which will then automatically create the report as a pdf file and save it to the user's machine. I am using Adobe Acrobat (5.0 I think) and have Adobe Distiller as a
204
13136
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 = {0,1,2,4,9};
1
617
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 platform which has the following properties: 1) char's are 8-Bit. ( "char" is synomonous with "byte" ). 2) int's are 32-Bit. ( sizeof(int) == 4 ). 3) Pointers are 64-Bit. ( sizeof(int*) == 8 ).
8
17721
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 unsigned chars, i'll do this :
42
5348
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
7422
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
14
1981
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 <string.h> int main(void)
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9543
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10029
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7567
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.