0

I realized that in c++ I cannot initialize an array of double with memset. For an integer array I can easily initialize the array using:

int* a = new int[n];
memset(a, n*n, n*sizeof(int));

But how can I initialize an array of doubles in c++?

long double* d = new long double[n];
memset(d, n*n, n*sizeof(long double)); // does not work
orezvani
  • 3,595
  • 8
  • 43
  • 57
  • 4
    Try [`std::fill`](http://stackoverflow.com/q/8848575/) – Nemo Oct 02 '14 at 01:18
  • 1
    Why not use universal initialization syntax? `auto x = new T[N]{};` still, that's only good if you want to zero-initialize... – Deduplicator Oct 02 '14 at 01:20
  • @Nemo fill is used for `std::arrray`, is it used for my c style arrays? – orezvani Oct 02 '14 at 01:20
  • 1
    @Deduplicator: Because he's trying to initialize to a non-zero value. – Benjamin Lindley Oct 02 '14 at 01:20
  • @Deduplicator: That does not allow you to specify the value to initialize the elements with. – Remy Lebeau Oct 02 '14 at 01:21
  • 4
    `std::fill` will work on any iterable container. So `std::fill(d, d+n, n*n)` should do what you want – Nemo Oct 02 '14 at 01:21
  • @Deduplicator The syntax is not limited to default-initialization. You can also do `new double[n]{42}`. – 5gon12eder Oct 02 '14 at 01:25
  • @5gon12eder: Sure, but only for those elements you designate at compile-time. – Deduplicator Oct 02 '14 at 01:26
  • @Nemo, I guess c++11 should be enabled, I am trying it ;) – orezvani Oct 02 '14 at 01:26
  • 1
    `std::fill` has been around since C++98 with the same semantics. And most compilers optimize it pretty well. – Nemo Oct 02 '14 at 01:26
  • @Nemo using g++ must be enabled with the -std=c++11 or -std=gnu++11. But it worked. Cheers – orezvani Oct 02 '14 at 01:30
  • @Deduplicator I have again learned something. ;-) – 5gon12eder Oct 02 '14 at 01:36
  • @Nemo, is there any other option? because it seems that I don't have any of c++11 and gnu++11 available on my test machine (and obviously I can't install them) – orezvani Oct 04 '14 at 09:58
  • @emab: Again, `std::fill` has been in standard C++ since the original standard was ratified in 1998. If you seem to need C++11, you are doing something wrong... Did you remember to `#include `, for instance? If you can post a short test program in a different question that does not work, someone will surely help you fix it. – Nemo Oct 04 '14 at 14:44

1 Answers1

-2
memset(a, n*n, n*sizeof(int));

This is probably not doing what you expect. The doc says that the value param is converted to an unsigned char. Each byte will be set to that value. When you look at the 4 or 8 bytes that comprise an int, you'll get junk. The only useful value I've seen passed in is 0. That will work to init a double as well

The only way to initialize an array of numerics to a known value, like 42, is to write a loop

KC-NH
  • 748
  • 3
  • 6
  • 2
    "That will work to init a double as well" - Not in portable code. The spec permits `0.0` to be represented in ways other than all-zero. – Nemo Oct 02 '14 at 01:28
  • 1
    You don't need to write a loop. Use `std::fill` – Neil Kirk Oct 02 '14 at 01:31
  • Every modern CPU architecture I'm aware of uses IEEE floating point, for which all bytes zero is the representation of 0.0. I'm familiar with the old VAX format, but it also used all bytes zero to represent 0.0. While the spec permits other architectures, it's not something I worry about. – KC-NH Oct 02 '14 at 01:36