开发者

Initialize already declared char array in C++

I want to use something like this:

char theArray[]=new char[8];
theArray= { 1,2,3,4,5,6,7,8};
开发者_JAVA百科

instead of

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

Is a similar thing possible?


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.


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));


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.


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

memcpy( theArray, "\x01\x02\x03\x04\x05\x06\x07\x08", sizeof(theArray) );


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 );


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());
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