|
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() .
|
|
|
|
|
Richard is right, the expression in the push_back call is quite complex. You didn't show the inverse implementation and the operator * one.
You know, in order to find the actual problem it could be useful to refactor a bit the code, e.g.:
Matrix FMA = F * MA.inverse();
Matrix BAFMA = BA * FMA;
E.push_back(BAFMA);
|
|
|
|
|
I did already do what you suggest, and replaced the matrix operation with a single Matrix value into the push_back. Since it didn't make a difference I did not include it here. However I am certain the the matrix operation I show does return a valid matrix -- ten year old code that has worked without issue before.
|
|
|
|
|
Still, the vector push_back method is more tested. I see your code performs many operations. Could you reduce them ata the bare minimum still causing the error to occur? Then you could post such code in order to allow us to test it independently.
|
|
|
|
|
It is still not clear what error or exception causes the crash. Is it the data being passed to the call to push_back , or is the vector reference being corrupted in some way? You could try adding a try/catch block, or some logging messages around the point of the crash.
|
|
|
|
|
 It is reasonable that I am being asked about my Matrix class and its functions. However, since I have been using it for over 10 years, I am fairly confident that its bugs have been worked out. The matrix multiplication and inversion functions produce the results that should be expected. So, I really do not think the problem is there, and asking you to examine that voluminous code would probably not be useful.
If it helps, I tried the program written this way (differences on the last 7 lines):
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);
Matrix er;
er=BA * F * MA.inverse();
cout << i << endl;
er.dump();
E.push_back(er);
if (angle<minangle) minangle=angle;
if (angle>maxangle) maxangle=angle;
}
Again, the program can run several times without apparent error. When the error does occur it will even go several times through the for loop before it crashes. In the format I am giving here, it will print out the iteration of the loop and the matrix that was provided to the E vector (which should be pretty close to a 4x4 identity matrix).
The program just stops. No error messages are given. I've tried throw-catch around it, but I don't know what to catch here other than ..., and that doesn't work.
In my experience, odd behavior like this is due to a memory leak. But, the only place I can think of where memory is leaking would be if the Matrix destructor is not being called when the E (or other similar vectors like F3) goes out of scope.
Am I correct in this suspicion? If so, how do I invoke an array destructor before the vector of arrays goes out of scope?
I know that many of you want me to re-write the Matrix class with vectors instead of dynamic arrays. That may be the only correct ultimate solution to my problem. But right now, this is also an academic question about the proper use of a vector of arrays.
And, of course, if the vector of arrays issue is not my problem, then I am really lost and in greater need of any help that can be offered.
Thanks for the efforts so far, and for any efforts that appear in the future to help me solve this thing.
|
|
|
|
|