How many levels of pointers can you have?

J

Joachim Schmitz

madhawi said:
This question is occur in interview. Please help me.
What question?

Ahh, in the Subject... don't hide it there.
How many levels of pointers can you have?
no chapter and verse, but I believe that is implementatiion defined.
 
B

Ben Pfaff

madhawi said:
Subject: How many levels of pointers can you have?

This question is occur in interview. Please help me.

Twelve:

5.2.4.1 Translation limits
1 The implementation shall be able to translate and execute at
least one program that contains at least one instance of
every one of the following limits:13)
[...]
- 12 pointer, array, and function declarators (in any
combinations) modifying an arithmetic, structure, union,
or incomplete type in a declaration
 
S

siju

This question is occur in interview. Please help me.

answer of course is implementation specific. if my memory is correct i
think it is 71 levels of indirection in borland turbo c++ version 3.0
compiler.
 
K

Kenneth Brody

Ben said:

"At least 12."
5.2.4.1 Translation limits
1 The implementation shall be able to translate and execute at
least one program that contains at least one instance of
every one of the following limits:13)
[...]
- 12 pointer, array, and function declarators (in any
combinations) modifying an arithmetic, structure, union,
or incomplete type in a declaration

Now _there's_ a question for the standards committee... Where did
they pick the number 12 from?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
B

Ben Pfaff

Kenneth Brody said:
Now _there's_ a question for the standards committee... Where did
they pick the number 12 from?

One member wanted 8, another wanted 16, they compromised on 12?
 
R

Richard Heathfield

Ben Pfaff said:
One member wanted 8, another wanted 16, they compromised on 12?

No no no. It's a committee! One member wanted 16, another wanted 16,
another wanted 16, and quite a few more wanted 16, and everybody else
also wanted 16, so they compromised on 12.
 
K

Kira

Kenneth Brody said:
"At least 12."

So yeah, 12 is safe everything above that is implementation defined.
I wonder if anybody ever made a non toy program with 12 levels of pointers.
 
C

Chris Dollin

Kenneth said:
Ben said:

"At least 12."
5.2.4.1 Translation limits
1 The implementation shall be able to translate and execute at
least one program that contains at least one instance of
every one of the following limits:13)
[...]
- 12 pointer, array, and function declarators (in any
combinations) modifying an arithmetic, structure, union,
or incomplete type in a declaration

Now _there's_ a question for the standards committee... Where did
they pick the number 12 from?

Maybe all the compilers allowed "as many as you like (until we
run out of store)", except one that had a 4-bit field for
"number of indirections" in a type with the top three values
reserved for "struct", "union", or "function pointer".
 
J

John Cochran

Kenneth said:
Ben said:
Subject: How many levels of pointers can you have?

This question is occur in interview. Please help me.

Twelve:

"At least 12."
5.2.4.1 Translation limits
1 The implementation shall be able to translate and execute at
least one program that contains at least one instance of
every one of the following limits:13)
[...]
- 12 pointer, array, and function declarators (in any
combinations) modifying an arithmetic, structure, union,
or incomplete type in a declaration

Now _there's_ a question for the standards committee... Where did
they pick the number 12 from?

Maybe all the compilers allowed "as many as you like (until we
run out of store)", except one that had a 4-bit field for
"number of indirections" in a type with the top three values
reserved for "struct", "union", or "function pointer".

I have a vague memory from many years back about what I believe was a
Honeywell mainframe (36 bit words, 18 bit addressing). One of the features
of this beast was indirect addressing where if a bit was set in a word
when it was accessed, it indicated that the word should be used as an
address pointing to where the actual parameter was. And if *that* had that
magic bit set, then it was also an address pointing to where the actual
parameter was. This indirection could in theory go quite a distance. However
the computer would throw an exception if too many levels of indirection
were being used. I don't remember how many levels this was.

But given that the above feature could be used to implement pointer to pointer
to ..... in a fairly efficient C implementation. I can see a "must be able
to do at least X levels of indirection" requirement based upon the limits
of the computers with this capability.

Now this is pure speculation, but I wouldn't be surprised if there exists
a modern computer that has this indirect capability and has a limit on
how many levels of indirection it's willing to perform without causing
an exception.
 
R

Richard Tobin

John Cochran said:
I have a vague memory from many years back about what I believe was a
Honeywell mainframe (36 bit words, 18 bit addressing). One of the features
of this beast was indirect addressing where if a bit was set in a word
when it was accessed, it indicated that the word should be used as an
address pointing to where the actual parameter was. And if *that* had that
magic bit set, then it was also an address pointing to where the actual
parameter was. This indirection could in theory go quite a distance. However
the computer would throw an exception if too many levels of indirection
were being used. I don't remember how many levels this was.

But given that the above feature could be used to implement pointer to pointer
to ..... in a fairly efficient C implementation. I can see a "must be able
to do at least X levels of indirection" requirement based upon the limits
of the computers with this capability.

I doubt C has ever been implemented on that architecture by setting
the relevant bit in pointers. If we have char *****p, then both **p
and ****p are legal expressions: the number of dereferences is
controlled by the program, not the data. C doesn't have a
"dereference as much as you can" operator.
Now this is pure speculation, but I wouldn't be surprised if there exists
a modern computer that has this indirect capability and has a limit on
how many levels of indirection it's willing to perform without causing
an exception.

I *would* be surprised if there was such a modern computer.

In any case, the limit in C89 - which is a limit on types, not
dereferencing of objects - most likely just reflects how some common
existing implementation handled declarations.

-- Richard
 
K

Keith Thompson

Ben Pfaff said:
madhawi said:
Subject: How many levels of pointers can you have?

This question is occur in interview. Please help me.

Twelve:

5.2.4.1 Translation limits
1 The implementation shall be able to translate and execute at
least one program that contains at least one instance of
every one of the following limits:13)
[...]
- 12 pointer, array, and function declarators (in any
combinations) modifying an arithmetic, structure, union,
or incomplete type in a declaration

Yes, but like all the limits in 5.2.4.1, it doesn't necessarily mean
very much. A conforming implementation is merely required to
translate and execute *one* program that hits all the listed limits.
Another program with 12 levels of pointers might fail to compile.

The point of the requirement, I think, is that the easiest way to
satisify it is not to have any fixed limits at all, by making the
relevant data structures within the compiler dynamic. A typical
compiler most likely won't complain about 13, or 20, or 99 levels of
pointers (unless it issues a warning).
 
G

George Neuner

Now _there's_ a question for the standards committee... Where did
they pick the number 12 from?

I think they just doubled the 6 guaranteed by K&R. Studies showed
that some complicated code used 6 or more indirections but that
virtually no code used more than 10.

George
 
M

Malcolm McLean

madhawi said:
This question is occur in interview. Please help me.
You might know you might not.
The point is that the standard imposes some limit which is way above the
number of dereferences any paractical programmer would ever need. All
computers will eventually run out of memory if you try to impose a stupidly
large number, even if it is 4 billion.

As it stands it is rather a bad question. It tells you who knows every
irrelevant detail of the stnadard, but that only has a slight correlation to
broader abilities. However remember that the interviewer isn't really
interested in technical ability - he knows that from your CV - he wants to
find out what sort of person you are. So given that you don't know, how do
you handle that? If you can do it in a graceful and relaxed manner, but not
too arrogant - they are looking for teamworking skills, aka obedience, after
all - then you should do well.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top