Is the memory argv points to writable ?

S

spibou

Is the memory argv points to writable ? In other words
is it ok if the programme contains
argv = pointer_to_char where i<argc ?
Same question for *argv = some_char

Spiros Bousbouras
 
R

Richard Heathfield

(e-mail address removed) said:
Is the memory argv points to writable ? In other words
is it ok if the programme contains
argv = pointer_to_char where i<argc ?
Same question for *argv = 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.
 
B

Bill Pursell

Richard said:
(e-mail address removed) said:
Is the memory argv points to writable ? In other words
is it ok if the programme contains
argv = pointer_to_char where i<argc ?
Same question for *argv = 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.
 
R

Richard Heathfield

Bill Pursell said:
Richard 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.

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.
 
B

Bill Pursell

Richard said:
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.
 
H

Herbert Rosenau

Is the memory argv points to writable ? In other words
is it ok if the programme contains
argv = pointer_to_char where i<argc ?
Same question for *argv = some_char


Yes. For *argv 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!
 
J

Joe Wright

Herbert said:
Is the memory argv points to writable ? In other words
is it ok if the programme contains
argv = pointer_to_char where i<argc ?
Same question for *argv = some_char


Yes. For *argv 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.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top