2

The output shows the values of k as 0.000 but it should contain actual values that are returned from funcTest().

#include <stdio.h>
#include <stdlib.h>

float *funcTest(int *a, int size)
{
    float p[size];
    int i;
    for(i=0; i< size; i++){
        p[i] = *a;
        p[i]=p[i]/2;
        a++;
    }
    for(i =0; i<size; i++){
        printf("%f\n",p[i]);
    }
    return (float *)p;
}

int main()
{
    int a[4] = {1,5,3,7};
    int size = 4;
    int i,j;
    float *k;
    k = funcTest(&a[0],size);
    for(i =0; i<size; i++){
        printf("%f\n",*k);
        k++;
    }
    return 0;
}

OUTPUT :

0.5
2.5
1.5
3.5
1.5
0.000
0.000
0.000
1
  • 1
    p will be destroyed once you leave the method "functest". Therefore the pointer you receive is not valid at all. Commented Mar 7, 2016 at 12:41

5 Answers 5

3

You return a local variable. so, at end of function, local variables are cleaned and do not exist any more. You should use malloc

float *funcTest(int *a, int size)
{
    float *p = malloc(sizeof(float) * size);

And don't forget to free memory when you don't use it anymore with free. If you forget, you will have a memory leak

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

Comments

2

In your code, float p[size]; is local to the function funcTest(), so you cannot return the address of it from the function and expect it to be valid in the caller. Once the funcTest() function ends, p will cease to exist. Then, any attempt to make use of the return value will cause undefined behavior, because of invalid memory access.

Instead, you can make p a pointer, allocate memory dynamically using malloc() or family and then, return that pointer to the caller. Dynamically allocated memory will have a lifetime equal to the entire execution of the program (unless deallocated manually), so, even after returning from the function, in the caller, the returned pointer will be valid.

Comments

2

As the people before me pointed out, you're trying to return a stack allocated variable (a local variable in a function), meaning the memory you point to isn't valid once the function exits (it clears out on some implementations, but really it's behavior isn't defined).

The solution is to either use malloc, as pointed out before, OR pass a pointer to the function (thus avoiding malloc and free), keeping the allocation on the stack of the calling function.

i.e. (this might not work, it isn't tested, just here for demonstration of the idea):

#include <stdio.h>
#include <stdlib.h>

int funcTest(float *p, int *a, int size)
{
    int i;
    for(i=0; i< size; i++){
        p[i] = *a;
        p[i]=p[i]/2;
        a++;
    }
    for(i =0; i<size; i++){
        printf("%f\n",p[i]);
    }
    // we can use the return value for errors.
    return 0;
}

int main()
{
    int a[4] = {1,5,3,7};
    float p[4] = {0};
    int size = 4;
    int i;
    funcTest(p, a, size);
    for(i =0; i<size; i++){
        printf("%f\n",p[i]);
    }
    return 0;
}

Notice also, you don't need to use &a[0] - arrays are pointers at heart, so a is the memory address of a[0] while a + 1 is the memory address of a[1].

Good Luck!

4 Comments

Note: There is no need to return the array. Just print p directly from main.
@CoolGuy - Yap, you're right. I didn't even look at that part of the code ... I'll fix this up :-)
Also, no need for funcTest to return something, could be void funcTest(float *p, int *a, int size)
@Garf365 , yap, that's true here, but it's good practice to use error check, so I left a return 0 to nudge in the right direction... ;-)
2

p is an automatic local variable and will no longer be exist once function return. Returning a pointer to it will invoke undefined behavior.

Allocate p dynamically

float *p = malloc(size*sizeof(float)); 

Free the allocated memory once you are done with it

float *k;
k = funcTest(&a[0],size);
if(k == NULL)             // Check for null pointer
    exit(0);
float *temp = k;
for(i =0; i<size; i++){
    printf("%f\n",*k);
    k++;
}
free(temp);              // Free allocated memory
return 0;

1 Comment

@SouvikKundu don't forget to free memory when you don't need it anymore. If you forget, you will have a memory leak
0
#include <stdio.h>
#include <stdlib.h>

void funcTest(int *a, int size, float *output)
{
    output = (float *) malloc(sizeof(float) * size);
    int i;
    for(i=0; i< size; i++){
        output[i] = *a;
        output[i]=output[i]/2;
        a++;
    }
    for(i =0; i<size; i++){
        printf("%f\n",output[i]);
    }
}

int main()
{
    int a[4] = {1,5,3,7};
    int size = 4;
    int i,j;
    float *k;
    funcTest(&a[0],size, k);
    for(i =0; i<size; i++){
        printf("%f\n",k);
        k++;
    }
    return 0;
}

This might be worked.

  1. Remove your local definition float p[size]; in funcTest
  2. Add one more parameter float *output in your funcTest for output pointer.
  3. Allocate memory, malloc the sizeof(float) * size for your output pointer (float array size) in funcTest.
  4. Replace variable p into the parameter output.
  5. No need to return, change return type from float * to void.
  6. Fix into printf("%f\n",k); k is a address. *k is a pointer.

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.