Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
3  
generate_rand is returning a pointer to the contents of a local variable, which is useless.. –  Oli Charlesworth Jan 25 '13 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. –  twbbas Jan 25 '13 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. –  dreamlax Jan 25 '13 at 1:37
    
More accurately, returning a pointer to a local variable results in undefined behavior. –  Code-Apprentice Jan 25 '13 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? –  Code-Apprentice Jan 25 '13 at 1:38
show 5 more comments

3 Answers

up vote 3 down vote accepted

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);
share|improve this answer
    
Thanks! Exactly what I need and it worked! –  twbbas Jan 25 '13 at 1:46
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.