Posted: Tue Jul 05, 2011 10:58 am Post subject: Re: RE:Arrays and Mutability
apython1992 @ Tue Jul 05, 2011 8:45 am wrote:
I am trying to change the pointer's address in memory (pointing to NULL), which I believe is strictly forbidden with arrays, because they are essentially constant pointers. I'm finding myself getting thoroughly surprised by the behaviour of g++ - it sure isn't picking up a lot of these mis-uses
That's the problem. Not that it's forbidden, but you are changing the value of the local copy of the pointer (pointer's address is passed by-value; you are changing that value). For the changes to persist, you need to change only the data being pointed to (this is why we have double **pointers )
g++ will let you do all kinds of undefined behaviours because... heck, you might actually know better (about the specifics of your target hardware, OS, or any combination there-of). Interesting read -- The Story of Mel, a Real Programmerhttp://www.outpost9.com/reference/jargon/jargon_49.html
Posted: Tue Jul 05, 2011 11:34 am Post subject: RE:Arrays and Mutability
All of a sudden C++ feels way too nice
Thanks Tony, I've resolved the problem.
Brightguy
Posted: Tue Jul 05, 2011 3:11 pm Post subject: Re: RE:Arrays and Mutability
Insectoid @ Mon Jul 04, 2011 6:22 pm wrote:
Isn't that exactly what happens when you declare an array of characters in the following manner?
code:
char foo[5];
There's no pointer there. In many contexts foo can be used as if it were a pointer, but it's not: foo is of type "array of 5 chars".
Insectoid
Posted: Tue Jul 05, 2011 3:49 pm Post subject: Re: RE:Arrays and Mutability
Brightguy @ Tue Jul 05, 2011 3:11 pm wrote:
Insectoid @ Mon Jul 04, 2011 6:22 pm wrote:
Isn't that exactly what happens when you declare an array of characters in the following manner?
code:
char foo[5];
There's no pointer there. In many contexts foo can be used as if it were a pointer, but it's not: foo is of type "array of 5 chars".
As I understand, foo itself is just a constant pointer to the first element of the array, and enough RAM is allocated at that location in the stack to contain the entire array. The rest is just syntactical sugar. This is how it has always been explained to me. Now, when the compiler sees 'char foo[]', does it actually see a unique array type, or does it see some sort of internal function that takes a type and a size and returns a pointer to that RAM?
Now, some out-loud thought. I'm not sure exactly how a pointer works. As far as I know, it's basically a 4-byte (larger if you're running a 64 bit system I think) integer that just contains an address. However, if you add to a pointer (say, *int +2) the compiler knows to add 2*sizeof int to the pointer, and go to that location. Where is the metadata stored, that says it is a pointer to integer? Is this done at compile time, or runtime? I suspect it is compile-time, as that would reduce RAM usage of the final product, and explains why I can't subsequently point from an integer to a character. If this is the case, then an array and a pointer are identical, except that an array is allocated on the stack and a pointer (with malloc) allocates to the heap. Now, if there is a command similar to malloc that allocates to the stack, then there is no need for array syntax beyond convenience, as there is no actual array type (because it's just a pointer and allocated RAM).
Now bear in mind, this is all just assumptions based on what I've been told, and what I've guessed. I may be way off the mark here, so correct me if I'm wrong.
Brightguy
Posted: Tue Jul 05, 2011 4:30 pm Post subject: Re: Arrays and Mutability
The array foo is not a pointer. That said, in most cases where 'foo' appears in your code, a pointer is immediately constructed which points to the memory location of the array's first element. Essentally, 'foo' acts as if you wrote '&foo'. Tony gives an example where this does not happen, namely in sizeof(foo).
Velocity
Posted: Tue Dec 13, 2011 10:59 am Post subject: RE:Arrays and Mutability