Thursday, 15 March 2012

Array Creation in C++ -



Array Creation in C++ -

what i'm trying write function can take array of length n, , create array of say, length n-2. here code have far:

float* comp_arr(float vals[], int arr_size, float scale, float dim){ int arr_dim = (int)(arr_size+1-2*scale); float curvs[arr_dim]; for(int = scale; < sizeof(vals)-scale+1; i++){ float cur = comp_cur((i-scale)*dim, vals[i-1], i*dim, vals[i], (i+scale)*dim, vals[i+1]); int new_index = (int)(i-scale); curvs[new_index] = cur; printf("%f\n",cur); } homecoming curvs; }

ive been calling in main function this:

main(){ float vals [] = {2,3,6,1,7}; float *curvs = comp_arr(vals,5,1.0,1.0); }

but error:

comp.cpp: in function ‘float* comp_arr(float*, int, float, float)’: comp.cpp:35:8: warning: address of local variable ‘curvs’ returned [enabled default] /tmp/ccrdjjyq.o: in function `comp_arr(float*, int, float, float)': comp.cpp:(.text+0x590): undefined reference `__cxa_end_cleanup' /tmp/ccrdjjyq.o:(.arm.extab+0xc): undefined reference `__gxx_personality_v0' collect2: ld returned 1 exit status

i'm pretty new c++, doing wrong?????

the curvs array local variable within comp_arr function. first warning beingness thrown because function returns, memory using (which includes curvs array) go out of scope. referencing returned array in main cause undefined behavior; if you'd homecoming array function, you'll have dynamically allocate via new/malloc.

c++ arrays

No comments:

Post a Comment