Re: Binary file from C++ to Matlab
- From: "sturlamolden" <sturlamolden@xxxxxxxx>
- Date: 24 Mar 2006 08:24:57 -0800
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.
.
- Follow-Ups:
- Re: Binary file from C++ to Matlab
- From: noel
- Re: Binary file from C++ to Matlab
- References:
- Re: Binary file from C++ to Matlab
- From: sturlamolden
- Re: Binary file from C++ to Matlab
- From: Rune Allnor
- Re: Binary file from C++ to Matlab
- From: sturlamolden
- Re: Binary file from C++ to Matlab
- From: Venkat
- Re: Binary file from C++ to Matlab
- From: sturlamolden
- Re: Binary file from C++ to Matlab
- From: Venkat
- Re: Binary file from C++ to Matlab
- Prev by Date: Re: Small complete distribution
- Next by Date: Re: Multiple line edit text boxes
- Previous by thread: Re: Binary file from C++ to Matlab
- Next by thread: Re: Binary file from C++ to Matlab
- Index(es):
Relevant Pages
|