Public Group

# quick explanation of references please

This topic is 4909 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In the following block of code can anyone explain why the sizeof() function returns a 4. And what are the differences in array declarations between b and c.
A little demonstration:
#include <iostream>
using namespace std;
int main(void) {
int a;
int b[10];
int *c = new int[10];
cout << sizeof(int) << " = " << sizeof(a) << endl;	// 4
cout << sizeof(int[10]) << " = " << sizeof(b) << endl;	// 40
cout << sizeof(int*) << " = " << sizeof(c) << endl;	// 4
delete [] c;


##### Share on other sites
sizeof() returns the size of 'something' in bytes (as it is known at compile time).

An int on your computer is 4 bytes (32 bits).
A pointer (int *, void *, etc.) is also 4 bytes (32 bits).

An array of 10 ints, is thus 40 bytes. A pointer to this array is only 4 bytes.

Try using 'char' instead of 'int' to see what happens.

Compiling and running the same program on an old 8bit homecomputer would give you different results, something like: 2, 20, 2.

Hope that helps! :)

##### Share on other sites
sizeof() returns the size in bytes of the datatype passed to it. In this case an int, which is 4 bytes (or 32 bits) on your computer.

The difference between b and c is that b is allocated on the stack, while c is being allocated on the heap using new. In order to use memory allocated on the heap (=)), you need to obtain a pointer to the beginning of the block allocated by new.

edit: too slow!

##### Share on other sites
You might be confused by the fact that you can almost always use the same notation on both pointers and arrays. Like, you can use brackets on a pointer:

int *a = new int[10];
a[1] = 2;

or you can do pointer arithmetic on an array reference:

int b[10];
*(b + 1) = 2;

So you might be tempted to think that they are the same thing. But despite their incredible similarities, pointers and array references are not the same. Your sizeof() code is a great example of where they differ.

##### Share on other sites
@ontheheap:

The heap nor the stack have anything to do with sizeof(). It's like pinacolada said: the array and the pointer are different things (no matter where they're allocated).

I personally see an array as a pointer known at compile-time. Pointers which can only be resolved at run-time often need some space in RAM. That's where the sizeof() confusion comes from.

To put it simple:

'int *' is a place to _store_ a pointer [to an array or some other place in memory]. Its size is 4 bytes on most modern computers.

'[', '*', '&' and ']' are your friends! :)

[Edited by - Jan-Lieuwe on February 9, 2005 4:14:51 PM]

##### Share on other sites
Quote:
 Original post by Jan-Lieuwe@ontheheap:The heap nor the stack have anything to do with sizeof(). It's like pinacolada said: the array and the pointer are different things (no matter where they're allocated).

The OP stated two questions, the first being the sizeof() question, the second being "differences in array declarations between b and c". The difference in b and c helps explain why their sizeof()'s are different results.

##### Share on other sites
references are EXACTLY like pointers, except that U CANT have NULL references
(for some functions using references can provide speed improvements cuz u dont have to validate a pointer)

##### Share on other sites
are there still advantages of using pointers ove references? I know Java only has references which makes programming alot easier in that language.

##### Share on other sites
Java doesn't have pointers (from my point of view) because of security reasons. Just imagine a pointer intentionally pointing to an unsafe área could bring down not only your app, but the entire JVM.

Plus, I see Java like "C++ without all that things that make programmers run away from it". so to make things simpler, they used only references.

Check this. (point 17).

Hope this helps you to understand things a little more.
If you really want to understand all about pointers, just ignore java. When you feel a little more confident about them, Java references will seem pretty simple and logical.

##### Share on other sites
Quote:
 Original post by Jan-Lieuwe@ontheheap:The heap nor the stack have anything to do with sizeof(). It's like pinacolada said: the array and the pointer are different things (no matter where they're allocated).

I always appreciate people checking replies for inaccuracies, but in this case I was answering two separate questions. One regarding sizeof(), and the other regarding the difference between using and not using new to create an array.

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 23
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631766
• Total Posts
3002235
×