0

Can someone please tell me if there is anything incorrect with this initialization:

static unsigned char* var[1]   
var[0] = new unsigned char[ 800 * 600 ]

Is this creating a 2D array at var[0] ? Is this even valid ?

3
  • 1
    is the compiler giving any error? Commented Jun 1, 2012 at 17:44
  • Hi betabandido, sorry about that. I've edited my post now. Well, this was just some old code I was going through and couldn't understand what was going on. The compiler doesn't give any error. Commented Jun 1, 2012 at 17:46
  • Note that the type of var and the type of var[0] are not the same thing. Commented Jun 1, 2012 at 17:51

5 Answers 5

3

It's creating a single array with 480,000 elements (aka 800*600) and storing it in var[0]. It is not creating a 2D array. It's valid as long as var[0] is an unsigned char*.

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

5 Comments

Does that mean, it is over-writing var[1], var[2] etc.?
No -- if var[0] is an unsigned char* then var must be an unsigned char** or a std::vector<unsigned char*> or some other such type. Assigning to var[0] will not affect var[1] or any other element of var.
As var only has one element, var[0] is all there is!
@c0d3rz var[1] does not exist since your array contains only one element.
Thanks Edward for the reply. It gives me a lot more understanding. I'm relatively new to C++. Sorry for re-editing the post. I wish I could accept more than one correct answer ...
1

It is creating a 1-d array of length (800*600) at var[0]. You'll be able to access its elements with:

var[0][0]
var[0][1]
...
var[0][800*600-1]

Comments

1

I think you want something more like (although I may be interrupting the question wrong);

static unsigned char* var = new char*[800]

for (int i =0; i < 800; i++)
     var[i] = new char[600]

That will give you a 800x600 2d character array.

Comments

1

Your code is not correct, since var[0] is not a pointer anymore.

You may want to do something like:

static unsigned char* var;
var = new unsigned char[800 * 600];

That does not create a 2D array. It is just a 1D array. Having said that, you can use it as a 2D array if you compute the offset yourself. For instance to access position (row, col) you could do:

var[row * 600 + col]

If you really want a "true" 2D array, you will need to use for instance:

static unsigned char** var;
var = new unsigned char*[600];
for (int i = 0; i < 600; i++)
    var[i] = new unsigned char[800];

Of course, if you do not need to dynamically specify the array size at runtime, you can just use a statically allocated array:

static unsigned char var[600][800];

BTW, I assume 600 is the number of rows and 800 the number of cols (like in a matrix). That is why I always use 600 in the first index. Otherwise, just swap the values.

4 Comments

var[0] is of type unsigned char*.
@K-ballo the OP changed the code. Before it was just unsigned char* var;
Or you could do unsigned char var[600][800]; and save yourself a lot of trouble.
@BoPersson That's true, but I guess the OP would not use dynamic allocation if his/her problem could be solved with a statically allocated array.
1

Yes, it's quite incorrect to use new[]. What you're looking for is std::vector<unsigned int> var(800 * 600);.

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.