www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - bigger then size_t

reply maarten van damme <maartenvd1994 gmail.com> writes:
What happens when args[1].length is bigger then size_t?
Can I detect this?
Oct 09 2012
parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 09-Oct-12 18:46, maarten van damme wrote:
 What happens when args[1].length is bigger then size_t?
 Can I detect this?
Then size_t.max? It can't as it has type size_t. Equal or even close? Nope. As it there would not be enough of even _virtual_ memory to fit array of size_t.max and something else (like your program :)) -- Dmitry Olshansky
Oct 09 2012
parent reply maarten van damme <maartenvd1994 gmail.com> writes:
But doesn't args[1] get filled by the first argument followed by your
program? Wouldn't a user then be able to launch that program followed
by the contents of a big file of say 5 gig. Wouldn't that overflow the
dynamic array? Would the program gracefully exit or crash with a
cryptic error?
Oct 09 2012
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Tuesday, 9 October 2012 at 15:56:15 UTC, maarten van damme 
wrote:
 Wouldn't a user then be able to launch that program followed by 
 the contents of a big file of say 5 gig.
The operating system won't allow that. There's a limit on argument sizes enforced before the program actually runs.
Oct 09 2012
next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
Ok, that solves it. Thank you.
Oct 09 2012
prev sibling next sibling parent reply maarten van damme <maartenvd1994 gmail.com> writes:
Another quick question. When I know an array is going to have an
length smaller then 255, can I use bytes as index or do I have to use
size_t to make it portable across 64 bit platforms?
Oct 09 2012
parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 09-Oct-12 20:36, maarten van damme wrote:
 Another quick question. When I know an array is going to have an
 length smaller then 255, can I use bytes as index or do I have to use
 size_t to make it portable across 64 bit platforms?
The real question is why you need that? Byte is not faster (if not slower). And again you still need to cast to size_t and back (as .length is size_t). The only value could probably be to have indexes occupy less space (if you store them somewhere). Still this needs some extra checks (or at least asserts). -- Dmitry Olshansky
Oct 09 2012
prev sibling parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 09-Oct-12 19:35, Adam D. Ruppe wrote:
 On Tuesday, 9 October 2012 at 15:56:15 UTC, maarten van damme wrote:
 Wouldn't a user then be able to launch that program followed by the
 contents of a big file of say 5 gig.
The operating system won't allow that. There's a limit on argument sizes enforced before the program actually runs.
I'll just add the follwoing: And how do you think shell does call program? Via syscall(s). And that syscall is just like (+- internal details) a normal C function call. And thus all arguments are always loaded into RAM first in *any* case. It's up to this program (shell, or whatever executes your program) to load that. And it will either truncate command line or fail itself. Also OS have some sane limits on command line length. IRC it's around 32768 in Windows. -- Dmitry Olshansky
Oct 09 2012