[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] Bog om C



On Thu, 13 May 2004 11:08:23 +0200, Egon Andersen wrote:

> 
> sslug@sslug wrote:
> 
> > 
> > Jeg kan se at man allokerer den mængde hukommelse
man
> > har brug for til en given strenglænge dvs.
størrelsen
> > på structen + antal tegn:
> > 
> > text tt = (text *)malloc(sizeof(text) + 6 *
> > sizeof(char));
> > 
> > hvorefter man kan tilgå data[0] .. data[6] fordi C
> ikke
> > har check af array-grænser. Man skal sørge for at
> > 'data' står sidst i structen.
> > 
> > Jeg stødte ind i det her i PostgreSQL's doc om
> > C-funktioner - PostgreSQL's variabel længde typer er
> > defineret på denne måde.
> > ---
> 
> Det er en lidt uheldig måde at gøre det på.
> C99-standarden foreskriver at 'flexible array member'
> angives som fx.:
> char data[];
> altså uden angivelse af størrelse.
> Der er jo ikke nogen der siger at en C-compiler ikke
må
> udlægge run-time 
> check for array-grænser.
> Ved at følge standarden, har man altså på en
> standardiseret måde angivet 
> 'flexible array member' og slået en eventuel
> array-grænse check fra.
> Nu skal man så blot huske at allokeringen fra
eksemplet
> bliver:
> 
> text tt = (text *)malloc(sizeof(text) + (6+1) *
> sizeof(char));
> 
> (At PostgrsSQL så ikke overholder C99-standarden er
> sagen uvedkommende.)
> 
> Med venlig hilsen
> Egon Andersen

Ja, det må være den rigtige måde at gøre det på - hvis
man kan i praksis. gcc-2.95 forlanger faktisk en
størrelsesangivelse på arrayet, mens gcc-3.3 godtager
et 'flexible array member'.
---
Hilsen
Henrik Zederkof


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:43 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *