Re: Using strtod
On Feb 19, 12:15 pm, "Harald van Dijk" <true...@gmail. comwrote:
Ok, so the basic idea is that a macro can access variables directly,
whereas variables are passed by value to functions. Is that right?
On Feb 19, 12:15 pm, "Harald van Dijk" <true...@gmail. comwrote:
coder wrote:
>
>
>
>
>
>
>
Let's use strtol as another example for simplicity, and let's say the
compiler supports GCC-style statement-expressions and C++-style
references as extensions. Then, strtol might be implemented as
something similar to
>
#define strtol(n, end, base) \
({ \
const char * const &__n = n; \
char * &__end = *end; \
int const &__base = base; \
for (__end = __n; *__end != '\0'; __end++) \
if (!isspace(*__en d)) \
break; \
for (; *__end != '\0'; __end++) \
if (!isbasedigit(* __end, __base)) \
break; \
strntol(__n, __end - __n, __base); \
})
>
Consider what would happen when end == &n: the final call to strntol
(a non-standard helper function) would pass unexpected arguments.
>
(Note: this example would need some editing to even be a potential
valid implementation of strtol, but the basic idea stands.)
On Feb 18, 11:54 pm, "Harald van Dijk" <true...@gmail. comwrote:
coder wrote:
On Feb 18, 10:47 pm, "Harald van Dijk" <true...@gmail. comwrote:
coder wrote:
<snip>
On Feb 18, 10:47 pm, "Harald van Dijk" <true...@gmail. comwrote:
coder wrote:
<snip>
Thanks for the reply, Harald.
You are allowed to do this as long as you bypass any possible macro
definition of strtod:
value = (strtod) (p, &p);
definition of strtod:
value = (strtod) (p, &p);
I didn't get that. Can you please explain a little more?
strtod(p, &p) both reads from p and writes to p. If you're using a
function, there's no problem, because the reading from p is guaranteed
to take place before any write. If you're using a macro, the macro is
allowed to write to p before reading its original value.
function, there's no problem, because the reading from p is guaranteed
to take place before any write. If you're using a macro, the macro is
allowed to write to p before reading its original value.
I still don't understand fully. Can you (or someone else) please give
an example to illustrate this?
an example to illustrate this?
Please excuse me for my ignorance.
Let's use strtol as another example for simplicity, and let's say the
compiler supports GCC-style statement-expressions and C++-style
references as extensions. Then, strtol might be implemented as
something similar to
>
#define strtol(n, end, base) \
({ \
const char * const &__n = n; \
char * &__end = *end; \
int const &__base = base; \
for (__end = __n; *__end != '\0'; __end++) \
if (!isspace(*__en d)) \
break; \
for (; *__end != '\0'; __end++) \
if (!isbasedigit(* __end, __base)) \
break; \
strntol(__n, __end - __n, __base); \
})
>
Consider what would happen when end == &n: the final call to strntol
(a non-standard helper function) would pass unexpected arguments.
>
(Note: this example would need some editing to even be a potential
valid implementation of strtol, but the basic idea stands.)
whereas variables are passed by value to functions. Is that right?
Comment