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.8 required=5.0 tests=BAYES_00,FORGED_MSGID_YAHOO autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,c50f57c0c29b391b X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!peer01.cox.net!cox.net!p01!fed1read03.POSTED!53ab2750!not-for-mail Message-Id: <1176609.QemMZIaVxk@yahoo.com> From: alex goldman Subject: Re: memory management Newsgroups: comp.lang.ada References: <1131064.rs72P29t4t@yahoo.com> <1226363.QsRZW1KHie@linux1.krischik.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit Date: Fri, 03 Jun 2005 03:12:50 -0700 NNTP-Posting-Host: 68.107.102.129 X-Complaints-To: abuse@cox.net X-Trace: fed1read03 1117793643 68.107.102.129 (Fri, 03 Jun 2005 06:14:03 EDT) NNTP-Posting-Date: Fri, 03 Jun 2005 06:14:03 EDT Organization: Cox Communications Xref: g2news1.google.com comp.lang.ada:11218 Date: 2005-06-03T03:12:50-07:00 List-Id: Steve wrote: > struct Data_Container > { > int nbValues; > int data[1]; > } > > ... > numElements = 100; > struct Data_Container* dc = malloc( sizeof(Data_Container) + sizeof(int) > *( numElements - 1)); > > dc->nbValues = numElements; > for( i = 0 ; i < dc->nbValues ; i++ ) > { > dc->data[ i ] = 0; > } I think the results of doing this are undefined, according to the standard. In fact, the section on pointer arithmetic says even something like int* p = malloc(100*sizeof(int)); p = p + 101; // more than 1 off p = p - 50; is allowed to lead to undefined behavior. The pointer doesn't even need to be dereferenced! So the C standard certainly doesn't forbid bounds checking. Why C compilers don't come with this feature as an /option/ has always puzzled me.