1

My problem is that my integer array's values change when it is passed to the function calculate. The values are correct for indexes 0, and 2->5.

For some reason, indexes 1 and 6+ are not the correct values.

Below is my code.

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

int* generate_rand (int length, int MAX_ARRAY);
void calculate (int *array_ptr, int length, int *mean, int *sd);

main() {
    srand(time(NULL));
    int a;
    printf("\nArray length?: ");
    scanf("%d", &a);
    int* array_ptr2;
    array_ptr2 = generate_rand(a, 100);
    //int mean, sd;
    int* *mean;
    int* *sd;
    int i = 0;
    for (i = 0; i < 10; i++) {
        printf("Array2: %d\n", *(array_ptr2 + i));
    }
    calculate(array_ptr2, a, *mean, *sd);
    //printf("Mean: %d\n", (int)*mean);
}

int* generate_rand (int length, int MAX_ARRAY) {
    int arr[length];
    int i;
    for (i = 0; i < 10; i++) {
        int r = rand()%MAX_ARRAY;
        arr[i] = r;
        printf("Rand: %d\n", arr[i]);
    }
    int *arrPtr;
    arrPtr = &arr[0];
    return arrPtr;
}

void calculate (int *array_ptr, int length, int *mean, int *sd) {
    int sum;
    int i;
    for (i = 0; i < length; i++) {
        printf("Array: %d\n", *(array_ptr + i));
        sum += *(array_ptr + i);
        //array_ptr++;
        printf("Sum: %d, i:%d\n", sum, i);
    }
    //*mean = sum / length;
}

Do you know what I'm doing wrong?

10
  • 3
    generate_rand is returning a pointer to the contents of a local variable, which is useless.. Commented Jan 25, 2013 at 1:35
  • So you're saying that I should declare array_ptr2 just below the include statements? I just tried that and I am still getting the same result. Commented Jan 25, 2013 at 1:37
  • You must allocate your own array (using malloc or calloc or something), the one in generate_rand "disappears" at the end of the function. It seems you circumvented the warning about returning a local variable by putting it into a temporary pointer first. Commented Jan 25, 2013 at 1:37
  • More accurately, returning a pointer to a local variable results in undefined behavior. Commented Jan 25, 2013 at 1:37
  • @user1262552 Oli is saying that you need to find another solution to your problem. What is generate_rand() supposed to do? Is there a reason you are returning a int* rather than just a plain int? Commented Jan 25, 2013 at 1:38

3 Answers 3

3

The problem is that you are returning the address of a local variable of a function. Local variables of functions cease to exist after a function has exited, so the returned pointer suddenly becomes dangling (as in, not pointing to anything useful).

You need to allocate memory for your array with malloc, because the memory returned from malloc will exist until it is freed with free (which you can call anytime afterwards).

int* generate_rand (int length, int MAX_ARRAY) {
    int *arr = malloc(sizeof(int) * length);

    int i;

    for (i = 0; i < 10; i++) {
        int r = rand()%MAX_ARRAY;
        arr[i] = r;
        printf("Rand: %d\n", arr[i]);
    }

    return arr;
}

When you no longer need the array returned from this function, pass it to free to release the memory, e.g.:

int *arr = generate_rand(10, 100);
// do something with arr here...
free(arr);
Sign up to request clarification or add additional context in comments.

Comments

3

generate_rand() is returning a pointer to arr[0] where arr is a local variable. The local variable goes out of scope as soon as the function, generate_rand(), returns. Once a variable is out of scope, there is no guarantee about its value, in fact, accessing an out of scope of variable is undefined.

One possible solution: generate_rand() can allocate the array arr on the heap, i.e. allocate memory using malloc(). Needless to say, the memory must be free()-ed as soon as it is no longer necessary.

Comments

1

As you state in the comments, generate_rand() needs to return a pointer to an array that is filled with random values. At the moment, you are returning a pointer to the first element of a local array. As other's have stated, the memory of this array is only guaranteed to be available to your program inside of the generate_rand() function. In order to create an array which you can use anywhere in your program, you need to allocate the memory with malloc(). I suggest you research memory management further. You need to learn about both the malloc() and free() functions.

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.