Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 921737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:54:47+00:00 2026-05-15T18:54:47+00:00

I recently spent some time chasing an annoying little bug and I’m looking for

  • 0

I recently spent some time chasing an annoying little bug and I’m looking for suggestions for those of you who have either encountered the same problem or know the best way to avoid it.

I have a situation where I am developing in C++ and using strerror and as a result I am using something similar to

extern "C" {
#include <string.h>
}

(Same situation for #include <cstring>, btw). Now, there is a function defined in that file as follows: extern char *index (__const char *__s, int __c) This function lead to the fun I had where I originally had a construct similar to:

for (int index = 0; index != condition (); ++index) {
   // do something with buffer[index] here
}

log->write ("Final value of index: %d\n", index); // <- Ooops!!!

But instead of getting a compile error I get bogus output. I have my compiler (g++) flags set pretty high and the following did not catch this:

-W -Wall -Wextra -Werror -Wshadow -Wformat -pedantic -ansi

I can also not use an #undef trick like in <cstring> because this is not a macro.

My question is whether or not others have encountered this same problem and what is the best solution to it? Ideally, I’d love to hear about some obscure g++ functionality like -use-the-force-luke=... 😉

Note that I’m not asking how to solve this exact problem; I could just change the variable name. I’m looking for tips on how to avoid this situation in the future.

EDIT:

Due James Curran’s reply I think I should clarify a bit. I’m not looking at why this should not happen. I understand that in the absence of local variables the scope space is extended. What I am surprised about is that there is no flag I can set that warns about this. I’d think that -Wshadow would catch it since it catches variable/method shadowing within a class scope, but I digress.

What I am interested in is a way to have a notification that a local name is in conflict with a non-local scope. There is mention that I would have caught this particular bug had I used stream operations instead of variadic calls. True enough, but even the following will not produce warnings/errors with g++ (GCC) 4.1.1 20070105 (Red Hat 4.1.1-51) and the following flags -W -Wall -Wextra -Werror -Wshadow -ansi -pedantic.

#include <iostream>
#include <cstring>

int main () {
        int index = 42;
        std::cerr << index << std::endl;
        return 0;
}

That is curious to me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:54:48+00:00Added an answer on May 15, 2026 at 6:54 pm

    First, it looks like you are using printf style variadic argument list which causes an immediate loss of type safety. You should probably avoid this sort of design in C++.

    If you have to do this, then you could consider decorating the function declaration to tell gcc that it is a printf-like function and it will then give you warnings if your argument list doesn’t match your format string as it does for the standard *printf functions.

    E.g.

    void write(const char* f, ...) __attribute__((format (printf, 2, 3)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently spent some time looking for best practices on preventing browsers from caching
Recently I spent quite some time writing various Visual Studio Extensions projects. Even though
I have spent a lot of time recently reading the HTTP 1.1 specification and
I already spent some time developing small projects with the GWT and I recently
I recently ran into a familiar javascript/jQuery timing bug and spent too long debugging
Recently I've been working on some embedded devices, where we have some structs and
I recently learned of the IIF(A,B,C) function. I'm a long time VB/VB.NET Coder who
I've recently spent a good deal of time performing detailed UML designs of various
I spent a lot of time programming in Java recently, and one thing I
Was recently using some code along the lines of $(#divMenuContainer:visible).hide(explode); However after some time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.