I know that the U literal means in c, that the value is a unsigned integer. An unsigned intagers size is 4 bytes.

But how big are 2U or 1024U? Does this simply mean 2 * 4 bytes = 8 bytes for example or does this notation means that 2 (or 1024) are unsigned integers?

My goal would be to figured out how much memory will be allocated if i call malloc like this

int *allocated_mem = malloc(2U * 1024U); 

and prove in a short program my answer what i tried like this

printf("Size of 2U: %ld\n", sizeof(2U)); printf("Size of 1024U: %ld\n", sizeof(1024U)); 

I would have expeted for the first line a size of 2 * 4 Bytes = 8 and for the second like 1024 * 4 Bytes = 4096 but the output is always "4".

Would realy appreciate what 2U and 1024U means exactly and how can i check their size in C?

8

2 Answers

My goal would be to figured out how much memory will be allocated if i call malloc like this int *allocated_mem = malloc(2U * 1024U);

What is difficult about 2 * 1024 == 2048? The fact that they are unsigned literals does not change their value.


An unsigned intagers size is 4 bytes. (sic)

You are correct. So 2U takes up 4-bytes, and 1024U takes up 4-bytes, because they are both unsigned integers.


I would have expeted for the first line a size of 2 * 4 Bytes = 8 and for the second like 1024 * 4 Bytes = 4096 but the output is always "4".

Why would the value change the size? The size depends only on the type. 2U is of type unsigned int, so it takes up 4-bytes; same as 50U, same as 1024U. They all take 4-bytes.

You are trying to multiply the value (2) times the size of the type. That makes no sense.

How big?

2U and 1024U are the same size, the size of an unsigned, commonly 32-bits or 4 "bytes". The size of a type is the same throughout a given platform - it does not change because of value.

"I know that the U literal means in c, that the value is a unsigned integer." --> OK, close enough so far.

"An unsigned integers size is 4 bytes.". Reasonable guess yet C requires that unsigned are at least 16-bits. Further, the U makes the constant unsigned, yet that could be unsigned, unsigned long, unsigned long long, depending on the value and platform.

Detail: in C, 2U is not a literal, but a constant. C has string literals and compound literals. The literals can have their address taken, but &2U is not valid C. Other languages call 2U a literal, and have their rules on how it can be used.

My goal would be to figured out how much memory will be allocated if i call malloc like this int *allocated_mem = malloc(2U * 1024U);

Instead, better to use size_t for sizing than unsigned and check the allocation.

size_t sz = 2U * 1024U; int *allocated_mem = malloc(sz); if (allocated_mem == NULL) allocated_mem = 0; printf("Allocation size %zu\n", allocated_mem); 

(Aside) Be careful with computed sizes. Do your size math using size_t types. 4U * 1024U * 1024U * 1024U could overflow unsigned math, yet may compute as desired with size_t.

size_t sz = (size_t)4 * 1024 * 1024 * 1024; 

The following attempts to print the size of the constants which is likely 32-bits or 4 "bytes" and not their values.

printf("Size of 1024U: %ld\n", sizeof(1024U)); printf("Size of 1024U: %ld\n", sizeof(2U)); 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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