declaring functions inside other functions 
Author Message
 declaring functions inside other functions

I made a function that do some stuff (it opens files, assigns
dynamic-memory); Soon, I realized that I had to code another function that
cleans all the stuff allocated and call it at the end, or if some errors
were encountered); It's ended like this:

static void cleanCp_(void){
  if(destPath != NULL)
    free(destPath);
  if(destFilePath != NULL)
    free(destFilePath);
  if(srcFD != -1)
    close_(srcFD);
  if(destFilefd != -1)
    close(destFilefd);

Quote:
}

I want to avoid to pass all the arguments but I want to avoid to declare
them global too ('cause in reality this is a method in C++ class :).

I miss a trick (or a basic skill)...
Can I declare this function inside the other function ?
---
Sorry for the long post (and the bad English), ... thanx in advance
--



Mon, 28 Nov 2005 10:06:48 GMT  
 declaring functions inside other functions

Quote:
> I want to avoid to pass all the arguments but I want to avoid to declare
> them global too ('cause in reality this is a method in C++ class :).

> I miss a trick (or a basic skill)...
> Can I declare this function inside the other function ?

I do not see a way in C to satisfy your needs, but C++ has multiple. Since
you seem to use C++ but this is a C group here my brief off-topic answer
(please F'up2 comp.lang.c++):

-> First, you might check if exception handling is helpful in your case
   (see try, catch and finally keywords).

-> Second, try using an internal helper class ... e.g.:

class A {

        void Test();

private:

        class A_Res {

        public:
                char *res_ptr;

                A_Res() : res_ptr(NULL) {}
                ~A_Res() { delete res_ptr )
        };
);

void A::Test() {

        A_Res res;

        res.res_ptr = new char[255];

Quote:
}

See? Whenever A::Test is left, the destructor of A_Res is called which
deletes res_ptr.

HTH

Sven

--
Sven Semmler                   http://www.semmlerconsulting.com
fingerprint: 72CA E26D C2A3 1FEB 7AFC  10EA F769 A9A4 937F 5E67
--



Wed, 30 Nov 2005 05:42:35 GMT  
 declaring functions inside other functions


# I made a function that do some stuff (it opens files, assigns
# dynamic-memory); Soon, I realized that I had to code another function that
# cleans all the stuff allocated and call it at the end, or if some errors
# were encountered); It's ended like this:
#
# static void cleanCp_(void){
#  if(destPath != NULL)
#    free(destPath);
#  if(destFilePath != NULL)
#    free(destFilePath);
#  if(srcFD != -1)
#    close_(srcFD);
#  if(destFilefd != -1)
#    close(destFilefd);
# }
#
# I want to avoid to pass all the arguments but I want to avoid to declare
# them global too ('cause in reality this is a method in C++ class :).
#
# I miss a trick (or a basic skill)...
# Can I declare this function inside the other function ?

No, C does not allow functions defined within other functions.
(GNU C has this as an extension, but you want to avoid making
use of such nonportable extensions.)

It appears to me you could use a suitable macro, and thus sort of
"inline" the code of cleanCp_, e.g. like this

#define cleanCp_()            \
  do {                        \
    if (destPath != NULL)     \
      free(destPath);         \
    if (destFilePath != NULL) \
      free(destFilePath);     \
    if (srcFD != -1)          \
      close_(srcFD);          \
    if (destFilefd != -1)     \
      close(destFilefd);      \
  } while (0)

The do/while(0) is the canonical kludge if you want several statements
in a macro and force a semicolon after any macro usage (in order not to
mutilate C syntax) and have it work even in if/else statements without
compound statements.

Regards,

        Jens
--
Jens Schweikhardt  http://www.schweikhardt.net/
SIGSIG -- signature too long (core dumped)
--



Wed, 30 Nov 2005 05:42:38 GMT  
 declaring functions inside other functions
On 12 Jun 2003 02:06:48 GMT

Quote:

> I made a function that do some stuff (it opens files, assigns
> dynamic-memory); Soon, I realized that I had to code another function
> that cleans all the stuff allocated and call it at the end, or if some
> errors were encountered); It's ended like this:

> static void cleanCp_(void){
>   if(destPath != NULL)
>     free(destPath);
>   if(destFilePath != NULL)
>     free(destFilePath);
>   if(srcFD != -1)
>     close_(srcFD);
>   if(destFilefd != -1)
>     close(destFilefd);
> }

> I want to avoid to pass all the arguments but I want to avoid to
> declare them global too ('cause in reality this is a method in C++
> class :).

> I miss a trick (or a basic skill)...
> Can I declare this function inside the other function ?

You can't declare functions inside other functions. What you can do is
limit the scope of variables to just one compilation unit using static,
e.g.

static char *destPath;

Then group these functions in one file.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
--



Wed, 30 Nov 2005 05:42:42 GMT  
 declaring functions inside other functions

Quote:

> I made a function that do some stuff (it opens files, assigns
> dynamic-memory); Soon, I realized that I had to code another function that
> cleans all the stuff allocated and call it at the end, or if some errors
> were encountered);

In C, this is usually convenient to do with goto:

  bool copy(void) {
    char *destPath = NULL;
    char *destFilePath = NULL;
    int srcFD = -1;
    int destFilefd = -1;
    bool ok = false;

    destPath = computeDestPath();
    if (destPath == NULL)
      goto cleanup;
    // (similar computations for other variables)

    // (do the actual copy)

    ok = true;
   cleanup:
    if(destPath != NULL)
      free(destPath);
    // (similar cleanup for other variables)

    return ok;
  }

You need to be careful if some block in the function needs extra
cleanup, however.  For example, if you allocate a temporary
buffer dynamically and define the pointer variable in an inner
block so that the cleanup code cannot see it, you have to free
the buffer before jumping to the cleanup label.  In such cases,
it is safest to move the variable to the top of the function.

On the other hand, if an inner block temporarily alters some
global state (for example enters a critical section), then it
may not be desirable to do that immediately at the beginning.
You can then instead define a local flag variable that indicates
whether the state has been changed, and have the cleanup code
check that.  The danger is that you may forget to update the
flag, and this bug won't be seen until an error occurs within the
temporary state.

Quote:
> I want to avoid to pass all the arguments but I want to avoid to declare
> them global too ('cause in reality this is a method in C++ class :).

C++ has a mechanism for freeing resources when a variable goes
out of scope.  You may want to ask about that in a C++ newsgroup.

Quote:
> I miss a trick (or a basic skill)...
> Can I declare this function inside the other function ?

In standard C, you cannot *define* a function inside another
function.  You can *declare* it, but that isn't what you want:

  int f(void) {
    int i = 42;
    int g(void);   // declares the function g
    return g() * i;
  }
  int g(void) {    // defines the function g
    return 69;     // you cannot access f's local variable i here
  }

If you use GCC, look up "Nested Functions" in the manual.
--



Wed, 30 Nov 2005 05:42:44 GMT  
 declaring functions inside other functions

Quote:

> I made a function that do some stuff (it opens files, assigns
> dynamic-memory); Soon, I realized that I had to code another function that
> cleans all the stuff allocated and call it at the end, or if some errors
> were encountered);
[...]
> I want to avoid to pass all the arguments but I want to avoid to declare
> them global too ('cause in reality this is a method in C++ class :).

Then why on earth did you post this question to the *C* newsgroup?

Why have all that stuff in a separate function in the first place?
What you have discovered here is the real reason C++ grew the concept
of an "exception", and why C still, at times, needs a "goto" if you
want to keep your code sane.

Put the body of what you wanted to turn into a separate function at
the end of the "mother" function, write a label in front of it, called
something obvious like "error_exit:", and then goto there whenever
stuff goes bad.

It's ugly, but once you're beyond a certain level of nesting
complexity, goto, while considered harmful, can really save your day.

Quote:
> Can I declare this function inside the other function ?

No.  Nested functions don't exist in either C or C++.
--

Even if all the snow were burnt, ashes would remain.
--



Wed, 30 Nov 2005 05:42:46 GMT  
 declaring functions inside other functions
It sounds like you should include that in your class destructor method, if
you need some help with basic C++ try comp.lang.c++


Quote:
> I made a function that do some stuff (it opens files, assigns
> dynamic-memory); Soon, I realized that I had to code another function that
> cleans all the stuff allocated and call it at the end, or if some errors
> were encountered); It's ended like this:

> static void cleanCp_(void){
>   if(destPath != NULL)
>     free(destPath);
>   if(destFilePath != NULL)
>     free(destFilePath);
>   if(srcFD != -1)
>     close_(srcFD);
>   if(destFilefd != -1)
>     close(destFilefd);
> }

> I want to avoid to pass all the arguments but I want to avoid to declare
> them global too ('cause in reality this is a method in C++ class :).

> I miss a trick (or a basic skill)...
> Can I declare this function inside the other function ?
> ---
> Sorry for the long post (and the bad English), ... thanx in advance
> --


--



Wed, 30 Nov 2005 05:42:47 GMT  
 declaring functions inside other functions

Quote:

> static void cleanCp_(void){
>   if(destPath != NULL)
>     free(destPath);
>   if(destFilePath != NULL)
>     free(destFilePath);
>   if(srcFD != -1)
>     close_(srcFD);
>   if(destFilefd != -1)
>     close(destFilefd);
> }
> I want to avoid to pass all the arguments but I want to avoid to declare
> them global too ('cause in reality this is a method in C++ class :).

In C, the simplest solution is to define a macro instead:

#define cleanCp_() do { \
  if(destPath != NULL) \
    free(destPath); \
  if(destFilePath != NULL) \
    free(destFilePath); \
  if(srcFD != -1) \
    close_(srcFD); \
  if(destFilefd != -1) \
    close(destFilefd); \

Quote:
} while(0)

C99 provides inline functions, which generally avoid the overhead
of actual function call-return linkage, but you'd still have to
invoke the inline function with all the arguments.
--



Wed, 30 Nov 2005 05:42:49 GMT  
 declaring functions inside other functions
in comp.lang.c.moderated i read:

Quote:
>I made a function that do some stuff (it opens files, assigns
>dynamic-memory); Soon, I realized that I had to code another function that
>cleans all the stuff allocated and call it at the end, or if some errors
>were encountered);
>Can I declare this function inside the other function ?

not in standard c.  some compilers allow it as an extension.

--
a signature
--



Wed, 30 Nov 2005 05:42:51 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. functions declared inside blocks with storage class

2. Declaring static function returning pointer to extern function

3. Why functions declared within functions?

4. Setting a pointer to a function, inside a function

5. Function inside another function?...

6. printing function name and arguments from inside a function

7. function to be defined inside other function

8. Function pointer to function inside class

9. How to declare function with default parameters ?

10. Newbie: declaring the "main" function

11. Declaring Function Pointers...

12. declare static function?

 

 
Powered by phpBB® Forum Software