Aliased arguments in strtol
Asked Answered
Q

1

6

Here is how strtol has to be declared according to § 7.22.1.4 from C11 (n1570):

#include <stdlib.h>

long int strtol (const char *restrict nptr,
                 char **restrict endptr,
                 int base);

As far as I know, the restrict keyword means that the object referenced by the lvalue *nptr will be accessed only with it or a value directly derived from it.

However, a lot of programmers, and even experienced ones, use strtol in the following way:

#include <stdlib.h>

strtol (p, &p, 10);

In that case, **endptr == **&p == *p == *nptr, and the behavior is undefined. Is it right?

Quicksand answered 14/2, 2013 at 16:39 Comment(1)
A while ago I wrote a blog post on the topic that restrict is frustrating as an element of specification because it implicitly refers to the implementation. If a strtol() implementation accesses both **endptr and *nptr then it is undefined to pass p and &p to it. If it doesn't, then it is not undefined to pass these arguments. The specification implied by restrict only makes sense with respect to the very implementation that should be hidden by the specification. blog.frama-c.com/index.php?post/2012/08/02/restrict-not-modularLast
I
8

No. Nothing is accessed via **endptr in strtol. Only *endptr, a completely separate object, is accessed.

Ivan answered 14/2, 2013 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.