0

what is the difference between the following block of code :

#include<iostream>
using namespace std;
int main()
{
    int *p;
    float *q;
    cout<<p<<"\n"<<q;
}

and this code :

#include<iostream>
using namespace std;
int main()
{
    int *p;
    float *q;
    p = new int;
    q = new float;
    cout<<p<<"\n"<<q;
}

In both the cases the pointers are allocated some memory right?

Then why should i use the new operator?

jogojapan
  • 68,383
  • 11
  • 101
  • 131
Ajay
  • 61
  • 3
  • 13

4 Answers4

6

No, int *p and float *p declare pointers, but they are not initialised, so they each point to a random address in memory. In particular, they do not point to memory space allocated to your program.

The second piece of code correctly allocates memory and stores the address of that memory in the two pointers. However, note that your code never de-allocates the memory, so you have a memory leak. You'd have to use delete p and delete q at the end of the program to avoid this.

jogojapan
  • 68,383
  • 11
  • 101
  • 131
  • Thanks sir. But what is a memory leak ?? – Ajay Nov 06 '12 at 03:20
  • 1
    A memory leak is when you set aside a piece of memory but when you are done you never give back the space you took, over time those little pieces can add up, eventually causing your program to crash due to a out of memory error. – Scott Chamberlain Nov 06 '12 at 03:23
  • See [this old SO answer](http://stackoverflow.com/questions/10482974/why-is-stack-memory-size-so-limited/10483147#10483147) of mine that has a good analogy explaining the difference between stack and heap. A memory leak is (from my example) when you threw away the piece of paper but you never took the box out of the other pile. When you do `int p` you are using the stack, when you are using `int *p` + `new int` you are using the heap. – Scott Chamberlain Nov 06 '12 at 03:28
  • `new T` for any type `T` allocates `sizeof T` bytes of memory. If the size of `int` and `float` is 4 bytes each (it may be different on your platform), a total of 8 bytes will be allocated by the two `new` calls. But note that, as a result of these allocations, the operating will probably set aside more memory space (presumably, at least one memory page). – jogojapan Nov 06 '12 at 03:57
  • so you are saying that "int *p" just allocates a memory for my pointer somewhere in my ram and "p = new int" allocates a memory within the provided memory for my program, right ??? – Ajay Nov 06 '12 at 04:05
  • `int *p;` creates a pointer that points _somewhere_. The address it points to may, by accident, be an address that has already been allocated to your program (e.g. by an earlier call to `new`), or it may be an address of somewhere in the middle of your stack, or even an address of a function. Most likely it will be an address that can't be interpreted because the OS hasn't allocated it, i.e. there is no mapping to physical memory for that address. Also note that the address may be different each time you run your code. In any case you **must never use such a random address**, for _anything_. – jogojapan Nov 06 '12 at 04:30
2

To answer the question in your title a bit more directly: no, you do not have to use the new operator very often in C++; in fact, in well written code you usually use it quite rarely.

In this case, rather than using pointers at all, you would typically want to just define the int and float using the auto storage class, without defining any pointers or using new at all. While you're at it, most code shouldn't have using namespace std; in it either. Fixing those, you could end up with something like this:

#include<iostream>

int main()
{
    int p = 1;
    float q = 2.0f;
    std::cout<<p<<"\n"<<q;
}

If your code has new and/or T *xxx very often (where T is some type and xxx is some identifier) chances are pretty good that it's not really a question of whether you're doing something wrong, only of how many things and how badly wrong.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • +1, admittedly, the title of the question used to be simply "new operator c++". I added "do I need to use" as that seems to capture the gist of the question. Perhaps it should be changed again, into "need to use new to allocate memory" or something like that. – jogojapan Nov 06 '12 at 03:32
  • @jogojapan: Well, I think my answer addresses not only the title, but also (at least something about) the code in the question itself. – Jerry Coffin Nov 06 '12 at 03:35
  • Absolutely, and I totally agree. – jogojapan Nov 06 '12 at 03:36
0

In the first block of code, memory is allocated for the pointers themselves, but no memory is allocated which they can point to and validly use. The address that is printed out is not safe for your program to use.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

The "pointer" variable does already being allocated in main() stack, 4 bytes in 32-bit or 8 bytes in 64-bit machines. The memory storage for int and float, which are pointed by p and q respectively, are not allocated yet in the first case. Your cout just prints out the memory address of p & q, not the value where p or q points to.

jclin
  • 2,449
  • 1
  • 21
  • 27