|
I guess this won't be the reply you expect or want, but …
In the early 1980s, when the C++ language was first being developed at Bell Labs, the first (and for a long time the only) "compiler" for C++ was a tool called cfront . It converted C++ source code into valid C source code (for which there were already a gazillion compilers, of course) The cfront compiler was actually maintained for over a decade, ending with the release of version 3.0.3 (the last official version of which I am aware) in May 1994.
Your current project (as I understand it), is to convert some existing C++ source code (that allegedly works correctly) into C source code (presumably also with the works correctly option). I suspect the ultimate target is a platform for which no C++ compiler is available.
If the original C++ source that you are porting does NOT make extensive use of some of the more modern additions to the language, then you might consider running cfront on the original C++ source. I am not suggesting that it should replace your manual porting efforts to date, but to act merely as your assistant in a purely advisory capacity.
The last version of cfront was released around the time that the C++ language added templates (which are implemented) and exceptions (which are not implemented). If you are interested, you can download cfront or browse the source code courtesy of the Software Preservation Group (which is part of the Computer History Museum in Mountain View, California (Silicon Valley).
You can either download cfront version 3.0.3 from May 1994 (as a gzip'd tar archive), or as an alternative if you prefer, you can browse the source tree (in a new browser window).
|
|
|
|
|
ChrisFromWales wrote: The last version of cfront was released around the time that the C++ language added templates
[...]
cfront version 3.0.3 from May 1994 [...]
This can't be right. I remember programming templates around ~1988 when I was at university, working with PHIGS[^] . cfront did require multiple passes to correctly translate some templates, and was restricted more than the language specification would allow in theory. But for the most part it could handle templates back then.
Since there was no C++ standard in existence, it's hard to say at what time, exactly, cfront supported templates fully though: it led a catch-up races with the C++ designers who kept introducing new features.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Hi!
I am having difficulty processing the following problem.
modified 25-Apr-19 3:55am.
|
|
|
|
|
In what part exactly (1, 2a, 2b, 2c, 3) do you have "difficulty"?
What are the types of "difficulty"?
|
|
|
|
|
Quote: The purpose of this Task is to let you express your problem-solving skills, programing skills, as well as to reveal your style of coding. The question is clear, it is to test your skills, not the skills of some strangers on the internet. Show what you have tried, explain what you are having a problem with, and people will try to help you. But no one is going to do your work for you.
|
|
|
|
|
Hi,
I want to convert the following C++ code into C-language.
For the following code,
<pre> ifstream inFile;
inFile >> num_rows;
file_buffer.resize(num_rows);
I wrote:
FILE* inFile;
inFile = fopen(argv[1], "r");
fgets(strNum_rows, 20, inFile);
num_rows = atoi(strNum_rows);
But I can't understand how to declare file_buffer and how to convert :
vector<double> file_buffer;
file_buffer.resize(num_rows);
in 'C' language?
Somebody please guide me.
Zulfi.
|
|
|
|
|
Is file_buffer a vector ? If so, you could have something like:
int *file_buffer = malloc(num_rows * sizeof(int)); Note: your fgets() call could be the source of an issue. It'll read 19 characters, or until an EOF or newline character is encountered. If it reads too many, then any subsequent fgets() calls will start at the wrong spot in the file stream.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Hi,
Thanks for your response. God bless you.
Yes, file_buffer is a vector. Thanks for your comments about fegts(). I am reading from a file which has following contents:
22
1111.909
12.3456
0.000345
and so on.
First line is a integer and tells the count of data in the file.
Okay if I have somehing like:
send_buffer = new double[num_rows];
Then its equivalent C-code would be:
send_buffer = (double *)malloc(num_rows * sizeof(double);
Please let me know.
Zulfi.
|
|
|
|
|
zak100 wrote:
Please let me know. Looks okay from my vantage point. Have you tried it?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
There is one slight difference which may or may not be important in some situations.
C++ new usually zeros the allocated memory
In C you can use calloc for that it has the format
void *calloc(size_t nitems, size_t size)
So you can actually remove the multiply because you have the number of items and the size of each item
send_buffer = (double *)calloc(num_rows, sizeof(double));
Now it allocates and zeros the allocation.
In vino veritas
|
|
|
|
|
What is the role of preprocessor directives in c program? What if, we don't use it?
|
|
|
|
|
Preprocessor are executed before compilation. This is a macro processor, which is used automatically by the C compiler to transform your program before actual compilation.
In simple words, preprocessor directives tells the compiler to preprocess the source code before compiling. All the preprocessor commands are begin with "#" symbol.
The most common use of the preprocessor is to include header files. In C and C++, all symbols must be declared in a file before they can used. They don’t always need to be defined*, but the compiler needs to know they exist somewhere. A preprocessor is just another technique to help a programming language be more useful. There are numerous techniques available and every language designer must choose the ones they like.
|
|
|
|
|
Aakashdata wrote: What if, we don't use it? Depending on which one(s) you don't use, your program may not compile.
|
|
|
|
|
You don't have to use Preprocessor Directives it is entirely up to you they are there to make life easy and are not mandatory at all.
Here is a hello world without a single Preprocessor Directive
#include <stdio.h>
int main (void)
{
printf("hello world\n");
return 0;
}
No Preprocessor Directive was harmed in the making of that code.
In vino veritas
|
|
|
|
|
Umm ... I think you could argue that #include <stdio.h> is, indeed a preprocessor directive. It does, after all tell the pre-processor to include the given file as part of the source text passed to the compiler.
|
|
|
|
|
haha true ... lets try that again
int main (void)
{
volatile int i = 0;
i = i++;
}
Even stopped it optimizing away
In vino veritas
|
|
|
|
|
leon de boer wrote: Even stopped it optimizing away but atill doesn't return the int it declares it will.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Hey it was late only a cup of coffee between good code and bad code !!!
You are right I say return 0 to you
In vino veritas
|
|
|
|
|
As k5054 says, the #include is a preprocessor directive. But, even worse, it contains a plethora of other directives.
|
|
|
|
|
How to use the Google drive, One drive and Dropbox REST api in my console program to download a file from my dropbox shared link
|
|
|
|
|
Those are 3 white papers, not "questions".
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I have a class Matrix that I made years ago and has worked without problems. It looks like this:
class Matrix
{
private:
double *M;
int ROWS, COLUMNS;
public:
Matrix (): M(new double[1]), ROWS(1), COLUMNS(1) { }
Matrix (int R, int C): M(new double[R*C]), ROWS(R), COLUMNS(C) { }
~Matrix() {delete[] M; };
Matrix(const Matrix& m): M(new double[m.ROWS*m.COLUMNS]),
ROWS(m.ROWS), COLUMNS(m.COLUMNS)
{for (int i=0; i<(ROWS*COLUMNS); i++) M[i]=m.M[i]; }
double& operator() (const int R, const int C);
double operator() (const int R, const int C) const;
}
In my new program, I have made use of a vector of this Matrix class, as in
vector<matrix> J;
The problem comes when I add a new Matrix to this vector, as in:
J.push_back(NewMatrix);
The program will run fine 10 or 20 times, but then crash at this line with J.push_back().
I have tried using the vector like a conventional array, where I know the size, and use a for loop, in the form: for (unsigned i=0; i
|
|
|
|
|
Quote: J.push_back(NewMatrix); This could be the problem. It is not clear how do you add Matrix instances to your vector. Could please post the actual code?
By the way you could easily rewrite your Matrix class using std::vector instead of C -like arrays.
|
|
|
|
|
 Here is the code section that actually crashes:
for (unsigned i=0; i<frames; i++)
{
Matrix F(4,4);
double angle;
F=BA.inverse() * Jtertiary.at(i) * MA;
trans(x)=F(x,3); trans(y)=F(y,3); trans(z)=F(z,3);
trans(tertiaryaxis)=0;
if (tertiaryaxis==X)
{
angle=ExtractX(F.inner(), RotationSequence);
F=(Xrot(angle).transpose() * F.inner()).homogeneous();
F(x,3)=0; F(y,3)=trans(y); F(z,3)=trans(z);
F3.at(i)(1)=trans(x); F3.at(i)(2)=trans(y); F3.at(i)(3)=trans(z);
}
else if (tertiaryaxis==Y)
{
angle=ExtractY(F.inner(), RotationSequence);
F=(Yrot(angle).transpose() * F.inner()).homogeneous();
F(x,3)=trans(x); F(y,3)=0; F(z,3)=trans(z);
F3.at(i)(1)=trans(x); F3.at(i)(2)=trans(y); F3.at(i)(3)=trans(z);
}
else if (tertiaryaxis==Z)
{
angle=ExtractY(F.inner(), RotationSequence);
F=(Zrot(angle).transpose() * F.inner()).homogeneous();
F(x,3)=trans(x); F(y,3)=trans(y); F(z,3)=0;
F3.at(i)(1)=trans(x); F3.at(i)(2)=trans(y); F3.at(i)(3)=trans(z);
}
else
throw("ERROR: Tertiary Axis designation invalid for Three Axis Functional Alignment.\n");
F3.at(i)(0)=degrees(angle);
E.push_back(BA * F * MA.inverse());
if (angle<minangle) minangle=angle;
if (angle>maxangle) maxangle=angle;
}
When it crashes it typically crashes at
E.push_back(BA * F * MA.inverse());
E is another vector of Matrix that was passed by reference to this function. My matrix class contains a number of functions that allows for matrix algebra.
I know I could probably re-write the Matrix class to use vectors, but I've been using it for over 10 years (written before I knew about vectors) and I figured if it ain't broke don't fix it. But maybe it is broke, and it just took this long for it to become a problem. If it is broken, can you tell me where?
|
|
|
|
|
T Bones Jones wrote: E.push_back(BA * F * MA.inverse()); This is somewhat more complex than your original post suggested. You need to use the debugger to check the values of BA , F , MA , and whatever is returned by the call to MA.inverse() .
|
|
|
|
|