Re: Binary file from C++ to Matlab



if you want unmodifiable pointers and unmodifiable elements, this is
your statement:

double const * const ptr = {1.0, 2.0, 3.0};

But since the C and C++ languages are not strongly > typed, the
compiler will silently ignore the type
declaration mismatch in

double *ptr = {1.0, 2.0, 3.0};

and generate code with undefined action is
write operations are attempted.

what does strongly or weakly typed have to do with anything?

sturlamolden wrote:



Venkat wrote:

I had a mistake in the C++ code. value was a pointer to double
and
the c++ code should have been
foo.write((char *)value,dim*sizeof(double));


Yes, pointers and arrays are convoluted in C/C++, yet very
different.
This is one very common way C and C++ allows you to shoot yourself
in
the foot. By convoluting array and pointer syntax, C and C++
introduces
an entire class of bugs that are very difficult to trace and
impossible
to create with the majority of other languages.

Take a look at this:

double arr[] = {1.0, 2.0, 3.0}; /* this is an array variable
*/
double *ptr = {1.0, 2.0, 3.0}; /* this is a pointer and an
array literal */


1. Analysis of the variable arr:

arr is an array of doubles but also an acronym for &arr. &arr is
the
address of the array. The size of arr is the number of elements in
the
array times the size of a double. You can safely overwrite the
content
of arr:

arr[0] = 0.0; /* not a bug */


2. Analysis of the variable ptr:

ptr is a pointer to a constant array literal. ptr is not an acronym
for
&ptr. &ptr is the adress of the pointer. The size of ptr is the
size of
a pointer, e.g. 4 on 32 bits systems. You *cannot* safely overwrite
the
content of the array pointed to by ptr. Although the compiler will
allow write operations to the memory pointed to by ptr, it is still
an
error to do so. It will produce undefined behaviour according to
the
standard:

*ptr = 0.0; /* undefined behaviour */

My declaration should have been:

const double *ptr = {1.0, 2.0, 3.0};

Now the compiler will disallow the statement *ptr = 0.0; But since
the
C and C++ languages are not strongly typed, the compiler will
silently
ignore the type declaration mismatch in

double *ptr = {1.0, 2.0, 3.0};

and generate code with undefined action is write operations are
attempted.


3. Different meaning of the & (address of) operator:

It is very easy to confuse the meaning of &arr and &ptr, which is
how
your problem originated. &arr and arr mean the same. &ptr and ptr
do
not mean the same.


4. Different meaning of double arr[] and double arr[] depending on
context:

If double arr[] occurs in the declaration of a local variable, arr
is
an array. If double arr[] occurs in the declaration of a function
argument, arr is actually a pointer and not an array. Thus, &arr
and
&arr will also have different meaning depending on context.


.



Relevant Pages

  • Re: Evaluating unary *
    ... 'arr' exists, ... value can be used with the same syntax as would be used to access a 2D array of the kind you're referring to, but that 2D array is just a different way of looking as the same object that was already created by the definition of 'arr'. ... to me, it makes sense to return a pointer to the first value of an array, but to return the address of the pointer to the first value of an array, is not directly possible as such. ... lea eax, ...
    (comp.std.c)
  • Re: Typecasting pointers
    ... array of shorts, and initialize them all to the same value. ... You're saving the value of ptr so you can freeit later. ... A pointer increment advances the pointer by one object size, ... optimizing compiler could have done a better job than you have. ...
    (comp.lang.c)
  • Re: Evaluating unary *
    ... value can be used with the same syntax as would be used to access a 2D array of the kind you're referring to, but that 2D array is just a different way of looking as the same object that was already created by the definition of 'arr'. ... the expression &arr requires no special handling beyond insertion of the appropriate address into a suitable register. ... to me, it makes sense to return a pointer to the first value of an array, but to return the address of the pointer to the first value of an array, is not directly possible as such. ... It does create a pointer value which points at arr itself, and treats the entirety of arr as the first element in an array containing exactly one element of type int. ...
    (comp.std.c)
  • Re: multi dimensional arrays as one dimension array
    ... Are you of the opinion that one or both of memcpy(p, arr, sizeof arr) ... way of converting a two-dimensional array into a one-dimensional ... &arr) is supposed to be pointer constrained legally to range over ... arrays of character type and other objects treated as arrays ...
    (comp.lang.c)
  • Re: Binary file from C++ to Matlab
    ... By convoluting array and pointer syntax, ... arr is an array of doubles but also an acronym for &arr. ... ptr is a pointer to a constant array literal. ...
    (comp.soft-sys.matlab)