Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make an array that holds an unsigned int?

Tags:

c++

I'm currently trying to make it where an array holds rgba data (0x00000000 → 0xFFFFFFFF) and when I put a value over 2,147,483,647 it overflows, I'm assuming because of some possible conversion (maybe unsigned → signed).

Here's my code:

int main(int argc, char* args[]) {
    uint32_t *background = new uint32_t[1920*1080];

    background[100] = 0xFF0000FF; //red, 4278190335
    printf("%d, ", background[100]);

    return 0;
}

Here's the output:

-16776961

I'm still somewhat new to C++ so if I'm being oblivious to something please point it out.

like image 649
Pixel Avatar asked Oct 19 '18 06:10

Pixel


People also ask

How is unsigned int stored?

An unsigned data type can only store positive values. It takes a size of 32 bits. A maximum integer value that can be stored in an unsigned int data type is typically 4, 294, 967, 295, around 232 – 1(but is compiler dependent).

Is it good practice for unsigned int?

The Google C++ style guide recommends avoiding unsigned integers except in situations that definitely require it (for example: file formats often store sizes in uint32_t or uint64_t -- no point in wasting a signedness bit that will never be used).

Can you cast int to unsigned?

You can convert an int to an unsigned int . The conversion is valid and well-defined. Since the value is negative, UINT_MAX + 1 is added to it so that the value is a valid unsigned quantity. (Technically, 2N is added to it, where N is the number of bits used to represent the unsigned type.)

How do you make an unsigned int signed?

To convert a signed integer to an unsigned integer, or to convert an unsigned integer to a signed integer you need only use a cast. For example: int a = 6; unsigned int b; int c; b = (unsigned int)a; c = (int)b; Actually in many cases you can dispense with the cast.


2 Answers

First, a quick note:

uint32_t *background = new uint32_t[1920*1080];

Here, background isn't an array (in the stack), rather, you are allocating memory (containing an array) and saving a pointer to the first element. You will need to delete the memory. In C++, it is much easier to use a std::vector:

// at the top: #include <vector>
std::vector<uint32_t> background(1920*1080);

Which will get deallocated automatically (so you don't have to worry about it). Another option is using an array, but in this case, you better not, because it is quite a lot of memory you have there (8 MiB), which may break your stack.

Now, if you want to use printf to print an unsigned int, you need to use %u (or %x if you want it in hexadecimal):

printf("%u, ", background[100]); // or...
printf("%x, ", background[100]);

However, in your code you are using uint32_t, which is a fixed-with type. For this, you would need to use:

// at the top: #include <cinttypes>
printf("%" PRIu32 ", ", background[100]); // or...
printf("%" PRIx32 ", ", background[100]); 

Further, a final note as @Someprogrammerdude commented, you can use std::cout in C++ instead:

// at the top: #include <iostream>
std::cout << background[100] << std::endl; // or...
std::cout << std::hex << background[100] << std::dec << std::endl;
like image 167
Acorn Avatar answered Sep 23 '22 15:09

Acorn


Change this:

printf("%d, ", background[100]);

to this:

// #include <cinttypes>
printf("%" PRIu32 "", background[100]);

since you want to print a uint32_t, not an int.

PS: Since this is C++, I strongly suggest using std::cout, which will take care automatically for these issues.

PPS: Since you used new [], don't forget to delete [] afterwards.

like image 33
gsamaras Avatar answered Sep 21 '22 15:09

gsamaras