So what would be the size of this array in bytes if on a 32 bit machine: double *arr[4] ;

Recommended Answers

All 4 Replies

4 * sizeof(double*) bytes. You can't assume the size of a pointer, so the 32-bit machine part is irrelevant to the question.

What I know is that the size of Pointers vary according to the size of Address.
Since pointer is normally considered of 4 bytes (whatever its type may be int,double etc.), so size should be 4*4 = 16.
You can check it by executing the following code:

double *arr[4];
cout << sizeof (arr) << endl;

What I know is that the size of Pointers vary according to the size of Address.

The size of pointers can vary according to the datatype they point to as well. So a pointer to int isn't guaranteed to be the same size as a pointer to double. There's also no guarantee that the size of a pointer match the largest address on the platform. For modern PC platforms you'll find that they do tend to have these properties, but the C++ language definition does not require it.

It's kind of silly to make assumptions about your platform if it's not needed, and I'd be very interested to hear ways that it would be needed in this case.

Decepticon is mostly correct in that pointers are almost always the same size. Actually, I don't know of any modern systems where they aren't, otherwise virtual memory would be very difficult to deal with; however, the size of the data they point to can vary quite a bit, such as this:

typedef struct foo {
    int intpart;
    double dblpart;
    char carraypart[128];
} foo_t;
size_t intptrsize = sizeof(int*);
size_t fooptrsize = sizeof (foo_t*);
size_t intsize = sizeof(int);
size_t foosize = sizeof(foo_t);

intptrsize should equal fooptrsize, but intsize won't equal foosize. So, to get the size of the array (4 int pointers) would be sizeof(arr), which should equal to 4x the size of an int pointer. That is NOT the size of the data contained in the array, which depends upon what is allocated and assigned to each element. Since this is an array of double pointers, each element COULD be an array of doubles. Maybe this will make it clearer:

double* arr[4] = {0,0,0,0}; /* Four null pointers to doubles - 4*sizeof(double*) */
foot_t* farr[4] = {0,0,0,0}; /* Four null pointers to foot_t - 4*sizeof(foo_t*) */
arr[0] = (double*)calloc(100, sizeof(double));
farr[0] = (foo_t*)calloc(100, sizeof(foo_t));
/* The sizeof(arr) and sizeof(farr) should be the same in ALL modern systems.
   arr[0] now contains an array of 100 doubles, but sizeof(arr) is still the same.
   farr[0] now contains an array of 100 foo_t's, but sizeof(farr) is still the same.
   Also, sizeof(arr[0]) == sizeof(double*), not 100*sizeof(double), etc.
   Total size of arr is sizeof(arr) + 100*sizeof(double), but that is difficult
   to determine at runtime without extra effort on programmer's part.
*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.