C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 7th, 2005, 04:03 AM
lefnire lefnire is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 4 lefnire User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 45 sec
Reputation Power: 0
2d pointers, the beaten topic!

So I'm making a game, go figure, and I'm trying to construct a map class that holds tiles. Anyway, I make and int[][] so as to straightup assign tiles like:
int Tiles[][]={{1,1},{2,2},};
which I can't do with int**, so I convert Tiles[][] to int** and pass to a function like so:
myMap.UpdateMap((int**)&Tiles,...);

So after all is said and done, it compiles but gives me the following error when I try to run it:
Unhandled exception at 0x00496e16 in CreateDevice.exe: 0xC0000005: Access violation reading location 0x00000000.

Code:
class cMap
  {
  private:
  	const static int TILESIZE=64;
  	int width, height;
  	int columns, rows;
  	
  
  public:
  	cMap(){}
  	int **Tiles;
  	void UpdateMap(int** arr, int row, int col)
  	{ 
  		memcpy(&Tiles, &arr, sizeof(int)*width*height);
  			rows=row;
  			height=row*TILESIZE;
  			columns=col;
  			width=col*TILESIZE;
  	}
  	void Paint(LPDIRECT3DDEVICE9 g_pd3dDevice)
  	{
  		g_pd3dSprite->Begin(D3DXSPRITE_ALPHABLEND);
  		for (int x=0; x<columns; x++){
  			for (int y=0; y<rows; y++){
  
  				RECT SrcRect;
 //it's right here that it gives me beef:
 				SrcRect.left=(Tiles[x][y])*TILESIZE;
 				SrcRect.right=SrcRect.left+TILESIZE;
  				SrcRect.top=0;
  				SrcRect.bottom=TILESIZE;  
  
 				g_pd3dSprite->Draw(g_pd3dTexture,
 					&SrcRect,
  					NULL,
 		 		&D3DXVECTOR3( (FLOAT)x*TILESIZE, (FLOAT)y*TILESIZE, 0.0f), //position
 					D3DCOLOR(0xFFFFFFFFF));
  			}
  		}
  		g_pd3dSprite->End();
  	}
  }myMap;


and in the main method:
Code:
int arr[3][3]={{2,2,2},{2,2,2},{2,2,2}};
  	myMap.UpdateMap((int**)&arr, 3,3);


I'd keep trying on my own, but I've been trying for days and i'm dumb. Thanks in advance.

Reply With Quote
  #2  
Old May 7th, 2005, 09:27 AM
ShadowCoder ShadowCoder is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: Ottawa
Posts: 20 ShadowCoder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 12 sec
Reputation Power: 0
For starters you're not allocating any memory for your "int **Tiles;" member var before doing a memcpy on it. Secondly, when it comes to allocating a multidimensional array, all dimensions except the first must be constant so if the size of array is going to be dynamic you're going to need to come up with a different algorithm ... hope this helps a bit.
Code:
int nNum = 99;
char (*pszArray)[10] = new char[nNum][10];
delete[] pszArray;

Reply With Quote
  #3  
Old May 7th, 2005, 05:48 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
Code:
int arr[3][3]={{2,2,2},{2,2,2},{2,2,2}};
myMap.UpdateMap((int**)&arr, 3,3);


when passing an array don't use the & operator. correct:

Code:
int arr[3][3]={{2,2,2},{2,2,2},{2,2,2}}; myMap.UpdateMap((int**)arr, 3,3); //no &


Reply With Quote
  #4  
Old May 10th, 2005, 01:33 PM
lefnire lefnire is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 4 lefnire User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 45 sec
Reputation Power: 0
Quote:
when passing an array don't use the & operator. correct:

Code:
int arr[3][3]={{2,2,2},{2,2,2},{2,2,2}}; myMap.UpdateMap((int**)arr, 3,3); //no &


aha! there we go, thank you!
Actually, I'm not going to even bother anymore.. I decided to use one of those flexible functions, or whatever they're called witht he function(int width, int height, ...) and reading every value after height as an entry in a 2d array. That way, i didn't have to allocate memory for 2 arrays and ditch the first, and I didn't have to bother with how to reference a 2d array.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > 2d pointers, the beaten topic!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT