help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Oct files: How to get read-only pointers to data for arguments of un


From: Stefan
Subject: Re: Oct files: How to get read-only pointers to data for arguments of unknown type?
Date: Tue, 28 Feb 2012 08:23:11 -0800 (PST)

Hello,

Thanks for your help.

| You are casting away const here, so you may see some unexpected 
| results. 
| 
| For example, try 
| 
| x = y = 1+i; 
| oct_test (x)
|

The data behind both x and y will be modified by oct_test(), since x is so
far used only as a pointer to y. I don't find this unexpected. Or this:

x = y = 1+i
y = 1+2*i
oct_test(x)

The data in y will not be modified, since y points to a different object
now. Just what one expects in a "lazy copy" language.

My applications are usually memory critical. Consider the memory needs of:

my_huge_matrix = octfile(my_huge_matrix)

Here, octave makes a copy of my_huge_matrix at the call of octfile, and then
copies the result back to the workspace. So 3 copies of my_huge_matrix must
fit into memory. It is particularly annoying if the memory error comes only
after the expensive calculation is completed. In pass-by-reference, once I
succeed to fit the initial matrix into memory, I know that the result will
fit as well.

This is of course the same problem in matlab. Only, since matlab mex is in
C, it was easier to get pass-by-reference behavior for mex files. In
octave's C++ API, everything is overloaded and has side effects, so you
never know which seemingly harmless statement causes a copy of the data
behind the scenes. (On a side note, I always thought that a simple void* in
C is vastly superior to C++ templates. Why all the copy-paste source files
like int32NDArray.cc, int64NDArray.cc, dNDArray.cc, ... ?)

There could be a mechanism to attach an attribute to a variable that should
be passed by reference, like IDL's TEMPORARY mechanism. Something like this
might be a good idea.

Thanks for the discussion,

Stefan


--
View this message in context: 
http://octave.1599824.n4.nabble.com/Oct-files-How-to-get-read-only-pointers-to-data-for-arguments-of-unknown-type-tp4425650p4428652.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]