#include #include using namespace std; /* malloc works in C++ and C both int* make_power_array(int base, int length){ int *result = (int*)malloc(length * sizeof(int)); for(int i = 0; i < length; i++) result[i] = pow(base, i); return result; } */ int* make_power_array(int base, int length){ int *result = new int[length]; // C++ only, calls constructors for(int i = 0; i < length; i++) result[i] = pow(base, i); return result; } int main(){ int *pa = make_power_array(5, 10); for(int i = 0; i < 10; i++) cout << pa[i] << " "; cout << "\n"; /* If we used malloc, we use free: free(pa); */ /* if we used new, we'll use delete */ delete[] pa; return 0; }