views:

135

answers:

5

I want to initialize an array and then initialize a pointer to that array.

int *pointer;
pointer = new int[4] = {3,4,2,6};
delete[] pointer;
pointer = new int[4] = {2,7,3,8};
delete[] pointer;

How can I do this?

+2  A: 

Why not use

int array[4] = {3, 4, 2, 6};

Is there a reason you want to allocate memory for the array from heap?

Suggestion after comment:

int arrays[32][4] = {{3, 4, 2, 6}, {3, 4, 1, 2}, ...}
int *pointers[4];
pointers[0] = arrays[0];
pointers[1] = arrays[12];
pointers[2] = arrays[25];
pointers[3] = arrays[13];
...
pointers[0] = arrays[13];
pointers[1] = arrays[11];
pointers[2] = arrays[21];
pointers[3] = arrays[6];
Tuomas Pelkonen
Well I'm writing a function that will have 32 arrays in it, basically the same 4 arrays but with different members. I wanted to somehow keep it to 4 arrays and re-initialize them at 8 different points in the function.
Alex
You could just re-use those four arrays by overwriting them with the new numbers needed.
sbi
What do you mean by "overwriting them"?
Alex
The suggestion after comment would work, but won't that use a lot of memory the entire duration of the function? I was hoping to spread it out so the memory foot print would be lower. It's the best solution so far though.
Alex
Yes it uses more memory, but I wouldn't say that it uses a lot of memory :) What kind of environment do you have?
Tuomas Pelkonen
Its not an environment restriction, I am just attempting to write the best code I can. I still have much to learn.
Alex
Well in almost any situation, reserving 32 * 4 * 4 bytes from stack is totally acceptable.
Tuomas Pelkonen
@Alex: I'd say, first try to write correct, easily readable code. Then optimize.
sbi
+1  A: 
int *pointer = new int[4]{3,4,2,6};

EDIT: As pointed out in the comments, this is C++0x syntax. To do this in earlier versions, write a function that takes a stack array + size, allocates a new array on the heap, loops through the stack array populating the heap array, and then returning a pointer to the heap array.

int* foo( const int size, int *array )
{
   int *newArray = new int[size];
   for( int index = 0; index < size; ++index )
   {
      newArray[index] = array[index];
   }

   return newArray;
}

The call would look like this:

int a[] = { 1, 2, 3, 4 };
int *ptr = foo( 4, a );

It takes two lines, but it at least is easier than initializing line by line.

j0rd4n
This is C++0x syntax.
Johannes Schaub - litb
This doesn't seem to work for me on the g++ compiler.
Alex
Yeah, my bad. I thought this would work pre-C++0x.
j0rd4n
A: 
//initialize the array
int array[] = {3,4,2,6};
// initialize a pointer to that array
int *pointer = array;
Eugen Constantin Dinca
The problem with this approach is that *pointer can ONLY be used in this scope because it points to a stack variable that will be removed from the stack once scope exits.
j0rd4n
Making sure your pointer points to valid data before using using it is kind of obvious, isn't it?One can definitely have pointer point to any of 'the same 4 arrays but with different members' (as Alex needs).
Eugen Constantin Dinca
A: 

As others have pointed out, you can initialize non heap arrays, e.g.:

 static const int ar1[4] = { ... };
 static const int ar2[4] = { ... };

Then initialize your dynamically allocated array from the static data:

void func()
{
    int *pointer = new int[4];
    ...
    memcpy(pointer, ar1, sizeof(ar1));
    ...
    memcpy(pointer, ar2, sizeof(ar2));
    ...
Richard Pennington
A: 

You can do something like this with a standard container and boost::assign.


std::vector vect = list_of(3)(4)(2)(6);

...

vect = list_of(2)(7)(3)(8);
Noah Roberts