sizeof Questions

2

Solved

Suppose there are classes: struct A { int a; virtual size_t GetMemoryUsage() const { return sizeof(*this); } }; struct B : public A { int b; }; And there may be deeper inheritance. What ...
Thimbu asked 11/5, 2018 at 14:48

2

Solved

I'm working on some simple bit manipulation problems in C++, and came across this while trying to visualize my steps. I understand that the number of bits assigned to different primitive types may ...
Marylandmarylee asked 23/4, 2018 at 3:59

2

Solved

Is there any way to get the size (in bytes) of the data stored by std::any? The only workaround I came up with is querying the type of its value by std::any::type and comparing the result to a list...
Goings asked 9/4, 2018 at 10:24

5

Solved

I found the following template on a blog: template <typename T, size_t N> struct array_info<T[N]> { typedef T type; enum { size = N }; }; It is an elegant alternative to sizeof(a) ...
Danelledanete asked 24/11, 2011 at 13:30

2

Solved

There is a topic already on this topic but I have doubts still. To calculate the size of a vector, which one is correct: sizeof(VEC) + sizeof(int) * VEC.capacity() or VEC.capacity() * (sizeof(V...
Benzoate asked 8/1, 2012 at 13:52

0

I get what appears to be a Visual Studio bug when I compile time test if a type with a default template parameter has some property. In the minimal example below I use std::is_integer. When compil...
Freida asked 27/2, 2018 at 7:54

4

Solved

According to cppreference: If the type of expression is a variable-length array type, expression is evaluated and the size of the array it evaluates to is calculated at run time. It means: ...
Endorsee asked 7/2, 2018 at 10:10

1

Solved

I have a program that uses dynamic allocation for a uint8_t array; can I safely assume that its length will always be one byte?
Gatekeeper asked 7/2, 2018 at 3:3

2

Solved

I am trying to read one short and long from a binary file using python struct. But the print(struct.calcsize("hl")) # o/p 16 which is wrong, It should have been 2 bytes for short and 8 bytes ...
Intersection asked 24/1, 2018 at 14:57

2

Solved

I am very new in C and was wondering about how to get each element of an array using a pointer. Which is easy if and only if you know the size of the array. So let the code be: #include <stdio...
Town asked 21/1, 2018 at 12:58

1

Solved

I tend to think I have a pretty good grasp of C++ internals and memory layouts, but this one has me baffled. I have the following test code: #include <stdio.h> struct Foo { //Foo() {...
Roots asked 20/12, 2017 at 21:29

4

Solved

There is an old post asking for a construct for which sizeof would return 0. There are some high score answers from high reputation users saying that by the standard no type or variable can have si...
Chili asked 17/11, 2017 at 14:14

3

Solved

I have a template that takes a struct with different values, for example: struct Something { char str[10]; int value; ... ... }; And inside the function I use the sizeof operator: jump in me...
Graecoroman asked 14/2, 2011 at 15:0

3

In the following code, are the functions test and test2 equivalent? typedef int rofl; void test(void) { rofl * rofl = malloc(sizeof(rofl)); // Is the final rofl here the TYPE? } void test2(void...
Swinge asked 15/11, 2017 at 1:15

3

Solved

I have a some cases about sizeof operator, which is used with function types. Case 1 : [Live Demo] #include <stdio.h> char func() { printf("sizeof func = %zu\n", sizeof(func())); //Ok. Ou...
Dextroglucose asked 10/11, 2017 at 13:18

7

Solved

I have a small piece of code about the sizeof operator with the ternary operator: #include <stdio.h> #include <stdbool.h> int main() { bool a = true; printf("%zu\n", sizeof(bool)); ...
Untruth asked 30/10, 2017 at 8:37

4

Solved

#include <stdio.h> int main(void) { printf("sizeof(char) = %zu\n", sizeof(char)); printf("sizeof('a') = %zu\n", sizeof('a')); } See https://godbolt.org/z/1eThqvMh...
Stole asked 31/1, 2010 at 19:15

4

Solved

I wrote the code about sizeof operator. If I write something like: #include <stdio.h> int main() { char a[20]; printf("%zu\n", sizeof(a)); return 0; } Output: 20 // Ok, it's fin...
Cymric asked 18/10, 2017 at 9:38

4

printf("%lu \n", sizeof(*"327")); I always thought that size of a pointer was 8 bytes on a 64 bit system but this call keeps returning 1. Can someone provide an explanation?
Ponderable asked 12/10, 2017 at 8:50

4

Solved

Why does this code compile? _Static uint32_t my_arr[2]; _Static_assert(sizeof(my_arr) == 8, ""); _Static_assert(sizeof(my_arr[0]) == 4, ""); _Static_assert(sizeof(my_arr)[0] == 4, ""); The first...
Kudu asked 9/10, 2017 at 19:9

9

Solved

I am trying to declare a struct that is dependent upon another struct. I want to use sizeof to be safe/pedantic. typedef struct _parent { float calc ; char text[255] ; int used ; } parent_t ; ...
Murielmurielle asked 24/8, 2010 at 3:6

2

I had always assumed that the size of an array of N elements of type T, as returned by sizeof was guaranteed to be exactly N times sizeof(T). The comments on this question made me doubt it though...
Folia asked 27/9, 2017 at 22:48

2

Solved

I have a struct defined in .h struct buf_stats { // *** }; then in .c file struct buf_stats *bs = malloc(sizeof(struct buf_states*)) ; where buf_states is a typo. but gcc does not warn me, ...
Stevenage asked 5/9, 2017 at 7:45

2

Solved

The sizeof char, int, long double... can vary from one compiler to another. But do I have the guarantee according to the C++11 or C11 standard that the size of any signed and unsigned fundamental i...
Tomikotomkiel asked 1/11, 2012 at 0:42

3

Solved

Different outputs of sizeof() operator in C and C++. In C: int main() { printf("%zu\n", sizeof(1 == 1)); return 0; } output: 4 In C++: int main() { std::cout << sizeof(1 == 1) &l...
Consociate asked 11/8, 2017 at 11:46

© 2022 - 2024 — McMap. All rights reserved.