How to create a pointer, two dimensional array in c++?

Started by
4 comments, last by MaulingMonkey 18 years, 11 months ago
Hi, with c++, I need to make a two dimensional array of a certain size to be defined, during run time, so I am going to use pointers(and not vectors). eg float **num; basically my problem is when I do: num = new float[2]; I get an error bout not be able to convert a float* or something like that. (then I would do something like: for(int i=0;i<2;i++) num = new float[3];) I'm pretty sure I've done this before, but for some reason I can't remember what to do. THanks oh and btw, when deleting the array do i have to loop through each of the first array 'num[]' deleting their memory, and then just one delete after that on the 'num' thx
Advertisement
You're almost there. What you need to do is initialize first an array of pointers and then each element seperately. You're right about delete. Here's a snipet:
float **num = NULL;const int array_size = 12;num = new float*[array_size];for (int i = 0; i < array_size; i++) {  num = new float;}// do your thingfor (int i = 0; i < array_size; i++) {  delete num;}delete [] num

Hope I've cleared it up!
The first allocation allocates an array with pointers. So you need to do this:

num = new float *[2];

[edit]And I replied too late. =)[/edit]
Quote:Original post by johnnyBravo
Hi, with c++, I need to make a two dimensional array of a certain size to be defined, during run time, so I am going to use pointers(and not vectors).
Vectors can be resized during run time. (std::vector that is)
Quote:num = new float[2];
You shouldn't allocate float array, you should allocate an array of pointers* to floats. num = new float*[2];
Quote:oh and btw, when deleting the array do i have to loop through each of the first array 'num[]' deleting their memory, and then just one delete after that on the 'num' thx
Yes.
If the number of elements of one of the dimensions is known at compile time then use a pointer to an array instead, you'll have more contiguous of elements in other words its more efficient:

#include <cstddef> // std::size_t#include <iostream>int main() {   const std::size_t N = 30; // constant, is known at compile-time   int (*foo)[N]; // pointer to an array of integers   std::size_t M = 0;   std::cout << "enter size: ";   std::cin >> M;	   foo = new int[M][N];   delete[] foo;}
Boost.MultiDimensional Array Library

Example:

#include "boost/multi_array.hpp"#include <cassert>int main () {  // Create a 3D array that is 3 x 4 x 2  typedef boost::multi_array<double, 3> array_type;  typedef array_type::index index;  array_type A(boost::extents[3][4][2]);  // Assign values to the elements  int values = 0;  for(index i = 0; i != 3; ++i)     for(index j = 0; j != 4; ++j)      for(index k = 0; k != 2; ++k)        A[j][k] = values++;  // Verify values  int verify = 0;  for(index i = 0; i != 3; ++i)     for(index j = 0; j != 4; ++j)      for(index k = 0; k != 2; ++k)        assert(A[j][k] == verify++);  return 0;}

This topic is closed to new replies.

Advertisement