Click here to Skip to main content
15,887,440 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a function that return a pointer to an array. I call this in main() and retrives the pointer. The when i try to loop over to access the values, only one value can be accessed without the rest being turned into garbage. I can only access one index, if i try more than 2, one of them turns to garbage.

C++
//This does not work, gives garbage
int *p = generateArray(10);

for(int i =0;i <10;i++){
cout << p[i];
}

cout <<p[i]; // works for any index, but if copy paste this at the next line, it gives garbage.


Here is the complete code.

C++
#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;
}
Posted
Updated 25-May-15 1:30am
v4
Comments
Frankie-C 25-May-15 7:21am    
What you told us is not sufficient.
Surely there is an heap corruption, this can be due to wrong type interpretation, or wrong coding somewhere.
Show us generateArray() code.

In fillArray() you have created a local array (automatic variable) on the stack heap, pretending to give it back as result. This is wrong when the function returns the stack is reused by next functions and filled with their automatic data that for you are garbage!
If you need an array to give back create it with the new operator, or allocate it from malloc.
Other observation: take care of compiler warnings, you should have had some, because not all paths of your function return a value!
This is the correct coding for fillArray(). And don't forget to free memory (using Delete) when finished with the array.
C++
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= new int[size];    //allocate memory for the array
 
    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){
            return pholdArray;
        }
    }
    return pholdArray;
}
 
Share this answer
 
Comments
_Asif_ 25-May-15 8:09am    
+5
Frankie-C 25-May-15 8:10am    
Thanks
C++ code can get great benefits from standard library usage. If you just want to fill the array with random numbers without repetition then you could write:
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int N = 20;
const int LB = 0;
const int UB = 19;
int main()
{
  vector <int> v;

  for (int n=LB; n<=UB; ++n)
    v.push_back(n);

  random_shuffle(v.begin(), v.end());


  for (size_t n=0; n<v.size(); ++n)
    cout << v[n] << endl;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900