Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why is my sizeof operator returning the value 8 when it should be returning the value 40. My array is of type double therefore, to my understanding, each element of the array should be 8bytes. And, with 5 elements in my array, the sizeof operator should be returning 40bytes in total. However, when I check the value returned from the entire array, I only get 8 bytes which is the value of only a single element. I forced the array length in my code below to 5 to ensure that my array did infact have all five elements, and it did. I am baffled. Why are there five elements in my array but, only 8 bytes being returned by my sizeof operator. My code is below:

#include<iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
	int exit = 1;
	int count = 1;
	int arrayValueLength = 5;
	double *arrayValue;

	arrayValue = new double[5];

	while(exit != 0)
	{
		cout << "Enter any number: ";
		cin >> arrayValue[count-1];
		cout << endl;
		cout << arrayValue[count-1] << endl;
		if(count == 5 )
		{
			exit = 0;
		}

		++count;
	}
	cout << "You entered the following values: " << endl;
	int value1 = sizeof(arrayValue);     //only returning 8bytes
	int value2 = sizeof(arrayValue[0]);  //returning 8bytes for single element
	int value3 = sizeof(double);         //checked size of doulbe and it is 8bytes
	cout << "value1 equals: " << value1 << endl;
	cout << "value2 equals: " << value2 << endl;
	cout << "value3 equals: " << value3 << endl;
	cout << "arrayValueLength equals: " << arrayValueLength << endl;

	for(int i = 0; i < arrayValueLength; i++)
	{
		cout << arrayValue[i] << " ";
	}
	cout << endl;
	return 0;
}


What I have tried:

I don't know what else to try. Every resource I checked says that my sizeof value should be returning the correct value.
Posted
Updated 27-Nov-17 1:24am
Comments
Truck53 27-Nov-17 8:47am    
Do you have to manually code the size of the array when using a dynamic array. I assume that it is not possible to use a range based loop on a dynamic array either. I tried but I got a compiler error.

P.S.

Thanks for all the replies.

sizeof is correct, of course.
See, for instance: Do not apply the sizeof operator to a pointer when taking the size of an array[^].
 
Share this answer
 
v2
C++
double *arrayValue;

arrayValue is a pointer to a double, sizeof have no idea that it is an array and have no idea of its length.
 
Share this answer
 
Comments
CPallini 27-Nov-17 6:08am    
5.
Patrice T 27-Nov-17 6:20am    
Thank you
That is because arrayValue is a pointer to allocated memory and such pointers have always the same size regardless of the data type stored in the array (4 bytes for 32-bit processes and 8 bytes for 64-bit processes).

sizeof() will only return the total size for non dynamic arrays:
C++
double statArr[5];
double *dynArr = new double[5];
size_t statSize = sizeof(statArr); // 5 * sizeof(double): 40
size_t dynSize = sizeof(dynArr); // sizeof(double*): 8 (or 4 on 32-bit systems)
 
Share this answer
 
v2
The answer is plain and simple: you get the size of the pointer as result.
C++
double *arrayValue;
sizeof(arrayValue);

To get the memory size of an array you must get the size of the element type (or one element) and multiply with its count. For example:
C++
double array[40];
int arrSize1 = sizeof(double) * 40;
int arrSize2 = sizeof(array[0]) * 40;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900