From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d02eb5c33ac65d9 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-03-09 09:04:37 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!arclight.uoregon.edu!wn13feed!wn12feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi.com!sccrnsc03.POSTED!not-for-mail From: "Jeffrey Creem" Newsgroups: comp.lang.ada References: Subject: Re: Array and memory usage X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 Message-ID: NNTP-Posting-Host: 66.31.5.146 X-Complaints-To: abuse@attbi.com X-Trace: sccrnsc03 1047229476 66.31.5.146 (Sun, 09 Mar 2003 17:04:36 GMT) NNTP-Posting-Date: Sun, 09 Mar 2003 17:04:36 GMT Organization: AT&T Broadband Date: Sun, 09 Mar 2003 17:04:36 GMT Xref: archiver1.google.com comp.lang.ada:35093 Date: 2003-03-09T17:04:36+00:00 List-Id: "Preben Randhol" wrote in message news:slrnb6m8mg.15o.randhol+news@kiuk0152.chembio.ntnu.no... > I was a bit puzzeled to see that if I do: > > type Str_Array_Type is array (1 .. 1000) of String (1 .. 800); > > type User_Data is > record > Name : Str_Array_Type; > Hostname : Str_Array_Type; > end record; > > my program uses 800 bytes of memory, while if I only change the program > like this: > > type User_Data is > record > Name : Str_Array_Type := (others => (others => ' ')); > Hostname : Str_Array_Type := (others => (others => ' ')); > end record; > > then the program uses 2356 bytes. > > I thought all the memory needed for an array would be claimed when the > program started no matter if the array was filled or not. > > Is this correct or am I just being tricked by the program that measures > the memory used by my program? > > If it is correct then it doesn't matter if I define my array as (1 .. > 100) or (1 .. 10_000) as long as I don't initialize the strings. > I can just add to User_Data a: > > type Used_Type is array (1 .. 1000) of Boolean; > > Used : Used_Type := (others => False); I believe what you are seeing is not something that will happen under all OS's. Linux uses a commit-on-us type memory manager. Even though you initialized your data, it probably was done in such a way that the memory was intialized within the ELF initialized data segment. Under linux even something like aa = malloc(A_BIG_NUMBER) will not show all of the data as actually allocated until a process accesses each page of memory.. What you are seeing is partially a trick and partially real. It is not a direct effect of the language or compiler. Whether or not you should write your programs to rely on it will depend on many factors. Certainly on an embedded OS like vxWorks, your memory would be allocated and consumed immediately.