How do I use "new" this way? Is it even possible?

Say I want to deal with an array of pointers to functions that take no arguments and return no arguments. I can declare an array of such pointers like so:
void (*pFuncs[5])(void);
But if I want to use new to allocate memory I get stuck:
1
2
void (**pFuncs)(void); //is this right?
pFuncs = new *what to put here?*

Is this even possible?
Whenever you get the chance, use typedefs, they make things clearer

1
2
3
4
5
6
7
8
int main()
{
	typedef void(*p)();
	p * pfuncs;
	pfuncs = new p[5];
        delete [] pfuncs;
	return 0;
}
Last edited on
In addition to using typedefs, use containers so you that you don't need to use new. And you don't end up with an extra level of indirection like Smac89 did. =P

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <vector>

typedef void (*func_type)();

void func1() { std::cout << "func1\n"; }
void func2() { std::cout << "func2\n"; }
void func3() { std::cout << "func3\n"; }
void func4() { std::cout << "func4\n"; }
void func5() { std::cout << "func5\n"; }


template <typename iter_type>
void execute(iter_type beg, const iter_type& end)
{
    while (beg != end)
        (*beg++)();
}

void with_new_and_array()
{
    func_type* funcs = new func_type[5];
    funcs[0] = func1;
    funcs[1] = func2;
    funcs[2] = func3; 
    funcs[3] = func4;
    funcs[4] = func5;

    execute(funcs, funcs + 5);
    delete [] funcs;
}

void with_vector()
{
    std::vector<func_type> funcs = { func1, func2, func3, func4, func5 };
    execute(funcs.begin(), funcs.end());
}


int main()
{
    with_new_and_array();
    with_vector();
}


http://ideone.com/PIfq6i
typedef got it. Thanks.

use containers

Never heard the term "container" before, but guesing with_new_and_array() is the container in your example?
containers are classes in the C++ container library: http://en.cppreference.com/w/cpp/container

as for doing that new without a typedef, the little-known catch is that if the type-id you're new'ing includes any parentheses, it must be parenthesized:

void (**pFuncs)() = new (void(*[5])());
Cubbi wrote:
as for doing that new without a typedef, the little-known catch is that if the type-id you're new'ing includes any parentheses, it must be parenthesized:

I suspect that this is because of default-initialization versus value-initialization syntax:

1
2
int *a = new int; // default initialization, *a is garbage
int *b = new int(); // value initialization, *b is 0 


If there's another reason please clarify.
@Catfish it breaks at grammar level even without taking initializers into account: the standard's example in 5.3.4[expr.new]/4 is

new int(*[10])(); // error
is ill-formed because the binding is
(new int) (*[10])(); // error
Instead, the explicitly parenthesized version of the new operator can be used to create objects of compound
types:
new (int (*[10])());


remember, new is an operator, like sizeof or +. It can be anywhere in an expression.
Last edited on
Topic archived. No new replies allowed.