1

What I'm Essentially trying to do is write a function that can take an array of length n, and make an array of say, length n-2. here is the code i have so 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 i = scale; i < 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);
    }
    return curvs;
}

Ive been calling it in the main function like this:

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

but i get this error:

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

I'm pretty new to C++, what am i doing wrong?????

3
  • Are you compiling using GCC or G++? Commented Feb 12, 2013 at 18:55
  • 1
    You are returning a pointer to a piece of memory on the stack (in automatic storage to use the language from the standard). That memory can be re-used at anytime. Very bad. You want to dynamically allocate memory for this purpose. Consider new[]. There are many duplicate questions on the site, but I'm too lazy to go find one. Commented Feb 12, 2013 at 18:58
  • 2
    Don't consider new[], consider std::vector. Commented Feb 12, 2013 at 19:06

2 Answers 2

1

The curvs array is a local variable within the comp_arr function. The first warning is being thrown because as soon as this function returns, the memory that it was using (which includes the curvs array) will go out of scope. Referencing the returned array in your main will cause undefined behavior; if you'd like to return an array from a function, you'll have to dynamically allocate it via new/malloc.

Sign up to request clarification or add additional context in comments.

2 Comments

i changed it to say: float curvs[arr_dim] = new float[arr_dim]; but now i get an error telling me that it "may have not been initialized"
Now you've reserved space for your array, but the elements have not been initialized. You can do this via a for loop, or may also be able to utilize syntax like curvs[arr_dim] = {0}.
0

You're returning a pointer to a local variable. As soon as the closing brace of your comp_arr function is reached, curvs is out of scope and no longer exists, yet you are returning it's address. The program might even behave correctly if the data is still present in memory, but it could be overwritten at any time.

Comments

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.