I am trying to wrap some C-code into Matlab.
But here I get a prototype of C++ function declared with a reference:
char myfunction(const short *Tab, const char TabMaxSize, char ¤tIdxTab);
I understand that the built-in lcc compiler is not appropriate for C++ so I use Visual Studio 2008 as a supported C++ compiler.
With lcc, I get this error:
Error Myfunc.c: .\myheader.h: 137 syntax error; found `&' expecting `)'
With Visual C++ (in french), I get:
c:\...\myheader.h(137) : error C2143: erreur de syntaxe?: absence de ')' avant '&'
My question is:
Is there a trick to use reference with mex-file or should I recode C++ functions?
I am not allowed to modify C/C++ code
Thanks in advance,
Nicolas
It seems that the compiler for some reason works in C mode,
in which case it will reject a number of C++ idioms. One reason
why it switches to C mode is that the source file names have
ending .c instead of .cpp.
If you are allowed to change source code file names, make sure
that the endings of source code files are .cpp, not .c.
If you are not allowd to change file names, try to compile with
the /TP compiler option. This, if I understand things correctly,
forces the compiler to wok in C++ mode no matter the file name.
Rune
It compile without error even if I get a lot of warning about casting now.
However, I have to use source.c. Is there a way to force the compiler to compile in C++ even if I use source.c?
Regards,
Nicolas
I use Microsoft Visual C++ 2008 according to the Compatible Compiler List http://www.mathworks.com/support/compilers/current_release/
Is there a FREE (I'm looking for Open Source) compiler which allows to switch between C and C++ depending on what the user (not the developper) needs?
The solution is to use Open Watcom:
http://www.openwatcom.org/index.php/Main_Page
It has all I wanted to develop my mex-files and a lot of tools well designed too.
For the C/C++ problem, it send warnings only about the useful information.
Ideal for beginners.
Thanks,
Nicolas
Create a 1-line file source.cpp:
#include "source.c"
Then compile source.cpp. However, I am totally confused why, if you have access to source.c, you can't simply create a copy of it called source.cpp.
James Tursa
Moreover, I won't be present to wrap each source.c in .cpp.
(I have already written a function to convert .c in .cpp, this is not the problem.)
The final user will just be able to modify source.c.
Regards,
Nicolas
> It's a student project to demonstrate how mex-files could be useful in an application.
> Obviously, it seems unappropriate to use .c instead of .cpp but I can't ask to the final user to define each modified source.c in source.cpp because it is an extra-operation.
If this is a student project, the confusion of C and CPP sources is not appropriate! A demonstration of how mex files are useful does urgently needs usable sources. Creating a C-mex file with a C-compiler from CPP sources (or the other way around) is not "useful", but confuses 1. the compiler and 2. the student.
In my personal opinion, it would be helpful for the students to ask them to rename the C-source.
Kind regards, Jan
Sorry, I am the student in the story and my supervisor send me source.c.
The point is that he uses reference "&" not a pointer.
Reference is an idea coming from C++ and so there is a problem with "&" operator and mex "C"-compiler which can be avoided using a C++ compiler.
I am just trying to force the compiler to compile instead of sending an error about "&".
Thanks,
Nicolas
> >Creating a C-mex file with a C-compiler from CPP sources (or the other way around) is >not "useful", but confuses 1. the compiler and 2. the student.
>
> Sorry, I am the student in the story and my supervisor send me source.c.
Then please forward my posting to your teacher.
> The point is that he uses reference "&" not a pointer.
> Reference is an idea coming from C++ and so there is a problem with "&" operator and mex "C"-compiler which can be avoided using a C++ compiler.
>
> I am just trying to force the compiler to compile instead of sending an error about "&".
I think, this is the perfect time for a meta-solution. Tell your teacher, that the question was wrong and therefore a "correct" answer is impossible (In the "real life" after the University, this happens nearly twice a day...). To perform your task of answering the question, reformulate it by renaming the C-source to CPP.
Perhaps your teacher used a C-compiler, which automatically switchs to CPP is a & appears or which compiles in CPP as default - or any other strange constellation might have yield to this problem. But it is not a good idea to use strange methods to solve strange problems. It is fundamental for scientific work (as for craftsmen also) to apply tools which match the problem (here: C-compiler for C-source, CPP-compiler for CPP-source).
Sorry for not "solving" your problem.
Good luck, Jan
PS. I had such a question which cannot be solved without leaving the context during my oral examination: Professor: "Why is the charge of the electron and the proton exactly equal except for the sign?" Me: "I don't know." Professor: "Exactly! We don't know. Good answer."
I guess my supervisor is not a fool and knows there is a lot of problem using "&" (that's the first time we both use mex-file to convert an entire project).
However, it is a common practice in the embedded world to use references instead of pointers to speed up computations.
Since Matlab claims to be a powerful tool to design embedded application, we thought this kind of problem was solved.
Perhaps I should use an other compiler with an other IDE to create a dll and then call this dll from Matlab.
Or I could code a function to handle the "&" references and post it on the FEX, but I thought it was already done.
Regards,
Nicolas
> I guess my supervisor is not a fool and knows there is a lot of problem using "&" (that's the first time we both use mex-file to convert an entire project).
> Since Matlab claims to be a powerful tool to design embedded application, we thought this kind of problem was solved.
> Or I could code a function to handle the "&" references and post it on the FEX, but I thought it was already done.
Oh, sorry for my less precise formulations (not a native speaker, as you can guess): I do not think, that your supervisor is a fool.
I just think, that there is no need and no possibility to use the CPP operator & in C source and with a C-compiler. This is no problem of Matlab, to be exact, it is even not a problem at all. C and CPP are just different languages and need different compilers.
Would the problem be solved if you install the free OpenWatcom 1.8 CPP compiler and rename the files from .c to .cpp?
Kind regards, also to your teacher, Jan
Agreed. The teacher (a teaching assstant?) is not
aware of the required attention to detail and strict
adherence to standards, when issuing such a program.
> Perhaps your teacher used a C-compiler, which automatically switchs to CPP is a & appears or which compiles in CPP as default - or any other strange constellation might have yield to this problem.
All C and C++ compilers include extensions to what is
standardized. What is useful and stanardized in C is
also useful, but possibly not standardized, in C++.
And vice versa.
So compiler vendors have a tendecy to mix and match
C and C++ constructs without regard to what is standardized.
Which means that one C compiler might support a C++
feature, while the next does not. It seems your teacher
uses a C compiler that accepts C++ references, whereas
yours does not.
Which is a lesson in why it is so important to stick
to the standardized language, be it C or C++.
Once upon a time I worked a few months with a team of
scientists that were leading on simulationg a particular
physical phenomenon. The guys wrote a highly efficient C
program for the simulations. As they started publishing
their results, everybody else wanted to get their code,
so they, too, could study the phenomenon.
A couple of years later I met the head scientist at a
conference. "It's so strange," he said, "We send the code
all over the place but no one ever give us feedback!"
As it happened, I got to see the code a couple of weeks
later, when my collegue got a copy. It turned out that
the guy who implemented the program had worked with a
DEC Alpha workstation and the AIX OS. And he had used
every OS- and system-dependent bell and whistle he could
find.
There was no chance whatsoever that the code could compile
on any other system (possibly even individual workstation)
than what the coder had used when implementing the stuff.
And of course, that summer with this particular group was
the last time I ever saw a DEC Alpha or the AIX OS.
Rune
Huh? This sentence doesn't make any sense to me. References in C++ is just a way to specify the passing convention ... it basically does the same thing as passing pointers except it relieves the programmer of the burden of doing the manual dereferencing inside the called function. There is no speedup to any computations that I am aware of.
James Tursa
> > However, it is a common practice in the embedded world to use references instead of pointers to speed up computations.
>
> Huh? This sentence doesn't make any sense to me. References in C++ is just a way to specify the passing convention ... it basically does the same thing as passing pointers except it relieves the programmer of the burden of doing the manual dereferencing inside the called function. There is no speedup to any computations that I am aware of.
Without any doubt, you are correct - in theory and for well designed compilers.
In practise I've worked with a Symantec-Cpp-Compiler on MacOS9, which has been faster for pointers than for references. I've called this a "feature worth to be improved".
By the way: The LCC 2.4 shipped with Matlab (at least until 2009a) uses sometimes(!) better optimization techniques (up to 10% faster), if an unused variable is declared. I assume something with the byte-alignment...
For such reasons I am disappointed, that compiled MEX functions are not accepted on the FEX anymore (although you find on the page for new submissions: "Compiled files must be accompanied by their source").
Kind regards, Jan
I read something about references, pointers and "handles" (it is my first program in C++, sorry if I am boring but I have to start from the beginning to understand):
http://www.parashift.com/c++-faq-lite/references.html
"References are usually preferred over pointers whenever you don't need "reseating". This usually means that references are most useful in a class's public interface. References typically appear on the skin of an object, and pointers on the inside."
Since my program uses a big structure defined in C (and a structure is a public class in c++, isn't it?) to handle every meaningful variables, I thinks it is why references are used.
For the compiler's point of view:
http://stackoverflow.com/questions/57483/difference-between-pointer-variable-and-reference-variable-in-c
"A pointer has its own memory address and size on the stack (4 bytes on x86), whereas a reference shares the same memory address but also takes up some space on the stack.
Since a reference has the same address as the original variable itself, it is safe to think of a reference as another name for the same variable.
A pointer is just a variable that holds a memory address. This variable is on the stack. Since a reference has its own space on the stack, and since the address is the same as the variable it references, this implies that there is real a address of a reference that the compiler will not tell you."
I think it explains why the Symantec-Cpp-Compiler has been faster for pointers than for references.
Since a structure is created on the heap and since the variables on the heap are slower to allocate in comparison to variables on the stack (OS lesson that I have not forgotten), it takes more time to use references because the reference to the structure is the structure itself and not just a pointer to the pointee.
However, there is something else that is annoying in my program that I don't understand (and the Watcom compiler too...).
The program uses a union composed of two structures, but there is no identifier for the first structure in the union:
typedef union {
st_B ;
st_A A ;
} un_B;
Apparently, it is not a mistake.
What is it? Where could I find a kind of tutorial about it?
Best regards,
Nicolas
If so, rename the source code file to .cpp or whatever your
compiler understands as C++. Again, C and C++ are two different
languages.
Coose which one to use, and stick with it.
> I think it explains why the Symantec-Cpp-Compiler has been faster for pointers than for references.
Forget about "fast". You are nowhere near skilled enough to
start discussing fast or slow C/C++ code. It will take you
at least a year, maybe two, to get to the point where you
can start discussing run-time efficiency.
C and C++ are not a languages to mess around with like a
sourcerer's apprentice. Once the program compiles, the
computer does *exactly* what the program specifies it to
do, with no safeguards or second-guessing of if it makes
sense. It is *yours*, the programmers, responsibility to
actually make sure that what the program has the computer
do is indeed what you want it to do.
Make the slightest mistake, and you might find yourself in
big trouble, like somebody described here not too long ago:
http://groups.google.no/group/comp.soft-sys.matlab/msg/72e63bfc8f1a7860?hl=no
"Re rogue pointers--that is *exactly* what happens.
After one of these crashes Matlab becomes completely
unpredictable. Sometimes I can keep going. Other
times (and at other stages of debugging) it will close
immediately, Windows will give me a (fatal) runtime
error, or--my favorite--it won't return to the command
line and won't let me close it, either, giving me an
error message when I try to do so."
> What is it? Where could I find a kind of tutorial about it?
Read some books about programming. If you choose to
pursue C++ (as opposed to C), try the book "Programming"
by Bjarne Stroustrup.
Rune
I apologize if my post seems arrogant to you Rune but I am not playing with compilers for fun and I am just here to learn something as do students on this newsgroup.
IAR 's compilers allow engineers to use Embedded C++ which is a subset of C++ intended for embedded systems programming.
As a subset, is it based on C but uses some features of C++.
I am just trying to use some of these features in a mex-file in order to embed IAR-source.c in Matlab without modifications (except mexFunction) and I was looking for help on the newsgroup.
About my problem of union containing a structure without identifier, it is not an anonymous structure so I am not looking for Bjarne Stroustrup's book but maybe for Andrei Alexandrescu's books.
About rogue pointers, I don't understand what is your problem. To pass by reference using references is the same thing than to pass by reference using pointers, there is no blue screens or fatal errors. I wasn't looking for hacking but just wondering why precisely should I use references instead of pointers and since the compiler implements references as pointers, I could write some macros to do it before mex's command.
Regards,
Nicolas
Your union declaration is incorrect; you can't just have a type name
without an identifier name following it in a declaration. I'd suggest
dumping any compiler that doesn't produce an error for that construct.
I believe that the newer versions of MATLAB support the Express
Edition of Visual C++; this a free C/C++ compiler.
You seem to have some basic misunderstanding of how memory access,
memory management etc. works. Heap access is NOT slower than stack
access; unless of course the RAM where the heap section is allocated
has slower access times compared to the RAM where the stack is
allocated. Variables created on the heap are usually only dynamically
allocated variables (using malloc, calloc, new etc.). Doing this is
slower than declaring an equivalently sized statically allocated
variable because for the former the heap manager must perform checks
and add it to a data structure to track it. In case of the statically
allocated variable the compiler knows about it at compile time and can
reserve space for it. For instance, declaring an int32 within the
scope of a function tells the compiler to push 4 bytes onto the stack
when entering the function; no additional checks need to be made for
this. However, once allocation is done, accessing both heap and stack
variables should take the same amount of time (when you really get
down into the nitty-gritty this may not always be true due to
processor addressing modes and such but that's beyond the scope of
this discussion).
Also, all structures are not created on the heap! Only the ones you
create using a dynamic memory allocation function will be created on
the heap, as will any such variable, regardless of its type. A
statically allocated structure will exist on the stack (if declared
within function scope) or in a data segment (.bss, .data etc.) if
declared in global scope.
You're correct about the difference between references and pointers.
The pointer does take an extra memory location but other than that, as
James mentioned, references are just easier to use when coding because
you avoid having to dereference them. There's nothing about a pointer
or a reference that should make one faster than the other. The
Symantec compiler that Jan experimented with probably had a bug in it
that caused that behavior.
HTH,
Ashish
You do not come across as arrogant, rather as ignorant.
If you are serious about using C or C++, take using them
seriously.
> About my problem of union containing a structure without identifier, it is not an anonymous structure so I am not looking for Bjarne Stroustrup's book but maybe for Andrei Alexandrescu's books.
Alecsandrscu don't write for beginners or bnewbies.
Take my word for it: You want Stroustup's book.
> About rogue pointers, I don't understand what is your problem. To pass by reference using references is the same thing than to pass by reference using pointers, there is no blue screens or fatal errors.
Again, it is up to you to know exactly what you are
doing. And assume responsibility when you do not.
Rune
One book I found very enlightening was "Expert C Programming. Deep C Secrets" by Peter Van Der Linden. It won't teach you much about normal programming, but it does give fascinating insight into some of the ways compilers and operating systems work and the way data is actually handled.