Sign in to follow this  
johnnyBravo

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

Recommended Posts

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[i] = 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

Share this post


Link to post
Share on other sites
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[i] = new float;
}

// do your thing
for (int i = 0; i < array_size; i++) {
delete num[i];
}
delete [] num

Hope I've cleared it up!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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;

}

Share this post


Link to post
Share on other sites
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[i][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[i][j][k] == verify++);

return 0;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this