2,167 questions
-1 votes
3 answers
123 views
sizeof operator appears to return wrong value for c type packed struct [duplicate]
Given the following C struct: struct __attribute__((__packed__)) XIRControl { uint8_t command; char code[19]; uint16_t error; union codeData { struct rcvCommands { ...
15 votes
4 answers
1k views
What is the size of std::array<T,0>?
Unlike C array, std::array is allowed to have zero length. But the current implementations differ in how std::array<T,0> is implemented, in particular the sizes of the object are different. This ...
0 votes
3 answers
265 views
What to use when malloc array length in c
When the array type is a "void *", then it can be allocated by: void **void_array; void_array = malloc(sizeof(void*) * 1); But, if the array type is a struct Hello *, then what should be ...
3 votes
4 answers
324 views
Is sizeof(pointer) the same as processor's native word size?
Say, is sizeof(void*) the same as the size processor can atomically access per instruction? For example, 32-bit processor can read aligned 4 bytes atomically, 64-bit processor can read aligned 8 bytes ...
0 votes
1 answer
108 views
Conditional compilation depending on the size of data type [duplicate]
This does not answer my need. I do not want to generate an error, I want to adapt the behavior of the software depending on the condition. Example: #if ( sizeof(unsigned long) == 4 ) # define a 5 #...
1 vote
1 answer
221 views
Suspicious comparison of 'sizeof(expr)' to a constant
I did a static_assert to make sure that a valid type was used. I don't know if I just should have checked if the type is void, or whether any type can have size zero (I think it can with [[...
3 votes
1 answer
134 views
Any way to create a SizeOf file in the Windows style as seen in the General tab of Properties, e.g., Size: 1.76 MB (1,856,000 bytes)?
I want to create a function like SizeOf() of C++ for powershell, but apparently Microsoft uses some strange algorithm/formula to determine the mebibytes. Example: If you create file with exactly 1.856....
1 vote
4 answers
192 views
Why does \0 not affect the length of a string in C?
Consider this example where I add some extra \0 to a string. #include <stdio.h> #include <string.h> int main(int argc, char **argv){ char str1[] = "dog"; char str2[] = &...
0 votes
0 answers
57 views
Unpredictive array size [duplicate]
I declared 2 arrays with the first one of size 6 and tried to concatenate the arrays using strcat(), then checked the number of elements in the array but the array size is still showing 6 and still it ...
3 votes
2 answers
197 views
Sizeof vs array size [closed]
I've got a question regarding the sizeof() operator. Browsing Open Source projects written in C obviously, some developers tend to use the sizeof() operator in one place and on other places the ...
-6 votes
1 answer
168 views
size of a struct in Go without initialising a variable of struct
There is one huge struct in our code, and its being passed around everywhere as a value. I would like to be able to know the size(amount of memory in bytes, go will allocate for a variable of this ...
4 votes
5 answers
236 views
Declare array based on size of another array
I have an array of color codes, the size of which is known at compile time. I want to declare another array of the same size. But the code below throws an error. I can, of course, declare the size ...
3 votes
1 answer
171 views
What is the effect of sizeof for hypothetically oversized objects?
Suppose we have the source: #include <stdint.h> #include <stdio.h> struct foo { char b ; char a [ SIZE_MAX ] ; } ; int main ( void ) { const size_t z = sizeof ( struct foo ) ; ...
2 votes
0 answers
120 views
Sizeof equality of direct members vs inherited members
Does the C++ standard have anything to say about the size equality of the following? struct AB { int a; int b; } Or struct A { int a }; struct B : public A { int b; } Is sizeof(...
4 votes
6 answers
274 views
Function returning 101 and -101 instead of 1 and -1
So my professor gave us the assignment to code a function that works exactly like strcmp() but without using any library functions. I came up with the following code but instead of returning 1 or -1 ...