Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #42460

Re: array-size/malloc limit and strlen() failure

From Kaz Kylheku <kaz@kylheku.com>
Newsgroups comp.lang.c
Subject Re: array-size/malloc limit and strlen() failure
Date 2014-04-02 07:36 +0000
Organization Aioe.org NNTP Server
Message-ID <20140402002802.249@kylheku.com> (permalink)
References <73748137-ca58-4518-b00d-86d886b3228b@googlegroups.com>

Show all headers | View raw


On 2014-04-02, jay <arnuld.mizong@gmail.com> wrote:
> #include <stdio.h>
> #include <limits.h>
>
> int main(void)
> {
>  char arrc[UINT_MAX] = {'a'};
>  printf("arrc = %s\n", arrc);
>
>  return 0;
> }
>
>================ OUTPUT ==============
> [myhome]$ gcc -ansi -pedantic -Wall -Wextra test2.c
> test2.c: In function `main':
> test2.c:6: error: size of array `arrc' is too large
> test2.c:6: warning: unused variable `arrc'

You do realize that on a system with 32 bit pointers and 32 bit unsigned int,
"char [UINT_MAX]" takes up the entire address space?

Are you compiling a 64 bit executable or 32 bit?

Four billion byte arrays call for 64 bit programming on a 64 bit OS.

> I am using gcc 3.4.6 on Solaris 10.  I completely understand the error that
> array size should be below some limit but what is that limit ?  Same happens
> if I try to malloc(UINT_MAX), it fails.
> 2nd, I am writing a program where input is taken from command-line (think
> argv[1]). User can input anything of any size. Imagine user inputs a string
> too long, just like UINT_MAX was way bigger for malloc(), and I try to do
> strlen() on it ?  How will I know/check that string is too long so that I can
> bail out on time ?

The operating system imposes a limit on the total amont of memory which can be passed from one
process to another as environment variables and argument material.

The user will run into that argument passing limit long before we worry about
UINT_MAX sized arrays for a single argument string.

If an argv[1] could be prepared and passed to your program that is four billion
bytes long, strlen should work on it just fine.

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

array-size/malloc limit and strlen() failure jay <arnuld.mizong@gmail.com> - 2014-04-01 23:31 -0700
  Re: array-size/malloc limit and strlen() failure Xavier Roche <xroche@free.fr.NOSPAM.invalid> - 2014-04-02 08:47 +0200
  Re: array-size/malloc limit and strlen() failure Kaz Kylheku <kaz@kylheku.com> - 2014-04-02 07:36 +0000
  Re: array-size/malloc limit and strlen() failure "BartC" <bc@freeuk.com> - 2014-04-02 10:10 +0100
    Re: array-size/malloc limit and strlen() failure Keith Thompson <kst-u@mib.org> - 2014-04-02 08:11 -0700
      Re: array-size/malloc limit and strlen() failure glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-04-02 19:03 +0000
        Re: array-size/malloc limit and strlen() failure James Kuyper <jameskuyper@verizon.net> - 2014-04-02 15:48 -0400
          Re: array-size/malloc limit and strlen() failure Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-04-02 14:42 -0700
            Re: array-size/malloc limit and strlen() failure Keith Thompson <kst-u@mib.org> - 2014-04-02 15:14 -0700
        Re: array-size/malloc limit and strlen() failure Keith Thompson <kst-u@mib.org> - 2014-04-02 14:55 -0700
          Re: array-size/malloc limit and strlen() failure glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-04-02 23:42 +0000
  Re: array-size/malloc limit and strlen() failure Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-04-02 02:34 -0700
  Re: array-size/malloc limit and strlen() failure James Kuyper <jameskuyper@verizon.net> - 2014-04-02 07:40 -0400
    Re: array-size/malloc limit and strlen() failure Thomas Jahns <jahns@idontlikespam.dkrz.de> - 2014-04-02 13:52 +0200
    Re: array-size/malloc limit and strlen() failure Keith Thompson <kst-u@mib.org> - 2014-04-02 08:21 -0700
      Re: array-size/malloc limit and strlen() failure James Kuyper <jameskuyper@verizon.net> - 2014-04-02 11:30 -0400
      Re: array-size/malloc limit and strlen() failure glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-04-02 19:26 +0000
        Re: array-size/malloc limit and strlen() failure "BartC" <bc@freeuk.com> - 2014-04-02 20:39 +0100
        Re: array-size/malloc limit and strlen() failure Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2014-04-02 14:39 -0600
        Re: array-size/malloc limit and strlen() failure Keith Thompson <kst-u@mib.org> - 2014-04-02 15:03 -0700
          Re: array-size/malloc limit and strlen() failure Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-04-03 01:11 -0700
            Re: array-size/malloc limit and strlen() failure Ike Naar <ike@iceland.freeshell.org> - 2014-04-03 14:27 +0000
              Re: array-size/malloc limit and strlen() failure James Kuyper <jameskuyper@verizon.net> - 2014-04-03 17:56 -0400
            Re: array-size/malloc limit and strlen() failure Keith Thompson <kst-u@mib.org> - 2014-04-03 08:18 -0700
              Re: array-size/malloc limit and strlen() failure gazelle@shell.xmission.com (Kenny McCormack) - 2014-04-03 15:30 +0000
      Re: array-size/malloc limit and strlen() failure Stephen Sprunk <stephen@sprunk.org> - 2014-04-03 00:33 -0500

csiph-web