4

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?

2
  • 1
    Use std::vector, don't manually manage resources. Commented Sep 27, 2010 at 17:55
  • 1
    What is it that you want to achieve? Statically initialize an array allocated dynamically? Commented Sep 27, 2010 at 18:18

6 Answers 6

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@Nemanja Trifunovic: dunno, ask OP, because it's him who wants declaration and initialization separate :)
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.
Thank you all for the answers. Actually what erjot suggested solved my problem. I needed to declare and initialize the variable in seperate lines.
4
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));

1 Comment

I’d recommend std::copy (and to basically never use the low-level) operatons, but this is otherwise what I’d do.
4

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.

2 Comments

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: 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.
2

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

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

Comments

2

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.