tags:

views:

194

answers:

6

Hello,

I want to use something like this:

char theArray[]=new char[8];
theArray= { 1,2,3,4,5,6,7,8};

instead of

char theArray[] = { 1,2,3,4,5,6,7,8};

Is a similar thing possible?

+5  A: 

C++0x

char* ch;
ch = new char[8]{1, 2, 3, 4, 5, 6, 7, 8};

@David Thornley: then switch these lines and there is no problem. And seriously you're talking about reallocating char[8] in the same memory pool as the previous value, then you need to play with own allocators, something like:

char* ch1 = new char[8]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'i'};
char* ch2 = new(ch1) char[8]{1, 2, 3, 4, 5, 6, 7, 8};

afaik OP is not likely to need that.

erjot
Why not in a single line?
Nemanja Trifunovic
@Nemanja Trifunovic: dunno, ask OP, because it's him who wants declaration and initialization separate :)
erjot
This doesn't address the exact question, since the `new` is on the first line in the OP's example and the second in yours.
David Thornley
Thank you all for the answers. Actually what erjot suggested solved my problem. I needed to declare and initialize the variable in seperate lines.
+2  A: 

This is terrible, I know, but will also work in this scenario:

memcpy( theArray, "\x01\x02\x03\x04\x05\x06\x07\x08", sizeof(theArray) );
adamk
+4  A: 
char theArrayCopy[] = { 1,2,3,4,5,6,7,8}; 
//
//
char* theArray=new char[sizeof(theArrayCopy)]; 
// or
char theArray[sizeof(theArrayCopy)]; 

memcpy(theArray, theArrayCopy, sizeof(theArrayCopy));
James Curran
I’d recommend `std::copy` (and to basically never use the low-level) operatons, but this is otherwise what I’d do.
Konrad Rudolph
A: 

Note: I know this isn't the answer to the original question, but I think it's the right thing to do, so I'm putting it here as a CW post...

You could generalize it a bit too if you want...

class IntegerSequenceGenerator
{
    int step_;
    int value_;
public:
    IntegerSequenceGenerator(int start = 0, int step = 1)
        : step_(step), value_(start) {}
    int operator()() {
        int result = value_ + step_;
        std::swap(result, value_);
        return result;
    }
};

Use:

int main() {
    std::vector<int> myArray;
    std::generate_n(std::back_inserter(myArray), 8, IntegerSequenceGenerator());
}

Or:

int main() {
    std::vector<int> myArray(8);
    std::generate(myArray.begin(), myArray.end(), IntegerSequenceGenerator());
}

or:

int main() {
    int myArray[8];
    std::generate(myArray, myArray + (sizeof(myArray)/sizeof(int)), IntegerSequenceGenerator());
}
Billy ONeal
+3  A: 

One possibility you might want to consider is using Boost Assign, where you could use something like this:

std::vector<int> v;

v += 1, 2, 3, 4, 5, 6, 7, 8;

This won't work with an actual array though -- while it doesn't necessarily have to be a vector, it does require some sort of collection class, not a built-in type. OTOH, absent some extremely specific reason to do otherwise, you probably want to use a collection anyway.

Jerry Coffin
With an actual array, the program will generate the values from 1 to 8, then add 8 to the starting address of the array, and do nothing. If you've got optimization turned on, though, it'll probably just skip it entirely, because it does nothing. The only possible way this can work with vectors is operator overloading, which requires an actual class, not a basic type or array.
David Thornley
@David: Yes -- I hadn't actually thought things through to be sure, but now that you mention it, I should have known better and just said it won't work. I should remember that an overloaded operator requires a UDT as at least one operand.
Jerry Coffin
A: 

There is a problem that the other answers don't address. (Although several do solve it.)

This places an array of type char[8] on the stack:

char theArray[] = { 1,2,3,4,5,6,7,8};

This places it on the heap (actually it is illegal, but I'll assume your compiler accepts it):

char theArray[]=new char[8];

Supposing you don't really want it on the heap, but just want to initialize it, this will work:

char theArray[max_array_size]; // need to specify size to place on stack

/* code */

static const char theInitializer[] = { 1,2,3,4,5,6,7,8};
std::copy( theInitializer, theInitializer + sizeof theInitializer, theArray );
Potatoswatter