Re: Re: quit files

"Ivan Warren [email protected] [hercules-390]" <[email protected]>
Newsgroups gmane.comp.emulators.hercules390.general
Message-ID <[email protected]>
Le 6/23/2019 à 3:11 PM, Joe Monk [email protected] [hercules-390] a 
écrit :
>
>
> So i've traced the release_config code in config.c (because debugs 
> show the code entering HDL: release config) as far as detach_devblk in 
> config.c down to this...
>
> /* Free the argv array */
>
>
> 	
>
> for(i = 0; i < dev->argc; i++)
>
>
> 	
>
> if(dev->argv[i])
>
>
> 	
>
> free(dev->argv[i]);
>
>
> 	
>
> if(dev->argv)
>
>
> 	
>
> free(dev->argv);
>
>
> How can you have an array item (argv[i]) referenced by a non-array 
> statement (argv)? Plus, when the loop falls thru because i = argc, 
> then what happens? Plus, dont you need braces on the for loop like this?
>
>
Joe,

No you don't ! the syntax for the "for" is for(initial statement;  
condition statement; iteration statement) <statement>;

Additionally argc cannot be non 0 and argv be null simultaneously (in 
principle)

"if" is a statement in itself in the form of if(condition statement) 
<statement>;

so

for(i=0;i<dev->argc;i++) if(dev->argv[i} free(dev->argv[i];

does what it is intended to do. (the line breaks, spaces and tabs change 
nothing)

The braces are not required. (but definitely make the code easier to 
read, prevents any issue if someone wants to add another statement 
between the "for" and the "if" or after the "if" (and doesn't see the 
lack of braces) and certainly does not change the resulting binary object).

I would do :

assert(dev->argc && !dev->argv);/* Ensure we don't have an argv array 
and no argument */
assert(!dev->argc && dev->argv); /* Ensure we don't have a NULL argv 
array but some arguments */
if(dev->argv)
{
     for(i=0;i<dev->argc;i++)
     {
         free(dev->argv[i]); /* free te argument in question */
         dev->argv[i]=NULL;  /* Ensure we don't double free */
     }
     free(dev->argv); /* Free the array */
     dev->argv=NULL;  /* Ensure we don't double free */
}
dev->argc=0; /* Be consistent */

--Ivan



[Non-text portions of this message have been removed]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.