0

I'm new to C++ and currently having a hard time with pointers.

I was wondering if there is a preferred method of coding between the two methods shown below:

#include <iostream>

using namespace std;

int main(){
    // method 1
    int* n;
    *n = 2;
    cout << "address of n in main(): " << n << "\n"; //returns 0
    cout << "value inside of n in main(): " << *n << "\n"; // returns 2

    // method 2
    int* m = new int(2);
    cout << "address of m in main(): " << m << "\n"; //returns some address
    cout << "value insdie of m in main(): " << *m << "\n"; // returns 2
}

The first method returns the following:

address of n in main(): 0
value inside of n in main(): 2

The Second method returns the following:

address of m in main(): 0x6c2cc0
value inside of m in main(): 2

Q1. What would be possible issues with having the address of n in main():0? (it only behaves like this on an online compiler sorry. Nevermind this question.)

Q2. What is the "new" declaration method called?

Acorn
  • 24,970
  • 5
  • 40
  • 69
Kevin
  • 3
  • 3
  • This code doesn't compile for several formatting reasons. Please make sure what you post here is actual valid code. After I fixed the formatting errors and got it to compile, it crashed. – JohnFilleau Mar 07 '20 at 03:59
  • 4
    Method 1 invokes undefined behavior, method 2 doesn't. – Etienne de Martel Mar 07 '20 at 04:02
  • 3
    Please post code that you actually compiled and tested. The shown code definitively does not compile. You should very rarely use `new` directly. Given that you seem to be a beginner, it would be best if you forget for now that it exists. `n` should just be declared as automatic variable `int n;`. Lastly, I suggest you work through [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn the language. – walnut Mar 07 '20 at 04:10
  • In method 1, the pointer's value is uninitialized which means it's not a valid memory address. Trying to store a value at that address is like shoving your stuff into a random locker in a storage facility instead of reserving a locker. – eesiraed Mar 07 '20 at 04:28
  • `bits/stdc++.h` is specific to certain compiler(s), it is not a good idea – M.M Mar 07 '20 at 13:41

1 Answers1

1

What would be possible issues with having the "address of n in main():0"?

If you are lucky, crashing the program due to segfault. If you are not, memory corruption, undefined behavior, etc.

The reason is that you are using an effectively random memory address that has not been mapped/reserved/allocated to your process.

What is the "new" declaration method called?

There is no "name" for the "method". new is a keyword and an operator, too. It is the standard way of allocating memory in C++. Although, in almost all cases, you will use a container instead that manages the memory for you, like std::vector or std::unique_ptr.

Acorn
  • 24,970
  • 5
  • 40
  • 69
  • The [operator new](https://en.cppreference.com/w/cpp/memory/new/operator_new) and [new expressions](https://en.cppreference.com/w/cpp/language/new) are two different things. OP is asking about the latter. – walnut Mar 07 '20 at 14:37