I have a vendor specifc code for ADC and other peripherals.
Now using it I am understanding the flow.
the extension of the files are .cpp but the statements there are similar to C rather C++
i.e. printf() is used instead of cout;
no namespace.std defined... also other things which made me sure that it is a c-language code.
(pardon me but whatever I ask the vendors but response is quite late from there)
So it is a complete C- code. But while understangin I came to a point where the class is defined and I am really puzzled now. Since I have not seen or heard any one using class
C4DSPBlast cBlast;
cBlast.GetBlastInfo();
where C4DSPBlast cBlast;
and following code shows that C4DSPBlast is a class. Now, while debugging, I found that I am getting error at exactly this statement cBlast.GetBlastInfo();
but since I don't know the classes in C- I post it here since I am not getting any forward any further in debugging.
class C4DSPBlast
{
public:
//! empty constructor.
C4DSPBlast(void);
//! empty destructor.
~C4DSPBlast(void);
//! Get BLAST information from the hardware(firmware).
/*!
* Read the BLAST information from an the PCI memory attached to the hardware device. This function populates internal class members with this information.
* #return CBLAST_IO_ERROR_BLAST_INFO_RD, CBLAST_NO_DEV_TYPE or CBLAST_SUCCESS if no errors.
*/
int GetBlastInfo(void);
//! m_valBLASTRegister the standard BLAST information register.
union { BLASTReg m_BLASTRegister; unsigned long m_val0; } m_valBLASTRegister;
//! m_valBLASTRegisterExt the extended BLAST information register.
union { BLASTReg m_BLASTRegisterExt; unsigned long m_val1; } m_valBLASTRegisterExt;
//! The whole BLAST information populated by GetBlastInfo() as a C data structure.
struct BOARD m_cBoard;
};
The code is C++. Compile it as C++ and the errors will disappear.
C and C++ are different languages. Current common convention is that if you give file with an extension .c to the compiler, it will compile it as C file. If you give it a .cpp or .cxx (exact list depends on the compiler) it will process it as C++ file. This will work even if you put mixture of C/C++ files on the same command line.
If you pick up arbitrary C file, rename to .cpp, and give it to compiler, 99% chance that it will be compiled. C++ standard describes a list of incompatibilities with C, but these are rather rare things.
Most likely that you see a file that was created as C and then started its new life as C++.
Related
Well this might be a very weird question but my curiosity has striken pretty hard on this. So here it goes...
NOTE: Lets take the language C into consideration here.
As programmers we usually define a user-defined datatype(say struct) in the source code with the appropriate name.
Suppose I have a program in which I have a structure defined as:
struct Animal {
char *name;
int lifeSpan;
};
And also I have started the execution of this program.
Now, my question here is;
What if I want to define a new structure called "Plant" just like "Animal" mentioned above in my program, without writing its definition in the source code itself(which is obviously impossible currently) but rather from a user input string(or a file input) during runtime.
Lets say my program takes input string from a text file named file1.txt whose content is:
struct Plant {
char *name;
int lifeSpan;
};
What I want now is to have a new structure named "Plant" in my program which is already in execution. The program should read the file content and create a structure as written in the file and attach it to itself on-the-go.
I have checked out a solution for C++ in the discussion Declaring a data type dynamically in C++ but it doesnt seem to have a very convincing solution.
The solution I am looking for is at the compiler-linker-loader level rather than from the language itself.I would be very pleased and thankful if anyone is looking forward to sharing their ideas on this.
What you're asking about is basically "can we implement C as a scripting language?", since this is the only way code can be executed after compilation.
I'm aware that people have been writing (mostly in the comments) that it's possible in other languages but isn't possible in C, since C is a compiled language (hence data types should be defined during compile time).
However, to the best of my knowledge it's actually possible (and might not be as hard as one would imagine).
There are many possible approaches (machine code emulation (VM), JIT compilation, etc').
One approach will use a C compiler to compile the C script as an external dynamic library (.dll on windows, .so on linux, etc') and than "load" the compiled library and execute the code (this is pretty much the JIT compilation approach, for lazy people).
EDIT:
As mentioned in the comments, by using this approach, the new type is loaded as part of an external library.
The original code won't know about this new type, only the new code (or library) will be "aware" of this new type and able to properly use it.
On the other hand, I'm not sure why you're insisting on the need to use static types and a compiler-linker-loader level solution.
The language itself (the C language) can manage this task dynamically (during execution time).
Consider Ruby MRI, for example. The Ruby language supports dynamic types that can be defined during runtime...
...However, this is implemented in C and it's possible to use the code from within C to define new modules and classes. These aren't static types that can be tested during compilation (type creation and identification is performed during runtime).
This is a perfect example showing that C (as a language) can dynamically define "types".
However, this is also a poor example because Ruby's approach is slow. A custom approved can be far faster since it would avoid the huge overhead related to functionality you might not need (such as inheritance).
I would like to be able to automatically generate a class in code if possible.
I know that I can have a text or script file that can be opened and the contents of that file be loaded into either a vector of strings or a string stream, and from there write back to a file or set of files to generate a class. I'm not interested in the details of the parsing aspect and this is not what I'm actually after.
Let's say I have a text file that looks something like this: My current pseudo file above is much longer with more verbose detailed explanations; but omitted here for simplicity. If you feel that it is needed don't hesitate to ask and I will post it.
script
// The finalized scripting file & its parser will not have any comments within code sections.
// Comments can be found before & after the <begin:file> & <end:file> sections
// This is the beginning of the file and whether or not a header and or cpp file
// is generated or not. If not then the idea is to generate the class in code directly.
// <begin:file "Foo.h"> // header only otherwise
// <being:file "Foo.h", "Foo.cpp"> for both header and cpp
<begin:file>
<class:"Foo">
<private:>
<variables: int=mX,mY,mZ float=mWidth,mHeight>
<public:>
<constructor:defualt=t, init=t>
<constructor:copy=t> // automatically generates both copy() & operator=() as = default;
<constructor:copy=f> // declares both copy() & operator() as = delete;
<destructor:default=t>
<end:class>
<end:file>
In the above script where I have <begin:file> since there are no strings after it; this means I do not want to write to files to create a header and or cpp file. Since they are omitted I would like to generate this class in code.
I do not want to resort to using macros. I could use templates if possible or some other mechanism.
What I am not sure about is this: let's say I'm at the part where I read in <class:"Foo"> this will tell my parser that I want a class named Foo and this would be it's shell:
class Foo {};
As expected, however we can not write the ending }; part yet because we have not reached the <end:class> part. So at this point we need to write out class Foo { and the part or problem that I'm seeing here is I do not know how I would be able to take the text or string such as std::string name("Foo");
and appended that after the c++ key word class. Pseudo example:
{
std::string name("Foo");
class name {
public:
int x;
};
std::cout << name << std::endl; // compiles and prints to the console "Foo"
std::cout << name.y << std::end; // will not compile.
}
The problem here is that after c++'s key word class it is expecting an identifier and the compiler will not accept this. The compiler will declare a string named name that has the contents of "Foo", then below when trying to declare the class using that string, it doesn't see the string and names to the class with the identifier name. Then if you try to use the class afterwards it doesn't find the class at all but rather it finds the string called name. Is it possible to use some kind of already built in feature to append the needed text here to automatically generate a class within code without having to type it out? I am not sure of how to extract the text from a string to use that as the identifier of the class's name.
Conclusion
From reading the comments and answer provided in my related question; it then proves my initial assumptions that I didn't mention to be true. It can not be done. This does resort into having to write the class to their respective files from the parsers point of view.
Sorry, but this just isn't how C++ works. A compiler understands things like the keyword class and the names of members, and typically puts all that understanding to use to convert all that into machine code, which normally uses just lots of raw pointers and offsets, not the names of members or things like that. Only after the compiler is finished, you run your program, and a typical program does not itself contain much of any capability at all to understand things like classes, member names, or assignment operators.
So what options do you have? You could write some utilities that are capable of doing some of the things a general class would do, but you won't be able to refer to it in the same ways as a class the compiler learned about from a header file within your program. This might look something like:
{
CustomClass myclass( "Foo.cls" );
CustomObject obj = myclass.create(); // default constructor
CustomObject obj_copy = obj; // copy constructor
}
Or you might do something that compiles and loads plugins at runtime, though there are a few complications to attempting that. This still doesn't let you use the custom class like one compiled into your program, but it allows arbitrary C++ code (which is both powerful and dangerous, since someone could accidentally or maliciously break just about anything that way). This would involve converting your configuration file to an actual temporary C++ source file you write out, running the compiler with special options to create a "shared library" (Unix, including Linux) or "DLL" (Windows), then loading that library using dlopen (many Unix flavors including Linux) or LoadLibrary (Windows). And for the compile step, this means any computer where you run your program needs the compiler installed, and it should probably be a reasonably close version to the compiler version you used to compile your program. If this compiler is on a special path, how will your program be told that path?
Plus you would need to design a plugin architecture, considering things like:
How does the plugin interface with the program? Does it provide a function that just inputs and outputs vectors of strings? Does it provide a function to create a class object derived from some abstract base class?
If the plugin needs to use any base classes or specialized functions specific to your program, how will you make sure the needed header files are provided to the compiler when compiling the plugin?
I'm looking for a way to parse a C structure in order to get the the name and the type of the variables.
For example I have a structure like this:
struct MyStruct {
int anInt ;
float aFloat ;
}
I need to get the types int and float and the 2 strings "anInt" and "aFloat".
After I have to use these values in another function:
addValue<int>("anInt") ;
add Value<float>("aFloat") ;
Do you know how to do this automatically, I guess, at compilation ?
Thanks.
You probably cannot do that with standard C++11 template code.
You could consider customizing your C++ compiler to get such information. For example, if compiling your code with a recent GCC, you might consider customizing it with your MELT extension (MELT is a domain specific language, implemented by a plugin, to customize GCC). You'll need to understand the details of GCC internal representation (so such an extension might take a week or more of your time).
The Qt MOC facility might perhaps be useful or inspirational. It is parsing a limited form of class declaration.
Alternatively you might consider generating the C++ struct or class representation from some other input; e.g. change your build procedure, perhaps your Makefile, to generate some .h header file (and perhaps some .cc C++ translation unit) from your higher level representation.
class SomeClass
{
//some members
MemberClass one_of_the_mem_;
}
I have a function foo( SomeClass *object ) within a dll, it is being called from an exe.
Problem
address of one_of_the_mem_ changes during the time the dll call is dispatched.
Details:
before the call is made (from exe):
'&(this).one_of_the_mem_' - `0x00e913d0`
after - in the dll itself :
'&(this).one_of_the_mem_' - `0x00e913dc`
The address of object remains constant. It is only the member whose address shift by c every time.
I want some pointers regarding how can I troubleshoot this problem.
Code :
Code from Exe
stat = module->init ( this,
object_a,
&object_b,
object_c,
con_dir
);
Code in DLL
Status_C ModuleClass( SomeClass *object, int index, Config *conf, const char* name)
{
_ASSERT(0); //DEBUGGING HOOK
...
Update 1:
I compared the Offsets of members following Michael's instruction and they are the same in both cases.
Update 2:
I found a way to dump the class layout and noticed the difference in size, I have to figure out why is that happening though.
linked is the question that I found to dump class layout.
Update 3:
Final Update : Solved the problem, much thanks to Michael Burr.
it turned out that one of the build was using 32 bit time, _USE_32BIT_TIME_T was defined in it and the other one was using 64 bit time. So it generated the different layout for the object, attached is the difference file.
Your DLL was probably compiled with different set of compiler options (or maybe even a slightly different header file) and the class layout is different as a result.
For example, if one was built using debug flags and other wasn't or even if different compiler versions were used. For example, the libraries used by different compiler versions might have subtle differences and if your class incorporates a type defined by the library you could have different layouts.
As a concrete example, with Microsoft's compiler iterators and containers are sensitive to release/debug, _SECURE_SCL on/off , and _HAS_ITERATOR_DEBUGGING on/off setting (at least up though VS 2008 - VS 2010 may have changed some of this to a certain extent). See http://connect.microsoft.com/VisualStudio/feedback/details/352699/secure-scl-is-broken-in-release-builds for some details.
These kinds of issues make using C++ classes across DLL boundaries a bit more fragile than using straight C interfaces. They can occur in C structures as well, but it seems like C++ libraries have these differences more often (I think that's the nature of having richer functionality).
Another layout-changing issue that occurs every now and then is having a different structure packing option in effect in the different compiles. One thing that can 'hide' this is that pragmas are often used in headers to set structure packing to a certain value, and sometimes you may come across a header that does this without changing it back to the default (or more correctly the previous setting). If you have such a header, it's easy to have it included in the build for one module, but not another.
that sounds a bit wierd, you should show more code, it should 'move' if it being passed by ref, it sounds more like a copy of it is being made and that having the member function called.
Perhaps the DLL versions is compiled against a different version that you are referencing. check and make sure the header file is for the same version as the dll.
Recompile the library if you can.
The following is the situation. There is a system/software which is completely written in C. This C program spawns a new thread to start some kind of a data processing engine written in C++. Hence, the system which I have, runs 2 threads (the main thread and the data processing engine thread). Now, I have written some function in C which takes in a C struct and passes it to the data processing thread so that a C++ function can access the C struct. While doing so, I am observing that the values of certain fields (like unsigned int) in the C struct changes when being accessed in the C++ side and I am not sure why. At the same time, if I pass around a primitive data type like an int, the value does not change. It would be great if someone can explain me why it behaves like this. The following is the code that i wrote.
`
/* C++ Function */
void DataProcessor::HandleDataRecv(custom_struct* cs)
{
/*Accesses the fields in the structure cs - an unsigned int field. The value of
field here is different from the value when accessed through the C function below.
*/
}
/*C Function */
void forwardData(custom_struct* cs)
{
dataProcessor->HandleDataRecv(cs); //Here dataProcessor is a reference to the object
//of the C++ class.
}
`
Also, both these functions are in different source files(one with .c ext and other with .cc ext)
I'd check that both sides layout the struct in the same
print sizeof(custom_struct) in both languages
Create an instance of custom_struct in both languages and print the offset of
each member variable.
My wild guess would be Michael Andresson is right, structure aligment might be the issue.
Try to compile both c and c++ files with
-fpack-struct=4
(or some other number for 4). This way, the struct is aligned the same in every case.
If we could see the struct declaration, it would probably clearer. The struct does not contain any #ifdef with c++-specific code like a constructor, does it? Also, check for #pragma pack directives which manipulate data alignment.
Maybe on one side the struct has 'empty bytes' added to make the variables align on 32 bit boundaries for speed (so a CPU register can point to the variable directly).
And on the other side the struct may be packed to conserve space.
(CORRECTION) With minor exceptions, C++ is a superset of C (meaning C89), So i'm confused about what is going on. I can only assume it has something to do with how you are passing or typing your variables, and/or the systems they are running on. It should, technically speaking, unless I am very mistaken, have nothing to do with c/c++ interoperability.
Some more details would help.