-9

I have 3 questions:

Q1. When we create the object using the new operator, following is the syntax:

pointer_variable = new data-type //To create an object
pointer_variable = new data-type(value); //To create an object with value 
pointer_variable = new data-type[size]; //To create an array

Why always there is a pointer_variable on L.H.S?

Q2. What is the difference between declaring and assigning pointers with and without the new operator?

Consider the following code snippet and output to understand the question:

    int a = 10, b=20;
    int *p;
    p = &a;
    int *q = new int;
    q = &b;
    cout<<"P is: "<<p<<" : "<<*p<<endl<<"Q is: "<<q<<" : "<<*q<<endl;

Output of the above code:

P is: 0x61ff04 : 10
Q is: 0x61ff00 : 20

Q3. When we say, with a new operator we can dynamically allocate memory to the array at run time when we don't know the size of the array at compile time. We can do this without new operator as given below:

    cout<<"Enter the size of an array"<<endl;
    int n;
    cin>>n;
    
    int arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    for(int i=0;i<n;i++)
    {
        cout<<arr[i];
    }

Then what is exactly the need to use the new operator for arrays?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 3
    Don't ask multiple questions in one post – kesarling He-Him Aug 04 '20 at 06:21
  • 4
    [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Blastfurnace Aug 04 '20 at 06:22
  • 1
    RE: `We can do this without new operator as given below` That is a non-standard implementation and is called as variable sized array. C++ does not officially support that. The difference is, you cannot change the size of the array after the size has been allocated the way you've suggested, but you can if you allocate it with new operator – kesarling He-Him Aug 04 '20 at 06:22
  • 1
    The first two will be answered by any good book or [the reference for new](https://en.cppreference.com/w/cpp/memory/new/operator_new). The third one is wrong, you are using a compiler specific extension. See also https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard – Lukas-T Aug 04 '20 at 06:23

2 Answers2

2

Why always there is a pointer_variable on L.H.S?

Because new-expression results in a pointer.

What is the difference between declaring and assigning pointers with and without the new operator?

new-expression (not operator new) constructs a new object (and, optionally, allocates memory for it).

We can do this without new operator as given below

In fact, we cannot, according to the C++ Standard. Some compilers just allow this construct as a non-standard language extension.

Every good C++ book for beginners will explain these in more details.

Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
  • @OP I suggest you accept this answer. It is as good as it can get, and no answer will explain anything else :) – kesarling He-Him Aug 04 '20 at 06:26
  • Thanks for the clarification. for Q2, can you please explain in detail when to use int *p and when to use int *p = new int? – Krishna Upadhyay Aug 04 '20 at 08:41
  • @KrishnaUpadhyay As I wrote, you need to use `new int` if you want to create a new (dynamically-allocated) object of type `int`. – Daniel Langr Aug 04 '20 at 09:26
  • @KrishnaUpadhyay I would really strongly suggest reading some basic material about C++. Learning by asking is not a good approach if you don't understand even the language basics. – Daniel Langr Aug 04 '20 at 09:30
1

in C++, a typical new expression allocates memory on the heap, and returns a pointer to that memory.

Re Q1: you can save the resulting pointer to a local variable for immediate use: pointer_variable = new int. But you don't have to do that: you could instead use it as an argument to a function: use_pointer(new int).

Re Q2: your code allocates an int on the heap, stores its pointer in local variable q, and immediately overwrites it with the address of local variable b. So what you have done here is write a small memory leak.

Re Q3: variable-sized array is a nonstandard extension to C++, so it will not necessarily work in another compiler. However, when it does work it is just another automatic variable: it will be automatically de-allocated for re-use when you leave the local scope. This is different from new allocations, which last until they are explicitly delete-ed.

comingstorm
  • 25,557
  • 3
  • 43
  • 67
  • Will you please explain what you meant by `However, when it does work it is just another automatic variable`? I didn't quite get that. Did you mean something like auto arr[n]; is standard? – kesarling He-Him Aug 04 '20 at 06:44
  • 2
    Local variables in C and C++ are called "automatic variables" because (as I mentioned) they are automatically de-allocated at the end of their scope. As automatic variables are allocated on the stack, this is a quick and simple operation. – comingstorm Aug 04 '20 at 06:52
  • Oh!, so `auto arr[n]` is still non-standard. Got it. Thanks :) – kesarling He-Him Aug 04 '20 at 06:54