[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: [MISC] Programmering, kode-stil, indent, flere return



Henrik Storner wrote:
In <sslug@sslug> Hans Schou <sslug@sslug> writes:


Må man have flere "return/exit" i en funktion?

Ja, flere return i en funktion kan bruges til status information:
     'return status'.

Min erfaring er, at det især er i forbindelse med fejlhåndtering, at
der bliver problemer. Det er jo også det, dit eksempel viser. Ved sådan noget fejl-håndtering kan selv den forkætrede "goto" jo være den pæneste løsning. F.eks. kunne dit eksempel jo også være skrevet:


	int ex3(void)
	{
		char *p1 = NULL, *p2 = NULL, *p3 = NULL;
		int n = 0;

		/* Alloker alle de buffere vi skal bruge */
		p1 = malloc(256); if (!p1) goto cleanup;
		p2 = malloc(256); if (!p2) goto cleanup;
		p3 = malloc(256); if (!p3) goto cleanup;

Dette kunne uden brug af goto skrives:
                p1 = malloc(256);
                p2 = malloc(256);
                p3 = malloc(256);
                if ((!p1) || (!p2) || (!p3)) {
                  free (p1);
                  free (p2);
                  free (p3);
                  return 0;
                }


-- Med venlig hilsen

Jørgen Heesche
mailto:sslug@sslug
Registered Linux User #401007



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2006-03-01, 02:04 CET [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] *