Pointer array not outputting values correctly

I have a function that creates and array and fills it with random numbers and return a pointer to that array. Now in my main function, i want to access those values, i can access each index individually, but if i put it in a for loop to cout all the elements, garabge comes out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
#include <ctime>
#include <iterator>
#include <cstdlib>

 
using namespace std;
 
int randomNum(int lowerBound, int upperBound){
    int random;
    random = lowerBound + rand() % (upperBound - lowerBound);
    return random;
}
 

 
int checkDuplicate(int size,int value, int *pholdArray){
    int *arrholder = pholdArray;
    for(int g =0;g<size;g++){
        if(arrholder[g] == value){
            return 3;
        }
    }
    return 4;
}
 

/*
int* generateArray(int size){
    int *parray = new int[size];
    return parray;
}
*/
 

int* fillArray(int size, int lowerBound, int upperBound){
    int counter_repeat;
    int loopcount = 0;
    int repeat =0;
    int random;
    int receive;
    int setrand;
    int pholdArray[size];
 
    if(checkDuplicate(size,random,pholdArray) == 3);
        counter_repeat++;
        for(int i =0;i<size;i++){
            random = randomNum(lowerBound,upperBound);
            if(checkDuplicate(size,random,pholdArray) == 3){
                counter_repeat++;
            while(checkDuplicate(size,random,pholdArray) == 3){
                random = randomNum(lowerBound,upperBound);
                counter_repeat++;
            }
        }
            pholdArray[i] = random;
 
        if(i == size-1){
            int *finalArray = pholdArray;
        return finalArray;
        }
    }
        //int *finalArray = pholdArray;
        //return finalArray;
}
 

 
int main(){
 
    srand (time(0));
   // int *p = generateArray(10);
    int *n = fillArray(10,0,10);
    cout << n[1]; // works
    cout << n[2]; // garbage

    for(int d = 0;d<10;d++){
    cout << n[6] << "\n"; // garbage 
    }
return 0;
}
Last edited on
Of course it works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int * generateArray(int size)
{

    int* array = new int[size];

    for(int i = 0; i < size; ++i) {
        array[i] = rand() % 100;
    }

    return array;
}


int main()
{

    srand(time(NULL));

    int *p = generateArray(10);


    cout << p[0] << endl;
    cout << p[1] << endl;

    for(int h =0; h<10; h++) {
        cout << p[h] << "\n";
    }

    return 0;
}


You had probably declared your array in your function as int[] and not as int*.

Best.
Filling in your blanks with predictable code, it appears to work for me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

int * generateArray(int size){
	int *array = new int[size];
	
	for(int i = 0; i < size; i++)
		array[i] = i*i;

	return array;
}


int main(){

	int *p = generateArray(10);


	std::cout << p[0];
	std::cout << p[1];

	for(int h =0;h<10;h++){
		std::cout << p[h] << "\n"; 
	}
	
	delete[] p;

	return 0;
}


out:
010
1
4
9
16
25
36
49
64
81
Last edited on
Updated with the full code.
Some warnings from my compiler before I get to your error:
warning: if statement has empty body on line 46.
warning: variable 'counter_repeat' is uninitialized when used on line 47.
warning: variable 'random' is uninitialized when used on line 46.

But you error is int pholdArray[size];. This creates the array, but deletes it when the function is exited. You need to use dynamic memory int *pholdArray = new int[size]; and remember to delete[] it after you finish using it.

http://www.cplusplus.com/doc/tutorial/dynamic/
Last edited on
The warnings that BlantantlyX pointed out are significant.

Line 46: The semicolon terminates the if statement, effectively cause the if statement to be ignored and the body of the if to be executed unconditionally. Remove the ; on line 46.

Line 47: Since couter_repeat is uninitialized, you're going to be incrementing a garbage value. BTW, what's the point of couner_repeat? You increment, but never do anything with it.

Line 46: You check_duplicate function is not going to work correctly, because the value of random has never been set.
Topic archived. No new replies allowed.