when I try to allocate memory in c++ it allocates 8 bytes by default I expected 4;
#include<iostream>
using namespace std;
int main(){
int* arr = new int;
cout << "arr " << sizeof(arr)<<endl;
cout << "int16 " << sizeof(int16_t)<<endl;
cout << "int32 " << sizeof(int32_t)<<endl;
cout << "int64 " << sizeof(int64_t)<<endl;
}
this is a result:
arr 8
int16 2
int32 4
int64 8
sizeof(arr) is the size of the int*. You're most likely compiling/executing for a 64bit architecture, so pointers are 8 bytes in size.
Dereferencing like sizeof(*arr) prints the size of int which is 4 as expected
Related
Here is my code:
#include <iostream>
using namespace std;
class MyClass{
int mem1;
float mem2;
};
class MyKids: public virtual MyClass{
int cmem1;
int cmem2;
};
class MyLawKids:public virtual MyClass{
int lmem1;
};
class MyGrands:public MyKids, public MyLawKids{
int gmem1;
};
int main(){
cout << "\n Size of MyClass: " << sizeof(MyClass) << " bytes" << endl ;
cout << " Size of MyKids: " << sizeof(MyKids) << " bytes" << endl ;
cout << " Size of MyLawKids: " << sizeof(MyLawKids) << " bytes" << endl ;
cout << " Size of MyGrands: " << sizeof(MyGrands) << " bytes" << endl ;
return 0;
}
Result:
Size of MyClass: 8 bytes
Size of MyKids: 20 bytes
Size of MyLawKids: 16 bytes
Size of MyGrands: 32 bytes
Process returned 0 (0x0) execution time : 0.094 s
Press any key to continue.
I think the size of MyGrands class its should be 36 bytes = (sizeof(MyKids + MyLawKids + int gmem1) - 4 bytes of virtual table pointer).
Why my result shown me "Size of MyGrands: 32 bytes" ?
MyGrands contains only one instance of MyClass due to MyKids and MyLawKids inheriting virtually from MyClass. Hence your calculation is wrong, by double counting the size of MyClass. By the way, in general you should not rely on a naive addition of the pieces being the same as the size of the whole. The compiler can add padding.
So while studying for my exams I was trying to do a practice problem for pointers.
In the following code I'm trying to display the number of elements before the first occurrence of 0.
There is only one part that i didn't understand please see the 6th last line.
#include <iostream>
using namespace std;
int main()
{
int A[10];
for (int i = 0; i < 10; i++){
cout << "Please enter number " << i + 1 << " in the array: ";
cin >> A[i];
}
int *Aptr = A;
while(*Aptr !=0){
cout << *Aptr << "";
Aptr++;
}
cout << "\nThere are " << (Aptr - A) //Here is what i don't understand.
<< " numbers before the first occurrence of 0." << endl;
system("pause");
return 0;
}
So why exactly is (Aptr - A) giving me the number of elements instead of a memory location, and why is this even doable since Aptr is a pointer and A is an array?
Can someone explain to me in detail?
When used in an expression, like Aptr - A, the name of an array A will be implicitly converted to a pointer (equal to &A[0]).
Then the compiler is faced with subtracting two pointers of the same type (both of type int * in your case). That is specified as giving a value of type std::ptrdiff_t, which is, in turn "a signed integral type able to represent the result of subtracting two pointers".
Pointer arithmetic, when subtracting two pointers of type int (i.e. two int *s) gives the number of ints between the two pointers (assuming they are in the same object, which is true in this case, since Aptr points at an element of the array A).
Practically, if Aptr is equal to &A[i], the subtraction Aptr - &A[0] gives a std::ptrdiff_t equal to i.
Note: there is another problem in your code, as since the first (for) loop reads 10 values, while the second while loop keeps incrementing Aptr until it points at an int with value 0. If the user enters any zero values, the second loop will stop when it finds the first (even if the user enters non-zero elements after that). If the user enters no values equal to 0, then the while loop has undefined behaviour, since Aptr will keep walking through memory past the end of A until it happens to find memory that compares (as an int) equal to 0.
First of all, name of array A is associated to address of (pointer at) the first item in the array.
So why exactly is (Aptr - A) giving me the number of elements?
Because according to rules address arithmetic subtraction operation (also +, and similar) is performed based on the data type.
I mean, that compiler operating with int* makes ++, --, addition, subtraction an integer, etc. adds addresses needed for shifting to next/previous item.
If you really want to see how many bytes are located between addresses, just convert addresses to int before making subtraction:
cout << endl << "Address difference is " << int(Aptr) - int(A) << endl;
You can try that with different data types as follows:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int A[5];
short B[5];
unsigned char C[5];
cout << "Array (data type) | Syze of array | Size of item | Item distanse | Bytes distance" << endl;
cout << "A (int) :" << setw(10)
<< sizeof(A) << setw(15)
<< sizeof(A[0]) << setw(15)
<< &A[4] - A << setw(15)
<< int(&A[4]) - int(A) << endl;
cout << "B (short) :" << setw(10)
<< sizeof(B) << setw(15)
<< sizeof(B[0]) << setw(15)
<< &B[4] - B << setw(15)
<< int(&B[4]) - int(B) << endl;
cout << "C (un.char) :" << setw(10)
<< sizeof(C) << setw(15)
<< sizeof(C[0]) << setw(15)
<< &C[4] - C << setw(15)
<< int(&C[4]) - int(C) << endl;
system("pause");
return 0;
}
UPDATE
To be better prepared for your exam, consider the following example with pointers:
#include <iostream>
using namespace std;
int main()
{
int A[5] = {0}; // all items now are 0
int * P = A + 2; // the same as P = &A[2];
*P = 33; // writing to item A[2];
cout << A[2] << endl; // just to check in usual way
cout << *(A + 2) << endl; // using A as a pointer
cout << *(2 + A) << endl; // almost the same to previous
cout << 2[A] << endl; // quite strange, but it works
cout << 0[P] << endl; // and this is the same
return 0;
}
You must understand that 0[P] means for compiler *(0 + P), as well as 2[A] means - *(2 + A), but you should not write in your program in such style (exceptions are only cases when you want to confuse a reader).
And one more important thing - difference between array and pointer - are shown in the following example:
int A[] = {1, 2, 3, 4, 5};
int *P = A;
cout << "A = " << A << endl;
cout << "P = " << P << endl;
cout << "size of A = " << sizeof(A) << endl;
cout << "size of P = " << sizeof(P) << endl;
even if the addresses (vaules A and P) are equal, compiler works with array (A) in a different way than with pointer: sizeof(A) means memory allocated for whole array (5 items of sizeof(int) each), but sizeof(P) means memory allocated for data type int * (pointer to int). So, sizeof(P) depends only on compiler and OS platform (e.g. pointer can be 32-bit or 64-bit), but sizeof(A) depends on size of item (int may be not 32 bits) and NUMBER OF ITEMS in the array.
And you can "go to the next item" with pointer:
P++;
cout << *P << endl;
but you are not able to do:
A++;
because A is not variable of pointer type (it is just similar in sense of "address of the first item"), and compiler will say you something like:
error : '++' needs l-value
So here is my code:
cout << "The size of an integer is " << sizeof(int) << endl;
cout << "The size of a double is " << sizeof(double) << endl;
cout << "The size of a string is " << sizeof(string) << endl;
int num = 1234;
cout << "The size of a num is " << sizeof(num) << endl;
int *pnum = #
cout << "The size of a pointer pnum is " << sizeof(pnum) << endl;
cout << "The size of a value pointed at by pnum is " << sizeof(*pnum) << endl;
return 0;
Im confused by this chunk of code because output of this is:
The size of an integer is 4
The size of a double is 8
The size of a string is 40
The size of a num is 4
The size of a pointer pnum is 8
The size of a value pointed at by pnum is 4
I think size of a pointer pnum should be the same like value pointed at by pnum because Im watching a tutorial "Programming C++" and in this tutorial they are the same. Can someone explain why they arent the same in my program?
sizeof(pnum) is exactly the same as sizeof(int*), number of bytes needed to represent a pointer to the int value. Such pointer takes 64 bits at 64-bite system, which is 8 bytes as shown in your code output.
At the same time, sizeof(*pnum) is exactly the same as sizeof(int), since *pnum has type int. This type usualy is 32-bits long, which gives us 4 bytes.
and in this tutorial they are the same:
the reason could be that for that specific machine (like my own 32 bit machine) sizeof(int) = sizeof(int*) = 4.
The reason you get different results is that *pnum is the pointee of pnum which is an int (4 bytes on your machine) and pnum itself is a pointer (8 bytes on your machine)
I wrote the following code:
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
char c;
int i;
short int j;
long int k;
float f;
double d;
long double e;
cout << "The size of char is: " << sizeof c << endl;
cout << "The size of int is: " << sizeof i << endl;
cout << "The size of short int is: " << sizeof j << endl;
cout << "The size of long int is: " << sizeof k << endl;
cout << "The size of float is: " << sizeof f << endl;
cout << "The size of double is: " << sizeof d << endl;
cout << "The size of long double is: " << sizeof e << endl;
system("pause");
return 0;
}
The purpose of this program is to print out the size of the fundamental data types, which I think I have accomplished. The other purpose of this program is to print the size of the pointer to each of these data types. I'm having a hard time figuring out how to do this. I understand that a pointer is a variable which stores the address of another variable and that pointers involve the deference operator (*). Can anyone please provide a suggestion? I'm not looking for the answer, just a nudge in the right direction.
int *p; // p is a pointer to an int
So sizeof the pointer would be: sizeof p, which you could print as:
cout << "The size of int pointer is: " << sizeof p << endl;
This is what you need to do print other pointers' sizes.
Dereferencing is only done when you want to access what a pointer is pointing to.
E.g.
int i = 5;
int *p = &i;
*p = 6;
*p = *p + 1;
//etc
Here, you just want to get the size of the pointers. So no dereferencing is needed.
int a[5];
cout << &a[1] << " " << &a[0] << endl;
cout << (&a[1] - &a[0]);
In the above code, why is &a[1] - &a[0] equal to 1 and not 4? Shouldn't there be 4 bytes between these addresses since we have an int array?
No, pointer difference is in elements, not in bytes.
Pointers are incremented by the size of there type. Reason is because you want to point to the next item. So taking you example further.
int a[5];
int *ptr=&a[0];
// ptr is now pointing at first element.
ptr+3; // now its pointing at 3rd element.
To get it in bytes: (see it live https://ideone.com/CrL4z)
int a[5];
cout << (a+1) << " " << (a+0) << endl;
cout << (reinterpret_cast<char*>(a+1) - reinterpret_cast<char*>(a+0));