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++?

7
  • You should really try and research on your own before posting here. Commented Oct 16, 2012 at 4:19
  • And keep in mind that C++ is not garbage collected... Commented Oct 16, 2012 at 4: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. Commented Oct 16, 2012 at 4:26
  • @ixe013 But that's fine here though right? Because it's on the stack, not the heap. Commented Oct 16, 2012 at 4:27
  • @xori : new means heap (by default). Commented Oct 16, 2012 at 4:28

3 Answers 3

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}});
}
Sign up to request clarification or add additional context in comments.

7 Comments

"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"
@EdS.: Right, I added a note about that.
So does the compiler put that declaration on the heap?
@EdS.: The elements of std::array are on the stack if you instantiate std::array on the stack. See here. It's basically a struct with an array as a member.
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
|
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.

Comments

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});

3 Comments

I believe (int[]){1,2,3} is only in C (it's a compound literal).
@JesseGood GCC supports compound literals in C++, though the semantics are somewhat different than in C.
it works with const ... e.g. double arr_func( int n, const double * xs ){ }; arr_func( 3, (const double[]){1.0,2.0,3.0} );

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.