declare function inside fork()

This is a discussion on declare function inside fork() within the C Programming forums, part of the General Programming Boards category; Is there anything wrong with declaring a function inside a forked block, eg: Code: pid=fork(); if (pid==0) { void newfunc() ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    declare function inside fork()

    Is there anything wrong with declaring a function inside a forked block, eg:

    Code:
    pid=fork();
    if (pid==0) {
          void newfunc() {
                  [....]
          }
          [...]
          newfunc();
    }
    it seems to work, but just because no one stopped you doesn't make it right -- so I thought I'd ask and find out what kind of trouble may await.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    EDIT: Wrong thread.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    It's nothing to do with fork, it's just illegal C code.
    C doesn't allow nested functions, you're relying on a gcc extension.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Salem View Post
    It's nothing to do with fork, it's just illegal C code.
    C doesn't allow nested functions, you're relying on a gcc extension.
    So as long as it's compiled on gcc that's okay?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    No, don't rely on it.
    Local functions are illegal both in C and C++ and frankly, I don't see it as good solution to anything - even if allowed.
    You should write standard and portable code, first and foremost, without extensions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,680
    Why not use a legal construct?
    Code:
    typedef void (*fork_func)(void);
    
    pid_t fork_run(fork_func pfn)
    {
        pid_t pid = fork();
        if (pid == 0)
            pfn();
        return pid;
    }
    gg

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    No, don't rely on it.
    Local functions are illegal both in C and C++ and frankly, I don't see it as good solution to anything - even if allowed.
    You should write standard and portable code, first and foremost, without extensions.
    Well, gcc being my sole compiler, I wasn't aware I was using an extension (and without digging, I'm not really sure what one is). The reason for it is that the child performs operations on it's own variables, and I can't declare the function outside the child unless I globalize those variables. So perhaps it's just about tidyness. But suddenly I wonder -- is it also illegal to nest variable declarations that way?

    Code:
    pid=fork();
    if (pid==0) {
          int x;
          char y;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    declaring variables at the top of any block is legal.
    Code:
    void func()
    {
        /* code */
        {
            int x, y; /* legal */
            /* code */
        } /* x and y go out of scope here */
    }

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    > Well, gcc being my sole compiler, I wasn't aware I was using an extension (and without digging, I'm not really sure what one is).
    gcc -W -Wall -ansi -pedantic prog.c
    Will tell you most things which are non-portable.

    > But suddenly I wonder -- is it also illegal to nest variable declarations that way?
    So long as they're the first thing inside any enclosing brace, that's just fine.

    > The reason for it is that the child performs operations on it's own variables, and I can't declare the function outside the child unless I globalize those variables.
    The scope of your variables doesn't matter.
    As soon as you fork(), you've got your own separate copies no matter what scope they're declared in.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    C99 and C++ does not have the restriction that variables must be at the start of a block anyway. You don't need to follow the poor ruleset of C89. GCC fully supports C99, too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Partial support, actually. http://gcc.gnu.org/gcc-4.3/c99status.html

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    Oh well, better than nothing, at least.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Salem View Post
    >
    gcc -W -Wall -ansi -pedantic prog.c
    Will tell you most things which are non-portable.
    Interesting. The nested function is just a warning.

    One of the repeated warnings in this which is sort of befuddling is:

    warning: too many arguments for format

    Because I have three arguments to sprintf. How should I take that?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    > Interesting. The nested function is just a warning.

    Add -Werror

    Code?

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    warning: too many arguments for format

    So subtle... if only gcc would output it more like this

    warning: too many arguments for format

    It means you are doing something like:

    Code:
    sprintf("%d %d", &x, &y, &z);
    Surely it isn't as bad as doing this:

    Code:
    sprintf("%d %d %d", &x, &y);
    But it means you likely copied and pasted carelessly, or your logic somehow got muddled during the code writing process.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 04-11-2009, 09:44 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21