Can't seem to pinpoint my problem? Possible pointer issues in use of my class

Hi,

I'm working on a textbook question, so far I got the program to at least compile
but it crashes immediately upon call to question 1

Header File
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
#ifndef PROB1RANDOM_H
#define PROB1RANDOM_H
#include <ctime>
#include <cstdlib>
using namespace std;


class Prob1Random
{
	private:
		char *set;      //The set of numbers to draw random numbers from
		char  nset;     //The number of variables in the sequence
		int  *freq;     //Frequency of all the random numbers returned
		int   numRand;  //The total number of times the random number function is called
	public:
		Prob1Random(const char num,const char *numSequence);     //Constructor
		{
			nset = num;
			set = numSequence;
		}
		~Prob1Random(void);                       //Destructor

		//Returns a random number from the set
		char randFromSet(void);                   
		{
			//seed generator
			srand((unsigned)time(0)); 
			//get a randnum between 0 and 4
			int randNum = (rand()%4)+0;
			
			// add 1 to frequency element of the
			// element chosen by the random number
			if(randNum = 0)
				freq[0]++;
			else if(randNum = 1)
				freq[1]++;
			else if(randNum = 2)
				freq[2]++;
			else
				freq[3]++;

			//add 1 to numRand for everytime
			// this function is called
			numRand++;

			return set[randNum];
		}

		//Returns the frequency histogram
		int *getFreq(void) const;                 
		{
			return *freq;
		}

		//Returns the set used
		char *getSet(void) const;
		{
			return *set;
		}

		//Gets the number of times randFromSet has
	    //been called
		int getNumRand(void) const;    
		{
			return numRand;
		}
};

#endif 


Main Function
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include "Prob1Random.h"
using namespace std;

void Menu();
int getN();
void def(int);
void problem1();
void problem2();
void problem3();
void problem4();
void problem5();
void problem6();
void problem7();


    int main(int argv,char *argc[])
    {
	int inN;
        do{
         Menu();
         inN=getN();
         switch(inN){
          case 1:    problem1();break;
          case 2:    problem2();break;
          case 3:    problem3();break;
          case 4:    problem4();break;
          case 5:    problem5();break;
          case 6:    problem6();break;
          case 7:    problem7();break;
          default:   def(inN);}
        }while(inN<8);
        return 1;
    }
    void Menu()
    {
           cout<<"Type 1 for problem 1"<<endl;
           cout<<"Type 2 for problem 2"<<endl;
           cout<<"Type 3 for problem 3"<<endl;
           cout<<"Type 4 for problem 4"<<endl;
           cout<<"Type 5 for problem 5"<<endl;
           cout<<"Type 6 for problem 6"<<endl;
           cout<<"Type 7 for problem 7"<<endl;
           cout<<"Type 8 to exit \n"<<endl;
    }
    int getN()
    {
           int inN;
		   cin>>inN;
           return inN;
    }
    void problem1()
    {
///////////////////////////////////////////////////////////////////////////////////////////
           cout<<"In problem # 1"<<endl<<endl;

	char n=5;
	char rndseq[]={16,34,57,79,121};
	int ntimes=100000;
	Prob1Random a(n,rndseq);
	for(int i=1;i<=ntimes;i++)
	{
		a.randFromSet();
	}
	int *x=a.getFreq();
	char *y=a.getSet();
	for(int i=0;i<n;i++)
	{
		cout<<int(y[i])<<" occured "<<x[i]<<" times"<<endl;
	}
	cout<<"The total number of random numbers is "<<a.getNumRand()<<endl;



/////////////////////////////////////////////////////////////////////////////////////////////
    }
    void problem2()
    {
           cout<<"In problem # 2"<<endl<<endl;
    }
    void problem3()
    {
           cout<<"In problem # 3"<<endl<<endl;
    }
    void problem4()
    {
           cout<<"In problem # 4"<<endl<<endl;
    }
    void problem5()
    {
           cout<<"In problem # 5"<<endl<<endl;
    }
    void problem6()
    {
           cout<<"In problem # 6"<<endl<<endl;
    }
    void problem7()
    {
	   cout<<"In problem # 7"<<endl<<endl;
    }
    void def(int inN)
    {
           cout<<"You typed "<<inN<<" to exit the program"<<endl;
    }


My weakness lies within the use of pointers, so I think it may have something to do within that area, some help would be great, I just can't seem to wrap my head around it.

Much appreciation to those who help
Your 'freq' in your Prob1Random class is never initialized. IE: it's a bad pointer. So when you try to do this:

1
2
3
4
5
6
7
8
			if(randNum = 0)
				freq[0]++;
			else if(randNum = 1)
				freq[1]++;
			else if(randNum = 2)
				freq[2]++;
			else
				freq[3]++;


You're dereferencing a bad pointer. This causes heap corruption, which is the nastiest of nasty bugs. If you're lucky, the program will crash. If you're unlucky, the program will just behave strangely and it'll be very hard to figure out why. If you're really unlucky, the program will work just fine until you make some totally unrelated change a few months down the line.

(on a side note, you're using the assignment operator = in the above, not the comparison operator == which I assume was intended)

(on another note, you never initialize 'numRand' either, so it'll probably be some garbage value and won't reflect the value you expect it to)

If you want freq to be an array of 4 (is that what you're going for?), then make it an array of 4, not a pointer:

1
2
//int* freq;  // makes a pointer
int  freq[4];  // makes an array 


There are some other oddities here. The whole if/else chain I pasted above can be simplified by using randNum as an index, rather than doing a bunch of comparison checks:

 
freq[ randNum ]++;  // much simpler 


You also have a few errors that the compiler should be giving you:

1
2
3
4
5
6
7
8
9
10
11
12
		int *getFreq(void) const;                 // if you're returning a pointer
		{
			//return *freq;  // then don't dereference your pointer.  This returns an int, not an int*
			return freq;  // this returns the pointer
		}

		//Returns the set used
		char *getSet(void) const;
		{
			//return *set;
			return set;  // ditto
		}


1
2
3
			//this actually returns a number between 0 and 3.  You'll never get 4.
			//  is this intentional?
			int randNum = (rand()%4)+0;

1
2
3
4
5
6
7
		Prob1Random(const char num,const char *numSequence);     //Constructor
		{
			nset = num;
			set = numSequence;  // numSequence is a const char*, but set is a nonconst char*
			  // this should be giving you a compiler error.  You should make 'set' a const char* as well.
			  // this also means you'll have to change getSet() to return a const char*.
		}


You also don't need the (void) for functions that don't have parameters. That's a weird C thing. In C++ it's pointless.
Last edited on
Good info Disch, thanks a lot.
Topic archived. No new replies allowed.