Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aliased arguments in strtol

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?

like image 228
md5 Avatar asked Feb 14 '13 16:02

md5


People also ask

What is strtol in C with example?

The C library function long int strtol(const char *str, char **endptr, int base) converts the initial part of the string in str to a long int value according to the given base, which must be between 2 and 36 inclusive, or be the special value 0. Declaration. Following is the declaration for strtol() function. Parameters.

How does unset-F work with alias arguments?

Alias arguments are only passed at the end. Note that f is called at the very end of the alias. The unset -f removes the function definition as the alias is executed so it doesn't hang around afterwards.

What happens if strtoll is negative in Python?

If the value is negative, the strtol () function will return LONG_MIN, and the strtoll () function will return LONGLONG_MIN. If no characters are converted, the strtoll () and strtol () functions will set errno to EINVAL and 0 is returned.

What is strict aliasing in C++?

The Strict Aliasing rule is enabled by default on optimization levels 2 and beyond, and when one tries to compile a program with aforementioned details the compiler throws warnings, though the program still compiles and can be executed, the output of such a program remains unpredictable.


1 Answers

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

like image 176
R.. GitHub STOP HELPING ICE Avatar answered Sep 25 '22 03:09

R.. GitHub STOP HELPING ICE