Jump to content
  • Advertisement
Sign in to follow this  
johnnyBravo

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

This topic is 4892 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 = 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
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 thing
for (int i = 0; i < array_size; i++) {
delete num;
}
delete [] num

Hope I've cleared it up!

Share this post


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

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[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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!