[C++] Let me get this straight...

General discussion for off-topic subjects.
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

[C++] Let me get this straight...

Postby BackRaw » Thu Oct 06, 2016 8:31 pm

- A pointer points to the address of allocated space
- A reference (lvalue reference, to be specific) is the address of allocated space
=> A pointer points to an lvalue reference
=> By dereferencing a pointer, you get the corresponding lvalue reference
=> By dereferencing an lvalue reference, you get the actual value that is stored in memory at that address (bits)

In other words: The name of an array of any type and size is a pointer to the address of the first element within itself, but it is not the actual reference to it. Therefore, by passing just the name of it to a function that expects an actual reference is not conforming to the C++ standard (of any year/edition) and therefore considered illegal, but is allowed by some (most) compilers because they then do the actual dereferencing of the lvalue reference you are pointing at in the background, thus making the code conforming to the standard behind the scenes and therefore valid CPU instructions.

In C++ words:

Syntax: Select all

int some_array[7];

// name of the array + pointer to the address of the first element of it
some_array

// lvalue reference (address) of the first element of it
&some_array[0]

// dereferenced lvalue reference (value in bits) of the first element of it
some_array[0]

// therefore...
some_array != &some_array[0] // pointer != lvalue reference
some_array != *some_array[0] // pointer != pointer to pointer [unless some_array is an array of pointers, I guess]
some_array != some_array[0] // pointer != value [for the sake of completeness]

Am I right to assume all of this?


Edit: The reason I'm asking is because of this example: http://www.cplusplus.com/reference/cstring/memset
The code I'm concerned about is:

Syntax: Select all

char str[] = "almost every programmer should know memset!";
memset (str,'-',6);
puts (str);

Having used memset as in the example at work and having it analysed via static code analysis tools (MSIRA C++ 2008 and PC-Lint v9), the analysis told me that this way of doing it is plain wrong (I don't know the exact wording any more, sorry). Anyways, yes, those tools are just checking for their own rules, however most of them do apply pretty well, as surprised as I am about that.

The correct code according to the static code analysis tools is:

Syntax: Select all

char str[] = "almost every programmer should know memset!";
memset (&str[0],'-',6); // <---- here
puts (str);
I'm not quite sure why this is, especially because memset expects a void pointer - with an emphasis on pointer. So I am a little confused, still. Is a void pointer actually a reference? lol

Thanks for your help :D

Edit 2: I think this is what I was looking for:
My rule of thumb is:

Use pointers if you want to do pointer arithmetic with them (e.g. incrementing the pointer address to step through an array) or if you ever have to pass a NULL-pointer.

Use references otherwise.

http://stackoverflow.com/a/114189/6805523

Nevertheless, what are your thoughts?

Return to “Whatever”

Who is online

Users browsing this forum: No registered users and 17 guests