What is forward reference in C?

2019-01-23 08:22发布

What is forward reference in C with respect to pointers?

Can I get an example?

6条回答
做个烂人
2楼-- · 2019-01-23 09:03

Forward references allow C compiler to do less passes and significantly reduces compilation time. It is probably was important some 20 years ago when computers was much slower and compliers less efficient.

查看更多
姐就是有狂的资本
3楼-- · 2019-01-23 09:08

See this page on forward references. I don't see how forward referencing would be different with pointers and with other PoD types.

Note that you can forward declare types, and declare variables which are pointers to that type:

struct MyStruct;
struct MyStruct *ptr;
struct MyStruct var;  // ILLEGAL
ptr->member;  // ILLEGAL

struct MyStruct {
    // ...
};

// Or:

typedef struct MyStruct MyStruct;
MyStruct *ptr;
MyStruct var;  // ILLEGAL
ptr->member;  // ILLEGAL

struct MyStruct {
    // ...
};

I think this is what you're asking for when dealing with pointers and forward declaration.

查看更多
Luminary・发光体
4楼-- · 2019-01-23 09:09

I think the C compiler originally had a pass in which it did symbol table building and semantic analysis together. So for example:

    ....
    ... foo(a,b) + 1 ... // assumes foo returns int
    ....

    double foo(double x, double y){ ... } // violates earlier assumption

to prevent this, you say:

    double foo(double x, double y); // this is the forward declaration

    ....
    ... foo(a,b) + 1 ... // correct assumptions made
    ....

    double foo(double x, double y){ ... } // this is the real declaration

Pascal had the same concept.

查看更多
三岁会撩人
5楼-- · 2019-01-23 09:11

Adding to previous answers. The typical situation in which forward reference is mandatory is when a struct foo contains a pointer to a struct bar, and bar contains a pointer to foo (a circular dependency between declarations). The only way to express this situation in C is to use a forward declaration, i.e.:

struct foo;

struct bar
{
   struct foo *f;
};

struct foo
{
   struct bar *b;
};
查看更多
Melony?
6楼-- · 2019-01-23 09:14

Forward reference is when you declare a type but do not define it.

It allows you to use the type by pointer (or reference for C++) but you cannot declare a variable.

This is a way to say to the compiler that something exists

Say that you have a Plop structure defined in Plop.h:

struct Plop
{
   int n;
   float f;
};

Now you want to add some utility functions that works with that struct. You create another file PlopUtils.h (let's say you can't change Plop.h):

struct Plop; // Instead of including Plop.h, just use a forward declaration to speed up compile time

void doSomething(Plop* plop);
void doNothing(Plop* plop);

Now when you implement those function, you will need the structure definition, so you need to include the Plop.h file in your PlopUtils.cpp:

#include "PlopUtils.h"
#include "Plop.h" // now we need to include the header in order to work with the type

void doSomething(Plop* plop)
{
   plop->n ...
}

void doNothing(Plop* plop);
{
   plop->f ...
}
查看更多
聊天终结者
7楼-- · 2019-01-23 09:20

I think "forward reference" with respect to pointers means something like this:

struct MyStruct *ptr; // this is a forward reference.

struct MyStruct
{
  struct MyStruct *next; // another forward reference - this is much more useful
  // some data members
};

The pointer is declared before the structure it points to is defined.

The compiler can get away with this because the pointer stores an address, and you don't need to know what is at that address to reserve the memory for the pointer.

查看更多
登录 后发表回答