| Jon Stewart on 1 Oct 2003 04:30:45 -0000 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
| Re: [ALACPP] Why won't g++ tell me I'm dumb? |
> void strrevtest() {
> char* t;
> t = ""; strrev(t);BOOST_CHECK_EQUAL( std::strcmp(t,""), 0);
> t = "a"; strrev(t);BOOST_CHECK_EQUAL( std::strcmp(t,"a"), 0);
> t = "ab"; strrev(t);BOOST_CHECK_EQUAL( std::strcmp(t,"ba"), 0);
> t = "asdf"; strrev(t);BOOST_CHECK_EQUAL( std::strcmp(t,"fdsa"), 0);
> t = "asdfg"; strrev(t);BOOST_CHECK_EQUAL( std::strcmp(t,"gfdsa"), 0);
> }
>
> test_suite*
> init_unit_test_suite ( int argc, char** argv) {
> test_suite* test = BOOST_TEST_SUITE( "strrev test");
> test->add( BOOST_TEST_CASE( &strrevtest ));
> return test;
> }
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> This produces a memory access violation when it does the first swap.
> The reason is that I'm dumb and I was trying to modify a (const) string
> literal. Now, I can fix my test case by sticking in some strdup's, but
> there was also a consensus that the C++ compiler ought to complain.
> However, it really seems to just silently discard the const-ness,
> despite the suggestions of the g++ man page.
>
> Chris and I also tried with the Intel compiler, but that didn't
> complain either.
>
> So, is there some flag we're missing that would spit out a warning
> about this const issue?
This is all supposition... I don't have my copy of Stroustrup at home...
Now that I look at this some more, it's simply another C gotcha'. To
maintain backwarks compatibility with C, C++ would certainly need to allow
you to assign a string literal to a char*. If you'd assigned the string
literals to const char*'s, the compiler would bitch when you assigned a
const type to a non-const type in the function call. But, I think C++
would have to allow this:
char *string = "I'm in write-only memory! Hahahaha!"; // ah, chardonnay
Were you using any optimization levels? One would think that could be
allowed to edit string literals, but I'm pretty rusty on my C. Josh?
Jon
--
Jon Stewart Advanced Los Angeles C++
stew1@xxxxxxxxxxx http://www.alacpp.org
_______________________________________________
alacpp mailing list
alacpp@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/alacpp