473,487 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Accessing values in a 2D array

3 New Member
Hi everyone. I've recently been asigned a program where we are required to create a 2D array dynamically and then do stuff with it. We are not allowed to "cheat" and use the 1D method. in the constructor of my class i have defined the array as:

//GamePiece is a different user defined class, size is an integer passed to the constructor and handle is the name of the 2D array

handle = new GamePiece*[size];

for(i=0;i<size;i++)
{
handle[i] = new GamePiece[size];
}

In the constructor i have no problem accessing the values in the array with

handle[row][column]

but in any other function in this class i get an unhandled exception, access violation reading location 0x000000.... error. my quesiton is:

is this the correct way to access the values stored in the 2D array within this class or should i overload the () operator?

if i need to overload the () operator, do you have any suggestions on how to go about that? i know how to overload the =, and the <</>>, but i've never had to do the () before. while i'm sure it would be similar to the others, i'd appreciate a little direction if you don't mind. Thanks!
Jul 3 '07 #1
4 2466
weaknessforcats
9,208 Recognized Expert Moderator Expert
Actually, there are only one-dimensional arrays in C++. The first index is the number of elements in the array:
Expand|Select|Wrap|Line Numbers
  1. int arr[5];        //5 elements. Each element an int
  2. int arr1[5][3];    //5 elements. Each element an array of 3 int
  3. int arr2[5][3][4];  //5 elements. Each element an array of 3 elements that are arrays of 4 int
  4.  
The "dimensions" aare just you telling the compiler about the sizeof the elements.

So, here you have allocated a one dimensional array:
handle = new GamePiece*[size];
This is an array of GamePiece pointers.

Is the GamePiece itself an array??

If not, you can't use the handle[row][column] syntax. All this syntax does is give the compiler instrustions on how to calculate the address you need.

Have you initialized the GamePiece pointers before using them??

An 0x00000000 access violation looks like an attempt to use a null pointer. Big no-no.

There is no indication here that you need to overload the function operator. You are not using functors that I can see.
Jul 4 '07 #2
vanderr
3 New Member
the first statement,
handle = new GamePiece*[size];
is declaring an array of gamepiece pointers, the for loop, i thought was making each of those pointers point to an array of gamepiece objects. Is my declaration incorrect? How can i initialize the pointers to fix this error.

we're not allowed to use the [][] syntax for declaring the array, but my teacher said that we could use that syntax for retrieving the values. like i said, i've gotten it all to work in the constrtuctor, but the other functions get the access violation error.
Jul 4 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
the first statement,
handle = new GamePiece*[size];
is declaring an array of gamepiece pointers, the for loop, i thought was making each of those pointers point to an array of gamepiece objects. Is my declaration incorrect? How can i initialize the pointers to fix this error.

we're not allowed to use the [][] syntax for declaring the array, but my teacher said that we could use that syntax for retrieving the values. like i said, i've gotten it all to work in the constrtuctor, but the other functions get the access violation error.
You have to use the [][] syntax to allocate an array of arrays.

Let's assume you are doing chess so you have a board of 8 rows and 8 columns. Each square is an bool that is 1 (occupied) or 0 (vacant).

The 2D array is created by:
Expand|Select|Wrap|Line Numbers
  1.  bool  (*board)[8] = new bool[8][8];
  2.  
Since the namne of the array (board) is the address of element 0, and element 0 is an array of 8 bool, then you need to declare a pointer to an array of 8 bool.

This is wrong:
Expand|Select|Wrap|Line Numbers
  1. bool** board = new bool[8][8];   //won't compile
  2. bool* board  = (bool*)new bool[8][8];  //don't lie to the compiler. Just do bool[64]
  3. bool* board  = new bool[64];     //OK but you lose the 8 rows 8 columns structure
  4.  
That said, this allocation:
handle = new GamePiece*[size];
allocates an array of GamePiece* but the pointers don't point to a GamePiece.

You will need to allocate a GamePiece for each of these pointers:
Expand|Select|Wrap|Line Numbers
  1. handle[0] = new GamePiece;
  2. //etc...
  3.  
This is not a 2D array. It is just a 1D array of GamePiece pointers that should be pointing to GamePiece objects.

You will not be able to use handle[row][column] because there no columns.
Jul 4 '07 #4
vanderr
3 New Member
thanks anyway, but this code is correct

m_board = new GamePiece*[size];

for(i=0;i<size;i++)
{
m_board[i] = new GamePiece[size];
}

for dynamically allocating a 2D array. i was initializing the pointer to null in the member initialization area, and the executable code wasn't correctly initializing it. once i got rid of the null in the member init, my code has been working fantastically. i still use [][] to access the individual values, but the above is how i declared it.
Jul 5 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
6849
by: Pedro Fonseca | last post by:
Greetings everyone! I'm porting everything to PHP5. I have session variables in all of my web application. Until PHP5 I was using session variables like: if ($_SESSION == 'Bar') { $value = 5;...
6
12743
by: ASPfool | last post by:
Hello everyone, Please help me before I throw my computer out of the window: I'm working on a web application in classic ASP (vbscript), which is making calls to some webservices. The calls...
27
4164
by: Daniel Lidström | last post by:
Hello! I want to work with individual bytes of integers. I know that ints are 32-bit and will always be. Sometimes I want to work with the entire 32-bits, and other times I want to modify just...
1
1589
by: Jason Bell | last post by:
Every example of properties I've seen have used simple types such as integers and strings. Here's the scenario I'm trying to work out (3D graphics programming): I have a class called...
3
975
by: Bob Altman | last post by:
Hi all, In unmanaged C++, I need to allocate an "array" of int values, put values into the array, then call a function and pass it the address and length of the array. The length of the array...
3
1550
by: mtjarrett | last post by:
i having trouble accessing the values from superglobal arrays. there are two situations but i'm pretty sure it's the same problem. here's the deal: on page1.php i have several check boxes. ...
7
3787
by: Chuck Anderson | last post by:
I'm pretty much a JavaScript novice. I'm good at learning by example and changing those examples to suit my needs. That said .... ..... I have some select fields in a form I created for a...
6
10374
by: Chuck Anderson | last post by:
My knowledge of JavaScript is limited. I learn from example and then adapt those examples to suit my needs. I have stumped myself on this one. I have a form with checkboxes that I want to...
16
6757
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
0
7137
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
7181
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...
1
6846
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
7349
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...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
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 ...
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
267
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...

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.