2

I have some code I can't quite understand.

typedef double Align;
union header{
    struct{
        union header *ptr;
        unsigned int size;
    }s;
    Align x;
};
typedef union header Header;   

So, after creating this union it's used in a wierd way.

Header *morecore(unsigned);    

This is then called like a normal function

Header *p;
p = morecore(nunits);

How exactly does this work? There is no code anywhere telling how this "function" works.

1
  • We need more information: does this code compiles? does it runs? How are you compiling int? What are you linking against? What is your compiler? Where is the line where morecore is mentioned? This all matters. Please show us more/full code. Commented Jan 2, 2015 at 4:08

2 Answers 2

3
Header *morecore(unsigned);

This function returns a pointer of type Header. So the return value is assigned to the same type which is

Header *p = morecore(nunits);

So basically the function morecore() internally does some operation and returns a pointer and this return value is assigned to p.

Sign up to request clarification or add additional context in comments.

1 Comment

The point is, there is no function morecore declared ANYWHERE! There is no code for it whatsoever, the ONLY places where it is mentioned is "Header *morecore(unsigned)" and "p = morecore(nunits)"
1
Header *morecore(unsigned);

is a forward declaration of a function named morecore which takes 1 parameter of type unsigned and returns a pointer to Header. It is not related to the way how Header was defined. This function is defined somewhere in your code together with its body.

5 Comments

"forward" is redundant. A function declaration is a function declaration - nothing more, nothing less. As you wrote, the implementation might be somewhere else.
@alk: A definition is also a declaration, but I wouldn't call it a forward declaration.
The point is, there is no function morecore declared ANYWHERE! There is no code for it whatsoever, the ONLY places where it is mentioned is "Header *morecore(unsigned)" and "p = morecore(nunits)"
@Chikage If the program is able to compile, link and run over the invocation code "p = morecore(nunits);", then there must be such a function. It may be in a source file, a linked object file or in some library. Other possibility would be that the invocation is in a commented part of code, or part removed by #if directive, or within a branch which is always false and put out by optimizer, something like "if (0) {p = morecore(nunits);}"
@Marian I just managed to find the rest of the code however that was in a book (The C Programming Language) so while there was code for implementing morecore it was NOT in the file nor in any linked directory or anything (unless the compiler managed to find the PDF with the code and somehow use it which surely must be impossible)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.