[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



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


 
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] *