 |
 |
|
 |
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Darren New wrote:
> Well, that made *me* laugh.
>
> "Nano is awful. I have nothing better. Oh, except the two most popular
> text editors."
Have *you* ever tried to make Vim or Emacs work? :-P
Besides, Emacs isn't even a text editor - it's an operating system! ;-)
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Orchid XP v8 wrote:
> Spot the mistake:
>
> int foo(vector<int>& stack)
> {
> int x = stack.back();
> stack.pop_back();
> }
>
> For reasons I can't begin to understand,
>
> 1. This doesn't produce any compile-time error.
>
> 2. This function always returns the value "2144912088".
Function lacks a return statement.
--
Inoculatte: To take coffee intravenously when you are running late.
/\ /\ /\ /
/ \/ \ u e e n / \/ a w a z
>>>>>>mue### [at] nawaz org<<<<<<
anl
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Mueen Nawaz wrote:
> Orchid XP v8 wrote:
>> Spot the mistake:
>
> Function lacks a return statement.
Yup. A simple and obvious mistake. But unfortunately, not one the
compiler warned me about. (I'm also curios to know exactly what the hell
the function is returning... No doubt the reason there's no compiler
warning is that this is actually a valid thing to do due to some bizare
C language rule kept for compatibility or something random like that.)
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Orchid XP v8 a écrit :
> Mueen Nawaz wrote:
>> Orchid XP v8 wrote:
>>> Spot the mistake:
>>
>> Function lacks a return statement.
>
> Yup. A simple and obvious mistake. But unfortunately, not one the
> compiler warned me about. (I'm also curios to know exactly what the hell
> the function is returning... No doubt the reason there's no compiler
> warning is that this is actually a valid thing to do due to some bizare
> C language rule kept for compatibility or something random like that.)
>
If you are using g++, you can add the option -Wall to your command line.
The compiler should give a warning in this case... Well two actually,
one because the variable x is not used for anything and one because the
function does not return.
I'm leaving the explanation of why this is still legal to someone more
competent :-)
--
Vincent
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Orchid XP v8 wrote:
> Have *you* ever tried to make Vim or Emacs work? :-P
I have used both for years at a time. I prefer vim, except for one
stretch of time when it was still "vi" and I wanted automatic
indentations for Tcl.
--
Darren New / San Diego, CA, USA (PST)
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Invisible wrote:
> Somehow... it seems so much easier in Haskell. ;-)
I understand the C++ one and the Haskell one looks like line noise.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Invisible wrote:
>>> ...you mean, you can take a string and make a stream that will read from
>>> it? (Or, presumably, write to it, if you desire.)
>>
>> Correct. Or, more precisely, you can write to an ostringstream and then
>> get a regular string out of it (with the str() function).
>
> Ah - because a string is fixed-size once created?
No. std::string is mutable, and can change its size.
This is how you can convert numbers to/from strings:
int answer = 42;
std::ostringstream oss;
oss << answer;
//oss.str() returns a std::string containing "42".
-------------
int answer;
std::istringstream iss("42");
iss >> answer;
//answer now contains the number 42.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Orchid XP v8 wrote:
> Darren New wrote:
>
>> Well, that made *me* laugh.
>>
>> "Nano is awful. I have nothing better. Oh, except the two most popular
>> text editors."
>
> Have *you* ever tried to make Vim or Emacs work? :-P
Vim is my main text editor. Even on Windows.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Invisible wrote:
> Warp wrote:
>
>> No need for atoi(). You can use stringstreams, as I wrote in the other
>> post.
>
> I did think using a C function seemed a little bogus.
>
> (For one thing, AFAIK, there is *no way* to know whether atoi()
> succeeded or failed; if it "fails", you get a zero. So was the string
> actually a zero, or did the conversion fail??)
atoi is always a bad idea.
If you're writing C, you should use strtol instead. If you're writing C++,
you should use a stringstream instead.
Or use boost lexical_cast, which looks like a cast but works with streams to
do string-number conversions. But any boost library involves lots of
inter-library dependencies, and would surely increase your compilation
times (the compiler has to untangle all the templated mess :P). So just
stick to streams.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Orchid XP v8 wrote:
> (I'm also curios to know exactly what the hell
> the function is returning...
Whatever random value happened to be in the appropriate place of the stack.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |