Click here to Skip to main content
16,010,334 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Put value in the registry Pin
Carlos Antollini5-Nov-01 10:58
Carlos Antollini5-Nov-01 10:58 
GeneralRe: Put value in the registry Pin
5-Nov-01 15:14
suss5-Nov-01 15:14 
GeneralDynamic Multidimensional Array or Alternatives Pin
Steve L.5-Nov-01 9:43
Steve L.5-Nov-01 9:43 
GeneralRe: Dynamic Multidimensional Array or Alternatives Pin
Ravi Bhavnani5-Nov-01 10:05
professionalRavi Bhavnani5-Nov-01 10:05 
GeneralRe: Dynamic Multidimensional Array or Alternatives Pin
Chris Losinger5-Nov-01 10:09
professionalChris Losinger5-Nov-01 10:09 
GeneralRe: Dynamic Multidimensional Array or Alternatives Pin
Remi Morin5-Nov-01 10:15
Remi Morin5-Nov-01 10:15 
GeneralRe: Dynamic Multidimensional Array or Alternatives Pin
Todd Smith5-Nov-01 13:29
Todd Smith5-Nov-01 13:29 
GeneralRe: Dynamic Multidimensional Array or Alternatives Pin
Sprudling6-Nov-01 3:49
Sprudling6-Nov-01 3:49 
C++ doesn't directly support dynamic multidimensional arrays, however there are ways although a bit more complicated than necessary. Here is another example on how to create one. Unlike Remi Morin's "version" this one is a little harder to create but alot easier to use. And it's alot faster too I guess, since there is no multiplication necessary to access the data. This version does take up a bit more memory though...:

#include "memory.h"
#include "new.h"
//
void main()
{
	int x = 4, y = 7, z = 9; // 3D array
	int i, j, k;
//
	// Allocate memory
	int nSize = x * y * z * sizeof(int);
	int* p = (int*)::operator new(x * y * z * sizeof(int));
	int* q = p; // Used while creating the array
	::memset(p, 0, nSize); // Initialize all array data to zero
//
	// Create the array
	int*** a = new int**[x];
	for (i = 0; i < x; i++)
	{
		a[i] = new int*[y];
		for (j = 0; j < y; j++)
		{
			a[i][j] = new (q) int[z];
			q += z;
		}
	}
//
	// Set values to all values in the array
	for (i = 0; i < x; i++)
	{
		for (j = 0; j < y; j++)
		{
			for (k = 0; k < z; k++)
			{
				a[i][j][k] = i + (j * x) + (k * x * y);
			}
		}
	}	
//
	// Deallocate the array -  NB! No destruction
	for (i = 0; i < x; i++)
	{
		delete[] a[i];
	}
	delete[] a;	
	::operator delete(p);
}
Remember that the array objects will not be destructed using the destructor (if any) when deleted. It's possible to make that happen too of course by implicitly calling each and every destructor before deleting the array data. This must be done if the data stored in the array is not of a primitive type, or if for some reason the objects do not need to be destructed.

The included "new.h" file is necessary for using placement new used in the last for-loop in the process of creating the array. This places the allocated object(s) (int's in this example) at the specified pointer (q).

Sprudling Smile | :)
GeneralRe: Dynamic Multidimensional Array or Alternatives Pin
Remi Morin7-Dec-01 6:36
Remi Morin7-Dec-01 6:36 
GeneralRe: Dynamic Multidimensional Array or Alternatives Pin
Sprudling7-Dec-01 6:41
Sprudling7-Dec-01 6:41 
GeneralRe: Dynamic Multidimensional Array or Alternatives Pin
Malcolm McMahon6-Nov-01 4:08
Malcolm McMahon6-Nov-01 4:08 
GeneralStatic control question.... Pin
Rickard Andersson205-Nov-01 8:29
Rickard Andersson205-Nov-01 8:29 
GeneralRe: Static control question.... Pin
Carlos Antollini5-Nov-01 8:42
Carlos Antollini5-Nov-01 8:42 
GeneralRe: Static control question.... Pin
Rickard Andersson205-Nov-01 8:48
Rickard Andersson205-Nov-01 8:48 
GeneralRe: Static control question.... Pin
Carlos Antollini5-Nov-01 8:57
Carlos Antollini5-Nov-01 8:57 
GeneralRe: Static control question.... Pin
Remi Morin5-Nov-01 10:33
Remi Morin5-Nov-01 10:33 
GeneralRe: Static control question.... Pin
Carlos Antollini6-Nov-01 3:11
Carlos Antollini6-Nov-01 3:11 
GeneralCertificate validation for SSL on Pocket PC Pin
5-Nov-01 8:25
suss5-Nov-01 8:25 
GeneralActiveX Mouse Events Pin
Teletran15-Nov-01 7:39
Teletran15-Nov-01 7:39 
GeneralRegarding outlook bar on codeguru Pin
vgandhi5-Nov-01 7:31
vgandhi5-Nov-01 7:31 
GeneralDrag & Drop Pin
ananth_rs5-Nov-01 6:23
ananth_rs5-Nov-01 6:23 
GeneralRe: Drag & Drop Pin
vgandhi5-Nov-01 11:50
vgandhi5-Nov-01 11:50 
GeneralGDI+ Pin
AJ1235-Nov-01 6:17
AJ1235-Nov-01 6:17 
GeneralRe: GDI+ Pin
Michael Dunn5-Nov-01 6:56
sitebuilderMichael Dunn5-Nov-01 6:56 
GeneralWeird problem Pin
Peter Liddle5-Nov-01 6:00
Peter Liddle5-Nov-01 6:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.