Thread: Compiler error: warning: implicit declaration of function 'strdup'

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by TalonStriker View Post
    I've included <string.h> and <wchar.h> so I shouldn't be getting the error right?
    I've seen environments where strdup() was declared in stdlib.h instead of string.h where it was supposed to be.

    Alternatively, you could be banging up against Microsoft's stupid attempts to "make C secure," by deprecating basic language functions. Do you happen to be compiling with a MS compiler?

    EDIT: Haha, nevermind. I forgot that strdup() isn't standard C, so if your compiler doesn't support it... That's tough. Write it yourself:

    Code:
    char *strdup(const char *str)
    {
        int n = strlen(str) + 1;
        char *dup = malloc(n);
        if(dup)
        {
            strcpy(dup, str);
        }
        return dup;
    }

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    23
    Well I'm using the gcc compiler.

    Quote Originally Posted by brewbuck View Post
    EDIT: Haha, nevermind. I forgot that strdup() isn't standard C, so if your compiler doesn't support it... That's tough. Write it yourself:

    Code:
    char *strdup(const char *str)
    {
        int n = strlen(str) + 1;
        char *dup = malloc(n);
        if(dup)
        {
            strcpy(dup, str);
        }
        return dup;
    }
    Thanks. I'm not sure why strdup() not being in standard C effects anything. According to the gcc API, strdup is supported in gcc. However, I'm compiling with the ansi and pedantic flags--does this somehow exclude the strdup() from being #include'd when I include string.h?

    Anyways thanks for the implementation of strdup...I was afraid that I'd have to waste brain cells writing my own strdup

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by TalonStriker View Post
    Thanks. I'm not sure why strdup() not being in standard C effects anything.
    Because if it's not standard, then there is no standard place where the prototype will live. On my gcc installation here, strdup() is defined in string.h. On yours, it clearly is not. Is that not enough to convince you that non-standard features are, in fact, non-standard?

    According to the gcc API, strdup is supported in gcc. However, I'm compiling with the ansi and pedantic flags--does this somehow exclude the strdup() from being #include'd when I include string.h?
    That MIGHT have something to do with it. Not sure though.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, if you compile with -ansi the compiler will not use non-standard functions, and if you compile with -pedantic it will also warn about some non-standard behaviours in the code you write. It won't make errors of it unless you use -Werror [which will make errors of all warnings]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    23
    Quote Originally Posted by matsp View Post
    It won't make errors of it unless you use -Werror [which will make errors of all warnings]--
    Mats
    Ah... I see... suppose I hadn't used -Werror, it would have created the object code...but would that object code be "wrong" (i.e. not run properly after linking)?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Possibly. I actually would recommend using -Werror for new projects - it may not work on old projects which already have warnings [or if you dramatically change the compiler version, e.g. changing from a 3.4 to a 4.2 version of gcc may throw a few new warnings in the mix, and in a large project that could cause major issues - of course, changing the compiler on large pieces of code is always a "interesting" project]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    23
    Quote Originally Posted by brewbuck View Post
    Because if it's not standard, then there is no standard place where the prototype will live. On my gcc installation here, strdup() is defined in string.h. On yours, it clearly is not. Is that not enough to convince you that non-standard features are, in fact, non-standard?
    Actually, I didn't quite understand what "standard" meant in this context. From your explanation, for a function to be standard, its prototype should exist in the same library in all compilers....or am I jumping to conclusions?

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by TalonStriker View Post
    Actually, I didn't quite understand what "standard" meant in this context. From your explanation, for a function to be standard, its prototype should exist in the same library in all compilers....or am I jumping to conclusions?
    For a function to be standard, it must be described in the document ISO-9899 (i.e. the "C standard"). That's what I mean.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    23
    Quote Originally Posted by brewbuck View Post
    Code:
    char *strdup(const char *str)
    {
        int n = strlen(str) + 1;
        char *dup = malloc(n * sizeof(char));
        if(dup)
        {
            strcpy(dup, str);
        }
        return dup;
    }
    I'm using this implementation of strdup. When I was checking whether I had any memory leaks, the compiler claims that there is a memory leak at line 4 of the function.
    Code:
    1179            char *dup = malloc(n * sizeof(char));
    (gdb) next
    memory clobbered past end of allocated block
    Any idea how that could happen?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see anything wrong with that line itself. Perhaps the REAL problem is elsewhere, but this is where it gets detected? What code runs just before this, and does it stick within it's allowed limits?

    Edit: It looks like your error message comes from the call to malloc() itself, rather than strdup(). I strongly suspect (as posted above) that this is part of "selfchecking" inside malloc that says "is the header that I'm using to allocate the next block of memory OK or not", and if it's NOT OK, it prints the above message. I have no idea how your particular malloc works, but most malloc functions keep it's own little structure before the actual address you get back from malloc. The data structure often contains some way to determine that it's correct, such as a "magic" value that should match either a constant, or some other data in the block in some way - e.g. the first element is a pointer to the next block of memory. After this pointer, we may for example store the inverse of that pointer, just to check. Or the first element should always be 0xaa55baba. If that doesn't "match up" then the malloc function says "what the **** is going on here"...

    --
    Mats
    Last edited by matsp; 11-10-2007 at 01:28 PM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Replies: 2
    Last Post: 11-08-2007, 02:33 AM
  3. Replies: 12
    Last Post: 10-24-2007, 08:10 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM