M
Mark S.
Hi,
I'm working on the "Thinking in C++" exercises and have the problem that
while I did make the code work, I don't know why. Here is the code:
/* Use aggregate initialization to create an array of string objects.
Create a Stack to hold these strings and step through your array,
pushing each string on your Stack. Finally, pop the strings off your
Stack and print each one. */
#include "../../C04/Stack.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s[] = { "This ", "is ", "a ", "very strange test." };
string* sp;
Stack test;
test.initialize();
for(int i = 0; i < sizeof(s) / sizeof*(s); i++)
{
cout << "Adding " << s << endl;
test.push(new string(s)); // works
// test.push(&s); // works but crashes at the end!
}
while((sp = (string*)test.pop()) != 0)
{
cout << *sp << endl;
delete sp;
}
test.cleanup();
}
Stack.h and Stack.cpp can be found here (the exercise 6.8):
http://www.linuxtopia.org/online_books/programming_books/thinking_in_c++/Chapter04_016.html
The output is:
Adding This
Adding is
Adding a
Adding very strange test.
very strange test.
a
is
This
Stack empty
My problem is that when I use test.push(&s); instead of test.push(new
string(s)); , the program compiles and runs (output is the same), but
crashes at the end. I don't understand this - both are using the address
of a string and obviously the elements have been added successfully
since they are can be called with pop().
I did some testing and it looks to me (who is new at this) that the
error is here:
(sp = (string*)test.pop()) != 0
sp should be 0 when the head is empty, but somehow with &s, this
isn't the case. BUT WHY???
Thank you for your time!
Kind regards
Mark
I'm working on the "Thinking in C++" exercises and have the problem that
while I did make the code work, I don't know why. Here is the code:
/* Use aggregate initialization to create an array of string objects.
Create a Stack to hold these strings and step through your array,
pushing each string on your Stack. Finally, pop the strings off your
Stack and print each one. */
#include "../../C04/Stack.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s[] = { "This ", "is ", "a ", "very strange test." };
string* sp;
Stack test;
test.initialize();
for(int i = 0; i < sizeof(s) / sizeof*(s); i++)
{
cout << "Adding " << s << endl;
test.push(new string(s)); // works
// test.push(&s); // works but crashes at the end!
}
while((sp = (string*)test.pop()) != 0)
{
cout << *sp << endl;
delete sp;
}
test.cleanup();
}
Stack.h and Stack.cpp can be found here (the exercise 6.8):
http://www.linuxtopia.org/online_books/programming_books/thinking_in_c++/Chapter04_016.html
The output is:
Adding This
Adding is
Adding a
Adding very strange test.
very strange test.
a
is
This
Stack empty
My problem is that when I use test.push(&s); instead of test.push(new
string(s)); , the program compiles and runs (output is the same), but
crashes at the end. I don't understand this - both are using the address
of a string and obviously the elements have been added successfully
since they are can be called with pop().
I did some testing and it looks to me (who is new at this) that the
error is here:
(sp = (string*)test.pop()) != 0
sp should be 0 when the head is empty, but somehow with &s, this
isn't the case. BUT WHY???
Thank you for your time!
Kind regards
Mark