Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is the memory argv points to writable ?

1 view
Skip to first unread message

spi...@gmail.com

unread,
Jun 24, 2006, 4:30:16 PM6/24/06
to
Is the memory argv points to writable ? In other words
is it ok if the programme contains
argv[i] = pointer_to_char where i<argc ?
Same question for *argv[i] = some_char

Spiros Bousbouras

Richard Heathfield

unread,
Jun 24, 2006, 4:43:24 PM6/24/06
to
spi...@gmail.com said:

argv itself is a local object, so you can do this if you like:

argv = newval;

and the runtime system won't even notice.

argv[0] through argv[argc] must not be changed. (This gives implementations
a certain amount of flexibility - for example, it gives them the freedom to
set up the storage for the strings via malloc if they choose, knowing they
can free them again at the end.)

For each argv[n] where n >= 0 && n < argc, you can write to the characters
starting with argv[n][0] and going no further than the null terminator.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Bill Pursell

unread,
Jun 25, 2006, 3:03:01 AM6/25/06
to

Richard Heathfield wrote:
> spi...@gmail.com said:
>
> > Is the memory argv points to writable ? In other words
> > is it ok if the programme contains
> > argv[i] = pointer_to_char where i<argc ?
> > Same question for *argv[i] = some_char
>
> argv itself is a local object, so you can do this if you like:
>
> argv = newval;
>
> and the runtime system won't even notice.
>
> argv[0] through argv[argc] must not be changed. (This gives implementations
> a certain amount of flexibility - for example, it gives them the freedom to
> set up the storage for the strings via malloc if they choose, knowing they
> can free them again at the end.)
>
> For each argv[n] where n >= 0 && n < argc, you can write to the characters
> starting with argv[n][0] and going no further than the null terminator.

Which is clear if you prototype main as int main(int argc, char
*const*argv),
plus you'll get a compiler warning if you try to write the values.

Richard Heathfield

unread,
Jun 25, 2006, 3:21:55 AM6/25/06
to
Bill Pursell said:

>
> Richard Heathfield wrote:
>>
>> argv itself is a local object, so you can do this if you like:
>>
>> argv = newval;
>>
>> and the runtime system won't even notice.
>>
>> argv[0] through argv[argc] must not be changed. (This gives
>> implementations a certain amount of flexibility - for example, it gives
>> them the freedom to set up the storage for the strings via malloc if they
>> choose, knowing they can free them again at the end.)
>>
>> For each argv[n] where n >= 0 && n < argc, you can write to the
>> characters starting with argv[n][0] and going no further than the null
>> terminator.
>
> Which is clear if you prototype main as int main(int argc, char
> *const*argv),
> plus you'll get a compiler warning if you try to write the values.

That's fine, provided your implementation documents that form for main().
Otherwise, ISTM that you are invoking undefined behaviour.

Bill Pursell

unread,
Jun 25, 2006, 4:25:07 AM6/25/06
to

Richard Heathfield wrote:
> Bill Pursell said:
> > Richard Heathfield wrote:

> >> For each argv[n] where n >= 0 && n < argc, you can write to the
> >> characters starting with argv[n][0] and going no further than the null
> >> terminator.
> >
> > Which is clear if you prototype main as int main(int argc, char
> > *const*argv),
> > plus you'll get a compiler warning if you try to write the values.
>
> That's fine, provided your implementation documents that form for main().
> Otherwise, ISTM that you are invoking undefined behaviour.

I didn't realize that. For a while, I've been thinking that I would
stop writing main functions completely and spend the time
figuring out the python interface so I can write my processing
in C and do all the argument parsing and process manipulation
in python, and this fact is a tick in the column in favor of doing
that.
But, it doesn't seem like it's worth the effort. The ability to
manipulate
strings easily is hardly worth the work it would take to get the
interface
right.

Herbert Rosenau

unread,
Jun 26, 2006, 1:31:07 AM6/26/06
to

Yes. For *argv[i] it is true until you does not write a longer string
as the original one.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!

Joe Wright

unread,
Jun 26, 2006, 7:25:02 PM6/26/06
to
Herbert Rosenau wrote:
> On Sat, 24 Jun 2006 20:30:16 UTC, spi...@gmail.com wrote:
>
>> Is the memory argv points to writable ? In other words
>> is it ok if the programme contains
>> argv[i] = pointer_to_char where i<argc ?
>> Same question for *argv[i] = some_char
>
> Yes. For *argv[i] it is true until you does not write a longer string
> as the original one.
>
The crt provides 'int argc' and 'char *argv[]' to us and gives us access
to all the argv pointers and access to their strings.

But, we don't know how long the strings are (We'd have to check). We
don't know that the strings occupy contiguous memory(We'd have to check).

The fact that this memory alluded to by argv is all writable (it is)
should not invite the C programmer to actually use and modify it.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

0 new messages