473,326 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

modifying the strings pointed to by argv

The standard allows that we can copy strings onto arg[0], arg[1], etc.
Why is it allowed ?
What can be the maximum length of such a string that is copied ?

Mar 14 '07
56 4381
Mark McIntyre said:
On Fri, 16 Mar 2007 14:19:44 +0000, in comp.lang.c , Richard
Heathfield <rj*@see.sig.invalidwrote:
>>Mark McIntyre said:
>>By this logic, one would never write to character arrays at all.

It's a deal. We're probably still in time for the next Standard - are
you going to write up the DR or shall I? :-)

I'm guessing in that case that there's no logic behind your aversion.
Chuck has encapsulated my logic very well, elsethread.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 16 '07 #51
CBFalconer said:

<snip>
Mine produces <grin:

[1] c:\c\junk>fact 52
Factorial(52) == 2147483648e12 * pow(3,23) * pow(7,8) * pow(11,4) *
pow(13,4) *
pow(17,3) * pow(19,2) * pow(23,2) * pow(29,1) * pow(31,1) *
pow(37,1) * pow(41,1
) * pow(43,1) * pow(47,1) * pow(2,6)
or approximately
80658175170943876840000000000000000000000000000000 0000000000000
00000.

without any fancy bignum arithmetic packages.
It's very sweet of you to call my bignum arithmetic routines "fancy",
but I don't think you'd say that if you saw them. "An ill-favoured
thing, sir, but mine own", as I think the Bard has it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 16 '07 #52
On Mar 16, 7:02 pm, "matevzb" <mate...@gmail.comwrote:
Um, functionality aside, now I know what "typecasting" is =)
Seriously though, why abuse main() in such manner, unless one would
like to apply for IOCCC?
Heh heh, I just looked that up on Wikipedia - I think I should enter
it, I'd be a natural :) I always love slipping in clever little
shortcuts where possible. To be honest I'm really enjoying finding my
feet in C... Basically, I'm going to finish my degree this summer and
after that I want a programming job. Unfortunately in my course we
only learned Java - nothing lower level (no C++, no C, no assembler).
I really hate hand-holding high-level langs like Java, so I've been
giving myself a crash course in C, hoping I'll be programming it
professionally by the end of this year! It's just so liberating -
anything I want to do, it lets me just do. And you've got to love a
language whose only runtime error message is "Segmentation fault"...

Anyway, back to your question - of course I'm not suggesting my
factorial program as a model for a larger program, but I think it's
kind of neat, and a good illustration of the flexibility of a char *.

Mar 16 '07 #53
On Mar 16, 8:20 pm, Chris Torek <nos...@torek.netwrote:
In article <1174064854.830162.87...@n59g2000hsh.googlegroups. com>

<Francine.Ne...@googlemail.comwrote:
The more I think about this thread, the more it seems to me that the
standard is quite broken on this point.

Maybe, from a "very small memory machine" point of view anyway:
I was thinking along these lines: OK, so my program starts, and let's
say I get one command line argument, so argv[0] and argv[1] are both
pointers to strings. Perhaps argv[1] is enormously long! Now I change
it to point to something else. Alarm bells ring - I've just lost all
record of whatever argv[1] was pointing too - boom, potential memory
leak alert.
But on further reflection, even if I don't change argv[1], it's still
as good as a memory leak! Once my program has finishing doing whatever
it wants to do with its command line arguments, there's still never
any way of releasing the memory they occupy.

Maybe not for you, but the implementation could.

Remember that your main() is called, somehow, by the implementation.
Typically this is accomplished with a bit of sneakily-written (and
usually machine-dependent in some way) code that vaguely resembles:

void __start(void) {
register struct __OS_start_info *args __sneaky("%r29");
int status;

... do some stuff with the OS-provided startup info ...
... in the process, set up argc and argv ...

__init_C_library_part_A();
__init_C_library_part_B();
...
__init_C_library_part_P();

status = main(argc, argv);

exit(status); /* __shutdown_C_library_* calls are done from exit() */
/* NOTREACHED */
}
That's interesting! I'd never guessed there was an extra bit of C
slipped in there - I guess I'd assumed the operating system directly
passed control to my main().

