GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Sep-2004, 22:02
dexter dexter is offline
Junior Member
 
Join Date: Jul 2004
Location: Queensland, Australia
Posts: 39
dexter will become famous soon enough
Question

Dynamic 2d Array Problems


Ok, what I'm trying to do is create a 2d array that has a user-specified size.

CPP / C++ / C Code:
int main()
{
	char **maze;
	
	// Assume a user inputted these values
	int mazeWidth = 13,
	    mazeHeight = 12;

	maze = new char*[mazeHeight];

	for(int i = 0; i < mazeHeight; i++)
		maze[i] = new char[mazeWidth];

             // Now, free the allocated memory
	for(i = 0; i < mazeWidth; i++)
		delete [] maze[i];

	delete [] maze;

	return(0);
}

Now, this works with a width of 12 and a height of 12 or 24 and 80 or other sizes, but if I put in width: 13 and height: 12, it gives me an assert error... why?
  #2  
Old 05-Sep-2004, 22:31
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by dexter
Ok, what I'm trying to do is create a 2d array that has a user-specified size.

CPP / C++ / C Code:
int main()
{
	char **maze;
	
	// Assume a user inputted these values
	int mazeWidth = 13,
	    mazeHeight = 12;

	maze = new char*[mazeHeight];

	for(int i = 0; i < mazeHeight; i++)
		maze[i] = new char[mazeWidth];

             // Now, free the allocated memory
	for(i = 0; i < mazeWidth; i++)
		delete [] maze[i];

	delete [] maze;

	return(0);
}

Now, this works with a width of 12 and a height of 12 or 24 and 80 or other sizes, but if I put in width: 13 and height: 12, it gives me an assert error... why?


You are allocating mazeHeight thingies in the first for loop.

You are deleting mazeWidth thingies in the second for loop.

By the way, i is not defined in the second loop.

Regards,

Dave
  #3  
Old 06-Sep-2004, 05:23
dexter dexter is offline
Junior Member
 
Join Date: Jul 2004
Location: Queensland, Australia
Posts: 39
dexter will become famous soon enough
Hehe, erm... oops is all I have to say... thanks.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Speed up C++ code about 3d array! Truong Son C++ Forum 0 16-Mar-2004 22:52
3D array dynamic memory allocation cjwatchdog C Programming Language 3 20-Feb-2004 16:27
c: array comparison jack C Programming Language 7 26-Jan-2004 12:21
Extra null element in an array samtediou MySQL / PHP Forum 2 11-Dec-2003 12:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:15.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.