[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




>-----Original Message-----
>From: Kenneth Geisshirt [mailto:sslug@sslug
>Sent: Thursday, May 13, 2004 8:04 AM
>To: sslug@sslug
>Subject: Re: [PROGRAMMERING] Bog om C
>
>
>Michael Rasmussen wrote:
>> On Wed, 12 May 2004 11:29:12 +0200, henrik wrote:
>> 
>>>af variabel størrelse defineres noget lignende sådan her: struct text
>>>{
>>>    int length;
>>>    char data[1];
>>>};
>>>hvor data så kan have hvilken som helst længde. Er der nogen som kan
>>>sige mig hvordan det fungerer og hvad fidusen er med denne 
>konstruktion?
>>>Hvorfor ikke ... char *data; ...?
>>>
>> 
>> En god grund kunne være, at struct's selv reserverer den nødvendige
>> hukommelse, der er behov for, så hvis længde sættes til 10, 
>vil struct
>> have længden 10 + 1 bytes (char data[1], der så kan bruges til '\0').

Jeg synes det bliver ufatteligt grimt:

struct text *textp = (struct text *) malloc(sizeof(struct text)+10);

sprintf(textp->data, "tekst %d", (int)sizeof(struct text)); /* vil virke som data er char data[11]; */

med masser af muligheder for fejl!

C++ exemple: fast længde data.

template<int siz = 1>
class text {
public:
  int length;
  char data[siz];
  text() : length(siz), data("") {} // eller siz-1 hvis det er det length skal fortælle
};

text<11>
  dintext; // data er 11 lang.

sprintf(dintext.data, "tekst %ul", sizeof(struct text));

!!! al kode utestet


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