POINTER VS VARIABLE

What do the two examples/variants? DIFFERENCE? POINTER-Dynamic memory and Variable-Static memory?

1
2
3
4
5
6
7
8
9
10
11
12
13
V1:
int a;
a = 1;
cout << a << endl;
cout << &a << endl;


V2:
int * b = new int;
*b = 1;
cout << *b << endl;
cout << b << endl;
delete b;


Does i need to use pointer only if I want to delete it and free the memory from it and its addres too, as i dont want memory leaks? Pointers are used to create dynamic memory? Does the simple variable from V1 is delete and free? Thank you. Does we need to replace simple variable to pointer for small amount of used memory?(V2:"delete b;")
Last edited on
http://stackoverflow.com/questions/2264969/why-memory-allocation-on-heap-is-much-slower-than-on-stack

Generally, if you don't have to - don't allocate memory dynamically. Also, using variables that aren't created using new keyword, you don't have to care about cleaning up.

Also, you forgot to change from V2 *a to *b :)
If you aren't sure how do these work, you may check reference page from this site. It contains useful C++ tutorial.
Sorry, I edit it and i come to C++ because visual basic 6 wasnt very good.
You say this but if you make a giant structure in a giant program , you must be crazy if you don`t use pointer and then don`t delete it!
Does i need to use pointer only if I want to delete it and free the memory from it and its addres too, as i dont want memory leaks?
Yes, but think of it this way.

You went to the heap as said, "Can I have an int?" The heap makes one up and gives you its address. So, when you're done, you're expected to give it back. That's what new/delete do.

You need to put this int somewhere, and the place to store it is in a variable of type pointer to an int. But that pointer is just another variable.

Not all pointers point to objects from the heap. But if the heap gives you an object, you are expected to give it back when you're done with it.

Pointers are used to create dynamic memory?
No. Pointers can be used to store the address of objects form the heap, plus other stuff.

Does the simple variable from V1 is delete and free?
No. In V1, when make the declaration:
 
int a;
the compiler creates space on the runtime stack for the int. When the function returns, the space is released automatically. That's why such variables are called automatic variables.

Does we need to replace simple variable to pointer for small amount of used memory?
There are two levels to this. First, you need to understand how the mechanism works. Second, you need to understand how the feature is used. There are two different things.

Let's take another look.

V1 uses an int that's created on the stack. Space for it is automatically created for it on the stack by the language. The space is released automatically when the function returns.

V2 uses an int that's created on the heap. Space for it is manually requested and it must be manually returned to the heap.

That's how it works. But why might you need to use it?

C++ supports procedural and object oriented programming. The two paradigms are different and demand different treatment of things.

Procedural programming tends to used user defined types directly. Pointers are used, but only to suit the task at hand.

Object oriented programming in C++ only works thru pointers. So if you're doing object oriented programming in C++, object should be allocated dynamically and must be used via pointers (or references).

So learn the basics first because you'll need them later. If you don't you'll be forever confused.

You say this but if you make a giant structure in a giant program , you must be crazy if you don`t use pointer and then don`t delete it!


If you are asking this question, I assume that you aren't creating giant program :) Anyway, I think that kbw explained everything clearly.
So C++ is crippled for garbage collector. And Java? Thank you, kbw. I want to ask another question, is there pointers in C#? "And what about You say this but if you make a giant structure in a giant program , you must be crazy if you don`t use pointer and then don`t delete it!"???????????
From http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/ I understand that structure is stack?????
Last edited on
C++ lack of automatic garbage collection. You can use external libraries to help you with that.

Java, afaik, has automatic garbage collection.

http://bit.ly/y5LmSZ

Actually you could just google all these questions, but whatever, you're welcome. Good luck!
The significant difference between Java and C++ is that C++ supports the Procedural and Object oriented paradigms. Java has direct support of the Object oriented paradigm (and with a good kicking, partial support of the Aspect oriented paradigm).

So moving from Java to C++, you'll see similar syntax, but Java has stuff going on under the covers for that support where as C++ is What You See Is What You Get.

For example, objects are accessed via indirection (pointers or references). But Java tries to hide the notion of a pointer and restricts their use to pointing to objects.

In Java, objects are reference counted and garbage collected at some asynchronous indeterminate time. In C++, object destruction is deterministic and reference counting is done via libraries.

So back to your questions.

So C++ is crippled for garbage collector. And Java?
Java's object model includes garbage collection, it's a language feature. C++ does not have built in garbage collection. The requirement for garbage collection in Java means it cannot support RAII and object shutdown is manual thus error prone.

I want to ask another question, is there pointers in C#?
Think of C# as Microsoft's version of Java with no support for backward compatibility.
Last edited on
So C++ is crippled for garbage collector.

C++ gives you the power to choose exactly how and when you do your garbage collection, rather than having a particular mechanism forced onto you.

The current (C++11) standard supplies you with some very useful tools, such as shared pointers, that will do some automatic garbage collection for you if you want to use them. For people using older versions of C++, the boost 3rd-party libraries are free, robust and give you similar tools.

This, in a nutshell, is the principle of C/C++ - it gives you lots of powerful, flexible tools, so that the developer can write the software to behave the way s/he wants. But, as a wise man once said, with great power comes great responsibility - the responsibility to code for the garbage collection yourself, for example.

Some other languages force built-in mechanisms on you. The benefit of this is convenience for the developer, but the cost is in flexibility and power.

You pay your money and you take your choice...
Topic archived. No new replies allowed.