Notices

Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

C++ Multidimensional Array

Post Reply New Thread Subscribe Search this Thread
 
 
Thread Tools
Old 11-11-03, 10:05 PM Thread Starter   #1
thef0x82
Member

 
thef0x82's Avatar 

Join Date: Jul 2002
Location: Nebraska

 
C++ Multidimensional Array


Okay, here's the catch, I'm trying to declare a 2-dimensional array dynamically.
The line:

mymatrix = new int[x][y]

doesn't work. What's the correct syntax?
thef0x82 is offline   QUOTE Thanks
Old 11-12-03, 01:31 PM   #2
Titan386
Senior Member

 
Titan386's Avatar 

Join Date: Jun 2002

 
I can give you the way I'd do that in C, which should also work in C++.

A multidimensional array is simply an "array of arrays." You'll understand it better once you start working with pointers.

Declaring the array is quite simple:

int bah[x][y];

where x and y are ints.

Now, if you want to use dynamic memory managament here, I'm not quite sure on the "proper" C++ way to do that. In C, I'd just use malloc() and free(), but I don't believe you should need those in C++.
Titan386 is offline   QUOTE Thanks
Old 11-12-03, 06:18 PM   #3
Cowboy Shane
Member



Join Date: Jan 2002
Location: Augusta, GA

10 Year Badge
 
My C++ is incredibly rusty, but would something like the following work?
Code:
int** DynArray = new int *[4];
		
	for(int i=0; i < ArraySize; i++)
		DynArray[i] = new int[4];

__________________
I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours. -- Stephen Roberts
Cowboy Shane is offline   QUOTE Thanks
Old 11-16-03, 03:08 AM   #4
macklin01
Computational Oncologist / Biomathematician / Moderator on Vacation, Ph.D.

 
macklin01's Avatar 

Join Date: Apr 2002
Location: Pasadena, CA

 
that's what I've always used. -- Paul

__________________
My heatware (macklin01)

Need image I/O for your science apps? Try EasyBMP

My biomedical research: Mathematical Cancer Modeling & Simulation

I'm on vacation as a moderator as I devote more time to my faculty position.
Thank you for your understanding if I don't respond to your PM. -- Paul
macklin01 is offline Folding Profile Heatware Profile   QUOTE Thanks
Old 11-16-03, 10:47 PM   #5
Annoyingrob
Member

 
Annoyingrob's Avatar 

Join Date: Feb 2001
Location: Calgary, Alberta, CanaDuh

10 Year Badge
 
heh, funny thing you asked this, just this morning I was working on an engineering assignment for c++, and had to declare a dynamic 2d array.

I used vectors to do it, as I find them easier to pass to procedures, not exactly what you need, but I can give you the code if you like.
Annoyingrob is offline   QUOTE Thanks

Post Reply New Thread Subscribe


Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Mobile Skin
All times are GMT -5. The time now is 08:14 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
You can add these icons by updating your profile information to include your Heatware ID, Benching Profile ID or your Folding/SETI profile ID. Edit your profile!
X

Welcome to Overclockers.com

Create your username to jump into the discussion!

New members like you have made this the best community on the Internet since 1998!


(4 digit year)

Why Join Us?

  • Share experience
  • Max out your hardware
  • Best forum members anywhere
  • Customized forum experience

Already a member?