Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Aw: Well defined meaning of pointer to an array

Reply
Thread Tools

Aw: Well defined meaning of pointer to an array

 
 
Syron
Guest
Posts: n/a
 
      04-18-2011
Just to put some oil into to fire... What is the following, Paul?

struct Foo {
int data[100];
};
int* bar = (int*)(new Foo);

The fact is that bar points to simply data, nothing else. The language structs/type-system are helpers for interpreting data the right way. Data and its interpretation (here as an int) is absolutely independent, thus it can be considered a nice coincident here that the second declaration points to astorage area that has been declared as an int earlier.

Arrays are only unaligned, continious storage areas of the same type, and the fact that an array alias (in the sense of "alias of a storage area") canbe implicitly converted into a pointer is just a convenience thing to iterate over the array elements.

Thus, when you write something like:
int A[100];
int* B = A;
B is both a pointer to an int as well as a pointer to an array of ints.

But my personal opinion is to call them the following:
- A is an alias for a storage area of the size of 100*sizeof(int).
- B is an alias for a storage area that can hold an address of memory.
- When dereferenced (by an array index specifier or by a pointer dereferencing asterisk), the memory referenced gets interpreted as an int.
- The fact that the elements at B+n*sizeof(int) with 1<n<100 /also/ have been declared as ints is nice, but nothing else.

By the way, 0x3f000000 interpreted as a float is 1.0.

(No, I'm no expert, but I spent some time thinking about ... Paul. These are some conclusions about my thoughts, and maybe they're wrong.)
 
Reply With Quote
 
 
 
 
Paul
Guest
Posts: n/a
 
      04-18-2011

>"Syron" <(E-Mail Removed)> wrote in message
>news:675375f5-7d4c-4ef5-99f5->(E-Mail Removed).. .
>Just to put some oil into to fire... What is the following, Paul?
>
>struct Foo {
> int data[100];
>};
>int* bar = (int*)(new Foo);
>
>The fact is that bar points to simply data, nothing else. The language
>structs/type-system are helpers for >interpreting data the right way. Data
>and its interpretation (here as an int) is absolutely independent, thus it
>can >be considered a nice coincident here that the second declaration
>points to a storage area that has been >declared as an int earlier.

This same concept can be represented by a void pointer.
void* p= almost_anything;

Remember a pointer doesn't alwas point to simple data , it may point to a
function for example. I prefer to think of a pointer as pointing to an
entity. This way its suitable for pointing to functions, arrays, polymorphic
types and simple data types.

>
>Arrays are only unaligned, continious storage areas of the same type, and
>the fact that an array alias (in the >sense of "alias of a storage area")
>can be implicitly converted into a pointer is just a convenience thing to
> >iterate over the array elements.

It's not just a convenience, its the underlying techincality because
historically an array is a pointer+offset in von neuman architecture. I
don't know of any system that has any other technical implementation.
>
>Thus, when you write something like:
> int A[100];
> int* B = A;
>B is both a pointer to an int as well as a pointer to an array of ints.


Yes I totally agree.
An array of ints is an entity that could be described as a contiguous
sequence of ints..
>
>But my personal opinion is to call them the following:
>- A is an alias for a storage area of the size of 100*sizeof(int).
>- B is an alias for a storage area that can hold an address of memory.
>- When dereferenced (by an array index specifier or by a pointer
>dereferencing asterisk), the memory >referenced gets interpreted as an int.
>- The fact that the elements at B+n*sizeof(int) with 1<n<100 /also/ have
>been declared as ints is nice, but >nothing else.

I think it should be 0<= n < 100.
What do you mean by "it's nice but nothing else"?
It's not just a random consequence that happens to be nice. It's the way
it's guaranteed to be, by the C++ standards.

>
>By the way, 0x3f000000 interpreted as a float is 1.0.

I don't think all implementations represent floating point numbers in the
same way.
I don't know where this random statement about floating point conversions
fits in with the rest of your post.

>
>(No, I'm no expert, but I spent some time thinking about ... Paul. These
>are some conclusions about my >thoughts, and maybe they're wrong.)


Your thoughts are welcome, Its makes a change from the repetative barrage of
nonsense I have to deal with form the numerous
http://redwing.hutman.net/~mreed/war...rouscranus.htm type
characters on this forum.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Well defined meaning of pointer to an array Paul C++ 65 04-23-2011 02:15 PM
Understanding search queries, semantics, and "Meaning" ...aren't weall looking for meaning? 5lvqbwl02@sneakemail.com Python 4 01-14-2009 02:28 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM
Array of pointer Vs Pointer to Array sangeetha C Programming 9 10-09-2004 07:01 PM



Advertisments