Accessing the shared memory
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello!
I am using some third party active-x controls, which create the shared memory and save a DIB image on the specific location in RAM and return the pointer to that image as an output. (In C++ wrapper would be type long)
The pointer in Matlab has been read as a normal double variable, now I have to pass him to MEX function which reads an DIB image from the shared memory and returns back a matrix to matlab.
As I am not an experienced mex/c programmer, I am asking for any suggestions regarding the topic.
1 Kommentar
James Tursa
am 12 Mai 2015
We would need to see more specifics about what you are doing. What is the specific signature of the 3rd party function that generated this image in shared memory? I.e., what exact pointer type is it returning? How did you somehow get this into MATLAB as a double? (A bad idea, btw, as Walter points out below). For the mex function that reads the image, were you planning on type-punning the double into a pointer and then passing that to some 3rd party code? What is the signature of that function? Is it using the same library code that created the original pointer?
Antworten (1)
Walter Roberson
am 2 Sep. 2011
Bearbeitet: Walter Roberson
am 12 Mai 2015
In C, there is a problem with that: a pointer can require more space than a long. Indeed, in C, pointers to different types can in theory require different amounts of storage; the one guarantee in C is that all pointers will fit in to (void *) . C also promises that a type intptr_t exists which will be an integral type large enough to store a pointer, but C does not promise which integral type it will be. Strictly speaking C does not even promise that it will be an arithmetic type (e.g., you might not be able to apply operations such as ++ to it, in theory); there is, though, enough wiggle-room in the standards on that point that it is pretty safe to assume that arithmetic operations will be possible.
To put this in to blunter terms: there are quite a few systems on which a pointer needs 64 bits but a long is only 32 bits; on such systems, a pointer usually fits in to a "long long". (But that's not a promise.)
Returning a pointer as if it is a double is not a good idea. On some of the systems I have used, useful pointers had a much better than average chance of falling in to the bit pattern that Signalling NaN's occupy. And there are a fair number of operations in MATLAB which feel free to substitute the first non-signalling NaN for any NaN's, which would ruin the pointer usefulness.
Urrr -- you do know that in C, a "long" is not required to be more than 32 bits long, but a double is required to be at least 64 bits long?
Anyhow.... One hopes that James Tursa will happen upon this topic as he probably knows how to do this properly.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!