2

Today I came across a new type of allocation in cpp which I never heard of any I tried to search google but didn't find any related answer.

long int *a=new long int[N+1]();

I know long int *a = new long int[N+1];

But what is the difference between the two above?

Brij Raj Kishore
  • 1,595
  • 1
  • 11
  • 24
  • 4
    `()` in the end just means "and initialize all elements to zero" (or to be precise, "[value-initialize](http://en.cppreference.com/w/cpp/language/value_initialization) all elements", but for `long int` this means "set to zero"). – Igor Tandetnik Nov 16 '17 at 04:01
  • but without (), I am printing array elements and they are also 0. See this [code](http://ide.geeksforgeeks.org/f6QOIg) – Brij Raj Kishore Nov 16 '17 at 04:05
  • It means that the random memory that `new` chose to allocate was all set to `0`s. This is not guaranteed. Consider making `N` a large number - for sure you will see some other values than `0`s. BTW, @IgorTandetnik, that should be the answer – Fureeish Nov 16 '17 at 04:07
  • can you provide any reference to your answer? Because I have submitted my code by both methods with and without brackets and both were accepted. – Brij Raj Kishore Nov 16 '17 at 04:09
  • 1
    Please, do not **ever** rely on online judges when it comes to language knowledge and how it works – Fureeish Nov 16 '17 at 04:10
  • That's why I am asking the proper reference and documentation for this. – Brij Raj Kishore Nov 16 '17 at 04:12
  • 1
    Without parentheses, elements are [default-initialized](http://en.cppreference.com/w/cpp/language/default_initialization), which for `long int` really means "leave uninitialized". Are the two links to [cppreference.com](http://cppreference.com) proper enough for you? If you insist on chapter and verse, the relevant section of the standard is [**dcl.init**](http://eel.is/c++draft/dcl.init) – Igor Tandetnik Nov 16 '17 at 04:15
  • 1
    @BrijRajKishore -- *Because I have submitted my code by both methods with and without brackets and both were accepted* -- [See this](http://rextester.com/HBJXY67256). See the first set of numbers are not 0. Second, get rid of the `#include ` stuff -- include the proper headers. Last, I know you're not familiar with that syntax, but that syntax is well-known by seasoned C++ programmers who know exactly what it means. An explanation without having to go to chapter and verse of the C++ standard would have, or should have been enough. – PaulMcKenzie Nov 16 '17 at 04:59
  • @PaulMcKenzie [See this](http://rextester.com/HBJXY67256) link is not working. – Brij Raj Kishore Nov 16 '17 at 05:21
  • The link works correctly for me. It links to the online Visual C++ compiler, and it shows that the first 3 values in the array are not 0. – PaulMcKenzie Nov 16 '17 at 05:26
  • @PaulMcKenzie Sorry not even [rextester.com](http://rextester.com/) is working for me. It is displaying DNS error. Can you show me your code on another website. Thanks – Brij Raj Kishore Nov 16 '17 at 05:34
  • @BrijRajKishore -- Place your code [here](http://webcompiler.cloudapp.net/). Use `#include ` (which is why you should never use that "bits..." header), and you will see that the output is not all 0. – PaulMcKenzie Nov 16 '17 at 11:09

2 Answers2

1
long int *a = new long int[N+1]();
                               ^^

Allocate memory, and initialize them to the default state of the type (for built-in types, zero). It's the standard way to initialize objects allocated by new so they do not contain indeterminate values (no UB, though). In C++11, you can also use curly brackets:

long int *a = new long int[N+1]{};
                               ^^
iBug
  • 35,554
  • 7
  • 89
  • 134
0

long int *a=new long int[N+1](); is the value initialization of int so each element of a is automatically initialized to 0, whereas in the second instance it just creates an array of size N+1 which may contain anything. In that case, you must assign a value to that variable before using it or invoke undefined behavior.

Stephen
  • 1,498
  • 17
  • 26
  • "*In that case, you must assign a value to that variable before using it or invoke undefined behavior*" - what..? That is **not correct**. There is no UB in: `int *a=new int[10]; std::cout << a[2]` for example... – Fureeish Nov 16 '17 at 04:18
  • 1
    @Fureeish Is too. "**[dcl.init]/12** If an indeterminate value is produced by an evaluation, the behavior is undefined" – Igor Tandetnik Nov 16 '17 at 04:25
  • 1
    @Fureeish just because you can do it, does not mean it isn't undefined behavior. The nature of undefined behavior comes in the fact that you don't know what will be inside the allocation in the second case, whereas you would in the first case. You can't perform arithmetic on an uninitialized variable, making it effectively useless. So yes, it is undefined behavior. – Stephen Nov 16 '17 at 04:25
  • So simply outputting that value is considered UB? – Fureeish Nov 16 '17 at 04:27
  • @Fureeish Just writing `a[2];` on a line of its own would exhibit undefined behavior. It evaluates an expression whose result is an indeterminate value. – Igor Tandetnik Nov 16 '17 at 04:28
  • Got it. Will leave the comment as a root for this discussion. Thank you for clarification – Fureeish Nov 16 '17 at 04:29
  • @IgorTandetnik That would not be UB; discarded-value expressions do not perform lvalue-to-rvalue conversion in C++ – M.M Nov 16 '17 at 04:50