[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: [CPROG] Returnerin af at "string" array



Kim Brugger <sslug@sslug> skrev:

> Noget andet er at du benytter gets, denne funktion laver let buffer
> overflows hvis man ikke passer på, derfor har jeg valgt at benytte
> getline.

Han brugte fgets(), som ikke har den slags problemer. Hvad i alverden
er getline() - hvor har du fundet den?


Et par yderligere, hurtige kommentarer:


> #define sEND  "END";

const char sEND[] = "END";


> /*Global Variables*/

Nej tak ;-).


>   logfile = fopen(filename, "r");
>   if (!logfile){
>     perror(NULL);
>     return NULL;
>   }

Du kunne nok nøjes med fseek(logfile, 0, SEEK_SET); i stedet for at
lukke og åbne - hvis du absolut vil læse filen to gange.


>   i = 0;
>   ENDLines = (char**) malloc(antalstop * sizeof(char*));
>   while (getline (&line, &linesize, logfile) != EOF) {
>     if(strstr(line, sEND)){
>       ENDLines[i] = (char*) malloc ((linesize  + 1)* sizeof(char*));
>       strncpy(&ENDLines[i][0], line, linesize);
>       i++;
>     }
>   }

- Der er ingen grund til at cast'e returværdien fra malloc().

- Du glemmer at checke for, om malloc() returnerer 0.

- I din malloc i løkken mener du sizeof(char), som er lig 1 og derfor
kan udelades.

- Det er ikke nødvendigt med strNcpy(), idet der garanteret er plads nok
(du har jo lavet en malloc i rette størrelse).

- malloc() og strncpy() kan reduceres til strdup(), selv om jeg nu kan
se, at det ikke er ISO C, men en BSD funktion. Jeg troede faktisk at
strdup() var med i C. Nå, så fik jeg også lært noget i dag :-).

-- 
Regards, Anders


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:08 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] *