2

again this question is also origined from "Thinking in C++" Chapter7, Q#7. I believe the Stack header file should be changed to Stack.h

        #ifndef STACK_H
        #define STACK_H

        class Stack {
          struct Link {
            void* data;
            Link* next;
            Link(void* dat, Link* nxt);
            ~Link();
          }* head;
        public:
          Stack();
          Stack(void* arr[], int size);
          ~Stack();
          void push(void* dat);
          void* peek();
          void* pop();
        };

and the implementation of Stack::Stack(void* arr[], int size) in Stack.cpp, I believe could be like:

       Stack::Stack(void* arr[], int size)
       {
         for (int i=0; i<size; i++)
          {
            push(arr[i]);
          }
       }

However, in the main test file StackTest.cpp, how could I pass the address of a string array to this constructor? Here is what I come up with:

          #include "Stack.h"
          #include "require.h"
          #include <iostream>
          #include <string>
          using namespace std;

          int main() {

            string tst_arr[] = {"hi 1", "hi 2", "hi 3"};
            Stack string_arr((void**)tst_arr, 3);
            string* s;
            while((s = (string*)string_arr.pop()) != 0) {
              cout << *s << endl;
              delete s;
            }
          } 

But it has some segmentation fault. What I could think of is to change Stack::Stack(void* arr[], int size) to Stack::Stack(string arr[], int size), however it doesn't satisfies the question requirement. The purpose of Stack to store generic objects, including string for example. I believe I still have difficulty to understand the conceipt of void* pointer and array of pointers and the chagne between string array to void* array etc... Anyone could help me solve this problem? Thanks a lot!!

4
  • (void **)tst_arr is technically wrong, though it might not be the source of your problem. Commented Mar 29, 2011 at 23:56
  • 2
    Eh, I'd say abandon this insanity. You won't learn anything useful this way (void* shouldn't be used in C++ at all) anyway. Learn templates first and then try to write your stack (and then again, don't use it in the real code — use standard library containers instead). Commented Mar 29, 2011 at 23:57
  • 2
    "Thinking in C++" is obviously a misnomer. This guy thinks in C. Commented Mar 30, 2011 at 1:24
  • Every time you end up with void** you know you are lots. Just leave it alone! Commented Mar 30, 2011 at 7:33

1 Answer 1

2

Your Stack constructor asks for an array of pointers on stuffs, and you give it an array of objects. As a bonus, I give you proper main function and freeing memory ^^

#include <cstdlib>
// --- Includes your stuffs ---

using namespace std;

int main(int argc, char* argv[]) {
  string* tst_arr[] = new string[3];
  tst_arr[0] = new string("hi 1");
  tst_arr[1] = new string("hi 2");
  tst_arr[2] = new string("hi 3");
  Stack string_arr((void**)tst_arr, 3);

  // --- Do your stuffs ---

  for(int i =0; i < 3; ++i)
    delete tst_arr[i];
  delete[] tst_arr;

  return EXIT_SUCCESS;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Please don't try this at home kids! :-) There is a std::stack just because of this.
thanks for your reply, I believe the array initialization should be changed to: string* tst_arr[3] = { new string("hi 1"), new string("hi 2"), new string("hi 3")}; otherwise, the complier will give you some error as above. for the memory free, I'm still digging into it, since Stack->pop already freed the pointer, I don't think either delete tst_arr[i] or delete[] tst_arr is necessary based on my initialization, right?
to Bo: imagine you are asked to implement a stack youself in an interview, can you say "there is std::stack existed, kid, don't you know that?" And I think it makes sense, even wheel do exist, there must be someone who know how to make them, right?
So if your pop function call delete, then yes, no need to delete like I did on my snippet. But in general, it's nice to separate things the jobs, here being memory management and a stack. By doing the delete in your pop method, your stack also takes the role of memory management. You might need to stack pointers without deleting them, happens quite often, like when working with trees or graphs...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.