Skip to content
Search

C++:Troubleshooting

lastmod 2021-11-19created 2015-09-0314 edits

A collection of all problems related to C/C++.

Format security warning

If a warning like the following is printed:

warning: format string is not a string literal (potentially insecure) [-Wformat-security]
    printf(print_string.c_str());

Fix it as follows.

printf("%s", print_string.c_str());

HEAP CORRUPTION DETECTED

The HEAP CORRUPTION DETECTED error is caused by trying to access a region larger than the memory allocated with malloc or new.

Check the alignment or size of the memory allocated with malloc() carefully.

Stackoverflow in function

If a Stackoverflow error occurs at the start of a function, with MSVC you can increase the stack size using the /STACK option.

direct access in ~ to global weak symbol

An ld compile warning like the following may be printed.

ld: warning: direct access in _main to global weak symbol std::__1::char_traits::eq(char, char) means the weak symbol cannot be overridden at runtime. This was likely caused by different translation units being compiled with different visibility settings.

See Controlling Symbol Visibility @ developer.apple.com for details.

It looks like your libs (eg. the C++ standard library) and your code have been compiled with different visibility settings, at least, that is what the linker error message is saying.

To fix the warning, you should use the same visibility settings when compiling your code, eg -fvisibility=hidden

In other words, the visibility setting of every linked library must be the same.

Hide terminal window

How to stop a terminal window from appearing when the application runs.

MinGW (Windows)

Add -Wl,--subsystem,windows or -mwindows to the linker options.

macOS

Based on your description, you're building what is a unix-style executable. On OS X, those will always launch inside of a terminal window. The choices that you have on OS X are:

  • Run as a daemon as described in the above-linked post
  • Run in Terminal as a unix executable
  • Create a minimal OS X application wrapper and run as an OS X Application

In most cases, you can create a wrapper for a unix-style executable by creating the appropriate Bundle using the instructions from Apple's Bundle Programming Guide (skip over the iOS stuff and look at the Mac bundle information).

The basic directory structure is:

MyApp.app/
  Contents/
    Info.plist
  MacOS/
    executable
  Resources/
    MyApp.icns

Your unmodified executable can go in the MacOS directory, and you'll need to set up the following keys in the Info.plist using a plist editing tool or editor:

  • CFBundleIdentifier - the id of your app in reverse-dns notation (com.mycompany.myapp)
  • CFBundleDisplayName- the name of your app in human-readable form (MyApp)
  • CFBundleName - the short name of the app (usually the same as your app and executable name)
  • CFBundleVersion - your version # in X.Y[.Z] form
  • CFBundlePackageType - the package type, which should be APPL for applications
  • CFBundleExecutable - the name of your executable
  • CFBundleSignature - old-school Apple signature (4 character code that should theoretically be registered with apple)

A minimal plist would look like this: CFBundleDisplayName MyApp CFBundleExecutable a.out CFBundleIdentifier com.mycompany.myapp CFBundleName MyApp CFBundlePackageType APPL CFBundleSignature FOOZ CFBundleVersion 1.0

(The above example uses a.out as the executable, which would be located in MyApp.app/Contents/MacOS/a.out)

The icon resources can be left out if you don't care about the icon, and the default application icon will be used.

std::ios_base::badbit

Memory shortage.
There is no memory available to create the buffer, or the buffer has size 0 for other reasons (such as being provided from outside the stream), or the stream cannot allocate memory for its own internal data, as with std::ios_base::iword() and std::ios_base::pword().
The underlying stream buffer throws an exception.
The stream buffer might lose its integrity, as in memory shortage, or code conversion failure, or an unrecoverable read error from the external device. The stream buffer can indicate this loss of integrity by throwing an exception, which is caught by the stream and results in setting the badbit in the stream's state.

When stdint.h or inttypes.h cannot be found

Download msinttypes.

error: 'UINT64_C' was not declared in this scope

If this error appears, add CXXFLAGS=-D__STDC_CONSTANT_MACROS to the compile options. Alternatively, you can add the following to a header file.

#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS
#  ifdef _STDINT_H
#undef _STDINT_H
#  endif
#include <stdint.h>
#endif

See also

author Zer0 sitemap