So maybe one way forward would be to set up some sort of callback
function - some mechanism for the programmer to signal to whatever can
control argc & argv that it's OK to free them up (I guess that would
mean moving the stack pointer an appropriate amount, from what you
said).
Some of the __init calls may arrange to release the space occupied
by the argv array and strings.
One practical solution would be: not only should each argv[i] be
modifiable, but in fact it should be a pointer allocated (or that
behaves as if it was allocated) by malloc. This would let the
programmer free() the memory taken up by the command line arguments
once she'd finished processing them.

While that might offer some advantage to a program that is running
short of memory otherwise, it would be a big change to existing
implementations, which the "OS-provided startup info" includes
storing the argv array and/or strings (and/or "environment" text
and pointers) in a stack frame that resides just "above" (or
co-incident with, depending on architecture) the frame for __start()
itself.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.

Mar 16 '07 #54
>On Mar 16, 8:20 pm, Chris Torek <nos...@torek.netwrote:
>Remember that your main() is called, somehow, by the implementation. ...
In article <11********************@d57g2000hsg.googlegroups.c om>
<Fr************@googlemail.comwrote:
>That's interesting! I'd never guessed there was an extra bit of C
slipped in there - I guess I'd assumed the operating system directly
passed control to my main().
It may not be C code (it might be hand-coded assembly, for instance,
or written in some other convenient language). It is pretty rare
to get a call straight from the OS, though, if only because the OS
has no idea how to arrange for the proper cleanup calls to happen
if main() returns. (Simply executing the "terminate process" OS
call is not sufficient, since there may be atexit() calls to handle,
stdio buffers to flush, and so on.)
>So maybe one way forward would be to set up some sort of callback
function - some mechanism for the programmer to signal to whatever can
control argc & argv that it's OK to free them up (I guess that would
mean moving the stack pointer an appropriate amount, from what you
said).
This is not really practical on most implementations (at least,
most of the ones I know of). An "I am done with arguments" call
would turn into a no-op. You could still include it, and have it
do nothing on those implementations, but do something on others,
of course.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Mar 26 '07 #55
<Fr************@googlemail.comwrote in message
news:11********************@d57g2000hsg.googlegrou ps.com...
That's interesting! I'd never guessed there was an extra bit of C
slipped in there - I guess I'd assumed the operating system
directly passed control to my main().
That's unlikely, since various languages and object formats will have
different entry mechanisms.

For instance, ELF has a pointer in the header to the function that is called
as an entry point. GCC links in a static hand-coded routine called
_start(), which grabs argc, argv, and environment variables, does certain
other OS-dependent things like setting up stdio, calls main(), and passes
the returned value back to the OS as part of the process tear-down
procedure. The compiler can't secretly insert that work into main() because
that would conflict with being able to call main() recursively.

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
--
Posted via a free Usenet account from http://www.teranews.com

Mar 27 '07 #56
On 14 Mar 2007 06:41:36 -0700, "Cong Wang" <xi************@gmail.com>
wrote:
<snip>
The standard just says "argc and argv and the strings pointed to by
the argv array shall be modi?able by the program." But the argv array
_itself_ is _not_ required to be modi?able.
Right, although many seem to feel this was an oversight and it was/is
intended to be. It's difficult to conceive of any reasonable (nonDS9k)
implementation where it isn't.
The following buggy code only works well when argc >=2 and
strlen(argv[1]) >=1:

#include <stdio.h>
#include <string.h>
(You don't actually need stdio.)
int main(int argc, char *argv[])
{
char buf[2] = {0};
Did you intend something else here, like {'X',0} ?
As written, you only need strlen(argv[1])>=0, which in fact is
redundant; if argc >= 2 argv[1] must be a valid string, and strlen of
any valid string is >= 0.
strcpy(argv[1], buf);
return 0;
}

I guess making that modifiable may be considered for exec*() functions
or for the recursion of main(). And the maximum length maybe found in
POSIX.
POSIX allows an implementation dependent maximum on the aggregate
total of all arguments and environment passed to an exec'ed program,
optionally including some overhead. But there is no allowance for a
separate limit on the length of a single argument, if that was meant.

Apr 15 '07 #57

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Joe Smith | last post by:
It is nothing short of embarrassing to feel the need to ask for help on this. I can't see how I would make the main control for this. What I want is a for loop and a test condition. And while I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.