struct question

A

Allan M. Bruce

I attended an interview yesterday for a programming job and was asked to
create an implementation of a struct using as little memory as possible. I
had one struct defined as follows:

typedef struct tagFoo
{
struct entry *bar;
} foo;

However, they wanted me to be able to use the following code to create a
strct foo.

foo x = NULL;

They did show me how it was done but now I cannot remember the syntax, it
was something like:

typedef struct entry *bar foo;

Can anybody shed some light on what this should be?
Thanks
Allan
 
K

Keith Thompson

Allan M. Bruce said:
I attended an interview yesterday for a programming job and was asked to
create an implementation of a struct using as little memory as possible. I
had one struct defined as follows:

typedef struct tagFoo
{
struct entry *bar;
} foo;

However, they wanted me to be able to use the following code to create a
strct foo.

foo x = NULL;

They did show me how it was done but now I cannot remember the syntax, it
was something like:

typedef struct entry *bar foo;

Can anybody shed some light on what this should be?

If the declaration
foo x = NULL;
is portably legal, then foo must be a pointer type. (Note that hiding
a pointer type behind a typedef is almost always a bad idea, and has
nothing to do with memory usage.)

I can't tell from your description what they were really asking for.
 
B

Barry Schwarz

I attended an interview yesterday for a programming job and was asked to
create an implementation of a struct using as little memory as possible. I
had one struct defined as follows:

typedef struct tagFoo
{
struct entry *bar;
} foo;

What does the word implementation mean? Since you talk about using
memory, it must be a definition, not merely a declaration. What you
have above merely declares a structure type and an alias for that
type.
However, they wanted me to be able to use the following code to create a
strct foo.

foo x = NULL;

This defines a pointer but does not define or allocate space for a
structure.
They did show me how it was done but now I cannot remember the syntax, it
was something like:

typedef struct entry *bar foo;

Again, this is only a declaration, not a definition, and therefore is
unrelated to memory usage.
Can anybody shed some light on what this should be?

I don't know what they were looking for but you might prefer to work
for a company that knows what they are doing as opposed to this one.

I cannot think of any structure that uses less memory than
struct tag_s{char x;}s;
I would expect on most systems that sizeof s would be 1.


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top