Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Confusion with fstat (int, struct stat *) 1

Status
Not open for further replies.

areza123

Programmer
Jul 4, 2002
60
IN
I have some doubts regarding the use of fstat(). The function signature described in ..\VC98\Include\Sys\stat.h is

_CRTIMP int __cdecl fstat(int, struct stat *);

The confusion is that in my c++ program if I pass a local (stack) variable declared as
struct stat st;
int result = fstat(fd,&st);
it compiles.
1. But if I try allocating from the heap using
struct stat *st = new stat;
I get a compilation error error C2061: syntax error : identifier 'stat'
What is happening here ?

2. why is the struct keyword necessary here ? If I remove the struct keyword and just use
stat st;
int result = fstat(fd,&st);
I get the error error C2146: syntax error : missing ';' before identifier 'st'

3. Could somebody explain what the signature
_CRTIMP int __cdecl fstat(int, struct stat *);
means. Specifically how will I know from this signature that the 2nd parameter I pass should be
on the stack or the heap ? And also whether I need to delete this if it is on the heap ?

 
_CRTIMP is used to determine if crtlib is linked as .lib or .dll. Not important in your case.

I think that the problem is with using "new" here, it's a C library, and compiler probably won't digest that! You should try 2 things :

struct stat* st = new struct stat;

or

struct stat* st =
(struct stat*)malloc(sizeof(struct stat));

Btw, you need struct keyword, because it's a "C" library, and "stat" is just a tag, not a typename.
 
Yes struct stat* st = new struct stat;
works perfectly...but can someone explain whats happening ? I declare another structure as:

struct test {
int test1;
};

There is only a tag name - no typename, why does the line:

test *ptr_test = new test;

compile perfectly without the struct keyword in this case ?
I also tried C linkage using extern C by placing the structure in a header file and including as extern "C" { #include ...}. Still test *ptr_test = new test;
It works without the struct key word. Am I doing something wrong? What about answers to other question please ? I am compiling using the nmake utility of VC 6.0

Thx / Ali
 
OK, here we go :

in C++ keywords "class" and "struct" are essentially the same thing. So, in C++ code like this :

struct some_struct
{
...
};

some_struct* mystruct = new some_struct;

is perfectly fine! But, in C, with the code above, you are only defining a tag, not a REAL typename, since struct is not defining a typename in C. If you use a C-compilation, than the previous code could be :

typedef struct
{
...
}some_struct;

and that trick would solve the problem with using additional "struct" keyword everywhere!

However, your "stat" structure is defined in extern"C" block, and will always be treated as C-struct. Do your self a favour and write something like : typedef struct _stat MyStat; and then keep using that.

As for your last question, it doesn't matter is your struct on stack or on heap, since fstat will just fill the structure, and it's not responsible for creating/destroying. It's up to you what to do with that!
 
OK...I declare my own structure as
extern "C" {
struct test_str {
int a;
};
}
As per the above explanation
test_str *ptr1 = new test_str; // (1)

should not work. Instead
struct test_str *ptr2 = new struct test_str; // (2)
should work.
But both (1) & (2) work. Why ?
 
Well, this is kind of a interesting question :)
It really depends on where/how things are declared, and how compiler treats them (as C or C++)!
If you declare extern"C" in your C++ file, compiler will use this information apparently only for linking, i.e. it won't mangle class/functions names but instead export them un-mangled! Every compiler/linker has a different way of doing a name-mangling. If you don't know what I mean look at any .lib file and you will see your function name with lot of meanningless characers infront. This way, you can compile your C-lib/dll with C++ compiler, but can link it later with code created with C-compiler.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top