-2

Note that I'm learning on Visual C++.

As I know, we should zero a dynamic array this way:

void main()
{
    int *arr = new int[15];
    memset(arr,0,15*sizeof(*arr));
    ...
    delete [] arr;
}

But I found another way to do the same:

void main()
{
    int *a = new int[15]();
    ...
    delete [] arr;
}

As you can see, I just added two parentheses after the brackets. But it works only if I set the size of the array.

I can't find any information on this, and I don't know what purposes this is needed for.

EDIT: First code is edited and verified by VC compiler now. EDIT2: So I can't find manual about 2nd code on MSDN. There I can find this one? (No Google plz)

6
  • The title really isn't informative, I don't have a better suggestion though
    – Marco A.
    Sep 9, 2014 at 12:57
  • The first version shouldn't even compile. But in general, profile and see for yourself what is faster. I doubt there will be a difference. Sep 9, 2014 at 12:57
  • 2
    If you don't care about correctness, and you want code that is fast, just remove all your code. Of course, your two versions do quite different things, so comparisons of correctness and performance are pointless. Sep 9, 2014 at 12:58
  • 6
    It could be said that the correct way is to use something like std::vector.
    – Biffen
    Sep 9, 2014 at 12:58
  • 2
    Your first version is wrong - you need to specify the array size, and sizeof(*arr) is the same as sizeof(int) - not the size of the array.
    – molbdnilo
    Sep 9, 2014 at 13:01

2 Answers 2

8

Indeed, adding parentheses specifies value initialisation, setting simple types like int to zero.

The first snippet is (or was, before the question was edited) wrong for two reasons:

  • You don't specify the size of the array in the new-expression, e.g. new int[15]
  • You only set the first element to zero. Multiply by the size of the array, or do something less error-prone instead.

In general, don't mess around with low-level memory allocation; use friendly types that take care of memory for you:

#include <vector>

int main() {                    // 'void' is also wrong here
    std::vector<int> arr(15);   // dynamic array of 15 'int' values, zero-initialised
    // no need to delete anything
}
1
  • FYI: OP has modified the post after your answer
    – Marco A.
    Sep 9, 2014 at 13:10
0

I can't find any information on this, and I don't know what purposes this is needed for.

This is initialization code, introduced in the language (if I remember correctly) to allow for native types to be treated like generic types, when writing templates and classes.

Here's an example:

struct S { int x; S(int a) : x(a) {} }; // structure with constructor taking an int
                                        // observe constructor syntax for int member

// example factory function (not very usefull in practice but good example)
template<typename T>
T make_instance( int x ) { return T(x); /* use old syntax for constructor call */ }

auto s = make_instance<S>(10); // create S instance with value 10
auto i = make_instance<int>(10); // create integer with value 10

In this example, the template constructs an instance of any type that supports a constructor taking an int. The second example (initializing an int) will only work if the language supports initializing an integer with the following syntax:

int x(10);

This is constructor syntax. The language also supports calling the "default constructor" for a native value (i.e. calling the constructor with no arguments, which for an int sets the value to zero). This is the case in your example (calling default implicit int "constructor").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.