-1

When specifying the data type when creating stacks, I came upon two different implementations.

stack<string>* stringStack = new Stack<string>();

Stack<string> stringStack = new Stack<string>();

What does the pointer do to the stack?

For example, my professor provided us with the code:

stack<std::string>* stringStack = new Stack<std::string>();

stringStack->push(“Jim”);
std::string top = stringStack->peek();
std::cout << top << “ is at the top of the stack \n”;

Here, my professor used the first scenario.

When I searched online for solutions, I found the code

Stack<string> stringStack = new Stack<string>();
stringStack.push(17);        
int a = stringStack.pop(); 

I'm confused to what the difference is. If someone could explain to me what the difference is, if there is or not, that would be great!

Chlo
  • 11
  • 2
  • 1
    Your code is still wrong in several ways. `c++` is case sensitive. Also you have an extra `string` inbetween the type and variable. Even if that was fixed the second example is still wrong since you would not use new. I am now with @FrançoisAndrieux on this. I am not sure what you want since the code is too wrong. – drescherjm Sep 17 '18 at 22:30
  • Is the new edit more clear? @drescherjm – Chlo Sep 17 '18 at 22:38
  • 1
    `Stack stringStack = new Stack();` is likely illegal. – drescherjm Sep 17 '18 at 22:39
  • `stack* stringStack = new Stack();` you are mixing `stack` and `Stack()`. `c++` would recognize these as 2 different things because the language is case sensitive. – drescherjm Sep 17 '18 at 22:41
  • besides those errors, would you know what the two differences are, with and without the asterisk? Are they used in different scenarios? @drescherjm – Chlo Sep 17 '18 at 22:42
  • Without the asterisk does not compile in `c++`. Is that `java`? – drescherjm Sep 17 '18 at 22:42
  • `Stack* stringStack = new Stack();` is correct code, once the `stack`->`Stack` typo is fixed, but probably the wrong way to do this. `Stack stringStack;` should do everything you need without messing around with a (probably) unnecessary dynamic allocation you have to manage. – user4581301 Sep 17 '18 at 22:44
  • @Chlo This is a fairly foundational question, basically "what is a pointer". The problem with asking such foundational questions on StackOverflow is that answering it effectively would basically mean writing you a book on C++. People have tried to answer some variants, e.g. ["Why use pointers"](https://stackoverflow.com/questions/22146094/), but I think making a stab in the middle is not as good as starting from the basics and working up. – HostileFork says dont trust SE Sep 17 '18 at 22:45
  • Ah come on, @HostileFork . Two, maybe three, chapters of a text book, tops. – user4581301 Sep 17 '18 at 22:46
  • On Ubuntu 18.4, g++ 7.3, the code"cout << std::stackOf1000Ptrs.size()\n" reports 1000 elements, and "cout << sizeof(stackOf1000Ptrs)\n" reports 80 bytes. The 1000 elements are in dynamic memory, not automatic. The stack object is in automatic memory, and remains small regardles of how many elements. – 2785528 Sep 17 '18 at 23:19

1 Answers1

1

When I searched online for solutions

That's your problem right there. Don't go searching the Internet and copying/pasting code you don't understand. That's for after you graduate.

I found the code

Stack<string> stringStack = new Stack<string>();
stringStack.push(17);        
int a = stringStack.pop();

That won't compile. new is for doing dynamic allocations, and returns a pointer. What you have on the left hand side (Stack<string> stringStack) is not a pointer.

The "mysterious" * you see annotating the left hand side (in the correct code) denotes a pointer. When it's not part of a type, it is a dereference operator, which means "look up what this pointer points to".

Dynamic memory allocation must be done in pairs... a new and a delete, otherwise you will leak. Demonstrated briefly:

{ // a scope begins
   int x = 10; // not a pointer; stack allocated
} // a scope ends, integer automatically is gone

{ // a scope begins
   int *px = new int; // px points to integer-sized memory box
   *px = 10; // put 10 into that box
} // a scope ends, you didn't delete px, you leaked memory

The question of whether you should dynamically allocate something or not is discussed here, and maybe you'll find something of value in it:

Why should I use a pointer rather than the object itself?

But to confuse you further, if your professor was truly teaching "modern C++", then you'd be warned against the casual use of raw pointers:

What is a smart pointer and when should I use one?

I'll reiterate that the best thing you can do for yourself is to not try to shortcut by searching for solutions on the Internet. Follow your coursework from the beginning, and if you feel that your course or professor are lacking then supplement your education by working through a good book on your own.