5

With the defined function as...

void func (int a [3]) { ... }

I'd like to be able to pass a dynamic static array like so

func (new int [3] {1,2,3});

instead of...

int temp [3] = {1,2,3};
func(temp);

Is this possible with C++?

xori
  • 716
  • 7
  • 9
  • You should really try and research on your own before posting here. – C0D3 Oct 16 '12 at 04:19
  • And keep in mind that C++ is not garbage collected... – ixe013 Oct 16 '12 at 04:25
  • @c0d3Junk13 I have, but I haven't been able to find a post telling me explicitly that I can't do this. I just don't understand how the compiler can't put that on the stack. – xori Oct 16 '12 at 04:26
  • @ixe013 But that's fine here though right? Because it's on the stack, not the heap. – xori Oct 16 '12 at 04:27
  • @xori : `new` _means_ heap (by default). – ildjarn Oct 16 '12 at 04:28
  • @xori: `new` == dynamic memory allocation (unless it's been overloaded to do something else, but that's probably not the case here). Some objects have automatic storage duration (i.e., "on the stack" in practical terms, but the stack is an implementation detail) and some have to be managed by you. In this case you are leaking memory. – Ed S. Oct 16 '12 at 04:28
  • The pointer to the memory is put on the stack. The memory itself is not, you must manage it. Follow @JesseGood advice – ixe013 Oct 16 '12 at 10:48

3 Answers3

4

If you have a C++11 compiler, func (new int [3] {1,2,3}); would work fine (but, remember that you are responsible for deleteing the memory). With gcc, just pass the -std=c++11 flag.

However, look into std::array so you don't have to use new:

Example

void func (std::array<int, 3> a)
{
}

int main()
{
    func({{1, 2, 3}});
}
Jesse Good
  • 50,901
  • 14
  • 124
  • 166
  • "would work fine" as in "the code runs and produces the expected output, but leaks memory because there is no matching call to `delete` in the example" – Ed S. Oct 16 '12 at 04:30
  • @EdS.: Right, I added a note about that. – Jesse Good Oct 16 '12 at 04:34
  • So does the compiler put that declaration on the heap? – xori Oct 16 '12 at 04:35
  • @xori: `std::array` is fixed at compile time and is on the stack. – Jesse Good Oct 16 '12 at 04:36
  • @xori: an instance of `std::array` is allocated with automatic storage duration ("the stack"), but it allocates its data store dynamically. You don't have to worry about it though as it will clean up after itself. – Ed S. Oct 16 '12 at 04:59
  • 1
    @EdS.: The elements of `std::array` are on the stack if you instantiate `std::array` on the stack. [See here](http://stackoverflow.com/questions/4424579/c-stdvector-vs-stdarray). It's basically a `struct` with an array as a member. – Jesse Good Oct 16 '12 at 05:08
  • 1
    Oh wow, now *that* I did not know, thanks. I can't say I've ever actually used std::array, but I routinely learn new things browsing around questions here. Thanks again. +1 – Ed S. Oct 16 '12 at 05:23
2

It works on my compiler (Eclipse indigo with CDT extension), but it gives a warning that implies it may not work in all environments:

extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]

Here's my test code:

void func (int a[3])
{
cout << a[2];
}

int main()
{
    func(new int[3]{1,3,8});
}

successfully prints "8" to the console.

2

Your example with new int [3] {1,2,3} works just fine, but it allocates an array on the heap each time it is called.

You should be able to do it on the stack using a typecast:

func((int[]){1,2,3});
scientiaesthete
  • 919
  • 9
  • 20