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; fthis and content are both const char * and/or char *?...

  1. #31
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    fthis and content are both const char * and/or char *?

  2. #32
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Interesting. Using gcc 4.0.1 on Mac OSX (yeah, I know), this code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
        char content[] = "A sample string.";
        char fthis[] = "Sample";
        char *found;
        found = strcasestr(content, fthis);
        if (found != NULL) printf("%s\n", found);
        return 0;
    }
    produces these warnings/errors (with -Wall -Wextra):
    Code:
     
    and runs correctly. Does it not for you?

  3. #33
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    and runs correctly. Does it not for you?
    It runs correctly, but it produces this warning:

    Code:
    test.c: In function ‘main’:
    test.c:8: warning: assignment makes pointer from integer without a cast
    If you substitute strstr there is no warning.
    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

  4. #34
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    When you type "man strcasestr", what does it tell you the return type should be? (Since the warning is on the assignment, meaning your compiler thinks strcasestr returns an int, and not a char *, which means your header claims that strcasestr returns an int.) I guess you can go poking in your headers.

    A little bit of googling shows that some headers surround it in #if __USE_GNU or something like that, which means that it's actually an implicit declaration rather than a mis-declaration. What happens if you add -std=gnu99 to your compile line?

    MORE: If you want, you can do
    Code:
    gcc -E -o temp.ohmygoodness temp.c
    to see what happens in the pre-processor, and then look for "strcasestr" and see if a prototype got #included.
    Last edited by tabstop; 10-11-2008 at 04:08 PM.

  5. #35
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Nobody has ever pointed out that what you're trying to do is DEFINE a function (nested) inside a block. DECLARING one (i.e. prototyping it) is not a problem.

    In other words, this is fine:

    Code:
    void func1()
    {
        void func2();
    
        func2();
    }
    This is not:

    Code:
    void func1()
    {
        void func2() { ... }
    
        func2();
    }
    Nested functions are not just unportable, they are gcc-specific and rely on trampolines to forward local variable scopes (i.e. they don't use "access pointers" because they break stack frame ABI compatibility) which means such a function takes huge performance hits when running in a non-executable-stack environment.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #36
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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 I guess fork() is out then, as that's not a C standard...

    Compiler authors would not bother adding extensions if nobody was willing to use them. The Linux kernel, for instance, is only compilable by gcc due to mass use of gcc extensions. This is fine as long as you are comfortable with your decision, since you are locked in.

    gcc, unlike most other compilers, is open source and unlikely to disappear overnight in a puff of smoke, so you won't be left with a mass of code that can't be compiled.

    Of course, using an extension gratuitously doesn't make much sense. And in this specific case, I'd definitely stay away from nested functions, since believe me, you don't want to know what gcc is doing under the hood when you do that.

    (gcc nested functions inherit the local scope of the enclosing function, meaning the nested function has access to the encloser's local variables -- the compiler must perform dirty tricks to make it happen correctly -- those tricks include putting CODE onto the STACK -- do you really want that?)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Page 3 of 3 FirstFirst 123
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