Problem with Initializing Consts

Posted by UdiM on Stack Overflow See other posts from Stack Overflow or by UdiM
Published on 2010-04-15T12:57:15Z Indexed on 2010/04/15 13:03 UTC
Read the original article Hit count: 147

Filed under:
|
|

This code, when compiled in xlC 8.0 (on AIX 6.1), produces the wrong result. It should print 12345, but instead prints 804399880. Removing the const in front of result makes the code work correctly.

Where is the bug?

#include <stdio.h>
#include <stdlib.h>
#include <string>

long int foo(std::string input)
{
        return strtol(input.c_str(), NULL, 0);
}

void bar()
{
        const long int result = foo("12345");
        printf("%u\n", result);
}

int
main()
{
        bar();
        return 0;
}

Compilation command:

/usr/vacpp/bin/xlC example.cpp -g

© Stack Overflow or respective owner

Related posts about xlc

Related posts about c++