sizeof(blank class) == 1. why? [duplicate] - c++

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C++: What is the size of an object of an empty class?
#include <iostream>
class C
{
};
int main()
{
std::cout << sizeof(C) << std::endl;
return 0;
}
output:
1
Why 1, but not zero ?

From Stroustrup's mouth sizeof. To ensure that the addresses of two different objects will be different. For the same reason, "new" always returns pointers to distinct objects.

Because the C++ standard requires all objects to have a nonzero size. This helps ensure that every object has a unique address.

The c++ standard says that every class/struct must have at least 1 byte.

Related

Pointer takes elements bigger than its size in c++ [duplicate]

This question already has answers here:
Accessing an array out of bounds gives no error, why?
(18 answers)
Closed 10 months ago.
When creating a pointer using new keyword or <stdlib.h>'s malloc, and put its size 0; what ever is the index number it doesnt give an error and works. ( i am not sure if this works if the size bigger then 0, because some times when i allocate a memory block and place an element outside its range program crashes).
My question : is this a c++ thing? Or just from my compiler? Is it safe to use it for arrays i don't want to limit it with specific size. Because i am thinking to use this in my game to make unlimited world generation and save it ( Not all terrain just the breakable and placable objects)
#include <iostream>
using namespace std;
int main() {
int* x = new int[0];
x[100] = 0;
cout << x[100];
return 0;
}
is this a c++ thing? Or just from my compiler?
It's a bug in your program. The behaviour of the program is undefined.
Is it safe to use it
It is not safe. The program is broken.

Why is there a difference in size of bytes between an array and a vector in C++? [duplicate]

This question already has answers here:
What is the size of sizeof(vector)? C++
(4 answers)
sizeof() a vector
(6 answers)
Closed 2 years ago.
I'm going through a beginner course on C++ and like to pause the lesson and tinker around with the code on what I'm learning. I had the idea to use the sizeof() operator in C++ on a vector and an array. Ended up finding out that with the same amount of numbers in the vector and array the size of them in bytes comes of different. Here is my code.
#include <iostream>
#include <vector>
int main() {
std::vector <int> test_scores { 100,99,90,70,75 };
int test_scores_array[]{ 100,99,90,70,75 };
std::cout << sizeof(test_scores) << std::endl;
std::cout << sizeof(test_scores_array) << std::endl;
return 0;
}
with an output of this
16
20
after about 30 minutes of googling I can't figure out why the size is different. I don't really "need" to know but I just find it really interesting that it is different and would really like to know why.

When does a non-dynamic variable goes out of scope if being referenced by a pointer? [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 2 years ago.
I'm new to C++ programming and I am having some hard time understanding some concepts.
Take this code as a example:
// Example program
#include <iostream>
class nber
{
int* value;
public:
nber(int n)
{
value = &n;
}
int getNber()
{
return *value;
}
};
int main()
{
nber var(111);
std::cout << "The number is:" << var.getNber() << "\n";
}
As you can see, the nber constructor receives an integer n and passes its address to the "value" pointer. What I expected is to have some kind of unwanted behavior, since the scope of the received integer (n) ends as soon as the constructor end, but the output is:
The number is:111
So the scope didn't end? If it really didn't end, when is the memory used to store the variable n going to be released? Thanks.
The scope did end. What you're seeing is Undefined Behavior - anything can happen. The number could be "purple", as far as the rules say. Or your hard disk could be erased. The latter is a bit rare, though.

zero an array inside a struct in c++ [duplicate]

This question already has answers here:
How come an array's address is equal to its value in C?
(6 answers)
Closed 7 years ago.
I have a struct defined in my program.
struct A{
int arr[10];
}
Lets say I have a pointer to it.
A * a = new A;
I can zero it in two ways:
memset(&a->arr,0,sizeof(A));
memset(a->arr,0,sizeof(A));
both work and look the same!
which one is more correct?
which one is more correct?
I'd argue neither. The easiest way would be to value initialize the allocated object:
A * a = new A();
Of course, this assumes that you actually have a good reason to new this object.
Since you are using C++ I would take advantage of C++11 features and use:
#include <iostream>
#include <cmath>
using namespace std;
struct A{
int arr[10]{}; // initializes array with all 0's
};
int main() {
A * a = new A;
for (auto e : a->arr) // ranged based for loop to show all of the 0's
cout << e << "\n";
return 0;
}
You can see it running with this Live Example
While the type of each expression is different, the actual result, the pointer you pass to memset, will be equal in both cases.
Personally I would probably use std::fill instead of memset in a C++ program:
std::fill(std::begin(a->arr), std::end(a->arr), 0);
Also note that if you have more members in the structure, sizeof(A) will be different from sizeof(a->arr) (or sizeof(A::arr)).
you can define a default construct function
struct A{
int arr[10];
A():arr(){}
};
This is the correct way for just the array
memset(a->arr,0,sizeof(a->arr))
Picked out the arr member just in case there are other structure members that do not need to be touched. Makes no difference in your example the following will do likewise
memset(a->arr,0,sizeof(A));

Array with size 0 [duplicate]

This question already has answers here:
What happens if I define a 0-size array in C/C++?
(8 answers)
Closed 1 year ago.
Today I incidentally defined a two dimensional array with the size of one dimension being 0, however my compiler did not complain. I found the following which states that this is legal, at least in the case of gcc:
6.17 Arrays of Length Zero
However, I have two questions on this usage:
First, is this considered as good programming practice? If so, then when should we use it in real world?
Second, the array I defined was two dimensional, with 0 size for one dimension. Is this the same as the one dimensional case? For example,
int s[0]
int s[0][100]
int s[100][0]
Are they all the same in the memory and for the compiler?
EDIT: Reply to Greg: The compiler I am using is gcc 4.4.5. My intention for this problem is not compiler-dependent, however if there are any compiler specific quirks that would be helpful too:)
Thanks in advance!
In C++ it is illegal to declare an array of zero length. As such it is not normally considered a good practice as you are tying your code to a particular compiler extension. Many uses of dynamically sized arrays are better replaced with a container class such as std::vector.
ISO/IEC 14882:2003 8.3.4/1:
If the constant-expression (5.19) is present, it shall be an integral constant expression and its value shall be greater than zero.
However, you can dynamically allocate an array of zero length with new[].
ISO/IEC 14882:2003 5.3.4/6:
The expression in a direct-new-declarator shall have integral or enumeration type (3.9.1) with a non-negative value.
I ran this program at ideone.com
#include <iostream>
int main()
{
int a[0];
int b[0][100];
int c[100][0];
std::cout << "sizeof(a) = " << sizeof(a) << std::endl;
std::cout << "sizeof(b) = " << sizeof(b) << std::endl;
std::cout << "sizeof(c) = " << sizeof(c) << std::endl;
return 0;
}
It gave the size of all the variables as 0.
sizeof(a) = 0
sizeof(b) = 0
sizeof(c) = 0
So in the above example, no memory is allocated for a, b or c.
Compiling your example with gcc, all three of them have sizeof 0, so I would assume that all of them are treated equally by the compiler.
Your link explains everything. They are used as last field in a struct when the length of struct is not known at compile time. If you try using them on stack or in a middle of other declarations you will end up overwriting next elements.