Bygonaut
Bygonaut

Reputation: 3

Where are the pointers supposed to be?

Unfamiliar with C++ programming, Trying to conceptualize a problem here:

In this exercise we’ll confine ourselves to one numerical type, float, so we’ll need an array of this type; call it fmemory. However, pointer values (addresses) are also stored in memory, so we’ll need another array to store them. Since we’re using array indexes to model addresses, and indexes for all but the largest arrays can be stored in type int, we’ll create an array of this type (call it pmemory) to hold these “pointers.”

An index to fmemory (call it fmem_top) points to the next available place where a float value can be stored. There’s a similar index to pmemory (call it pmem_top). Don’t worry about running out of “memory.” We’ll assume these arrays are big enough so that each time we store something we can simply insert it at the next index number in the array. Other than this, we won’t worry about memory management.

Create a class called Float. We’ll use it to model numbers of type float that are stored in fmemory instead of real memory. The only instance data in Float is its own “address”; that is, the index where its float value is stored in fmemory. Call this instance variable addr. Class Float also needs two member functions. The first is a one-argument constructor to initialize the Float with a float value. This constructor stores the float value in the element of fmemory pointed to by fmem_top, and stores the value of fmem_top in addr. This is similar to how the compiler and linker arrange to store an ordinary variable in real memory. The second member function is the overloaded & operator. It simply returns the pointer (really the index, type int) value in addr.

So, what I have deducted was that I need to create something like this

#include <iostream>
using namespace std;
class Float
{
private:
int addr;
float fmem_top,pmem_top;
public:
Float(float* fmem_top){};
Float(int* addr){}
};

int main()
{
//this is where I become confused
}

Would I use something like this in the main method? Float fmem; Float pmem;

Upvotes: 0

Views: 115

Answers (2)

Peter - Reinstate Monica
Peter - Reinstate Monica

Reputation: 16017

Try this. I think the exercise was to abstract for beginners. (I didn't compile this though, it's more for you to understand the concept.)

const int NUM_FLOATS_MAX = 1000;

class Float
{
    static float floatArr[];
    static int fmem_top;
    int addr;
public:
    int operator&() { return addr; }
    Float(float f) { floatArr[fmem_top] = f; addr = fmem_top; fmem_top++; }
}
// static members need to be defined outside the class declaration
float Float::floatArr[NUM_FLOATS_MAX];
int Float::fmem_top = 0;

// should print 0 and 1, the indices. (How we get the float value
// we don't know -- overload the operator*()?
int main() 
{ 
    Float f1 = 1, f2 = 2;
    cout << &f1 << endl << &f2 << endl; 
}

Upvotes: 0

Tony Delroy
Tony Delroy

Reputation: 106086

In this exercise we’ll confine ourselves to one numerical type, float, so we’ll need an array of this type; call it fmemory. However, pointer values (addresses) are also stored in memory, so we’ll need another array to store them. Since we’re using array indexes to model addresses, and indexes for all but the largest arrays can be stored in type int, we’ll create an array of this type (call it pmemory) to hold these “pointers.”

float fmemory[N]; // "we'll need an array of [float]"
int pmemory[N];   // "we'll create an array... pmemory"

An index to fmemory (call it fmem_top) points to the next available place where a float value can be stored. There’s a similar index to pmemory (call it pmem_top). Don’t worry about running out of “memory.” We’ll assume these arrays are big enough so that each time we store something we can simply insert it at the next index number in the array. Other than this, we won’t worry about memory management.

int fmem_top = 0; // "next available place..." fmemory[fmem_top]
int pmem_top = 0; // "similar index to pmemory"

Create a class called Float. We’ll use it to model numbers of type float that are stored in fmemory instead of real memory. The only instance data in Float is its own “address”; that is, the index where its float value is stored in fmemory. Call this instance variable addr.

class Float
{
    int addr;

class Float also needs two member functions. The first is a one-argument constructor to initialize the Float with a float value. This constructor stores the float value in the element of fmemory pointed to by fmem_top, and stores the value of fmem_top in addr.

Float(float f)
{
    fmemory[fmem_top] = f;
    addr = fmem_top;
    ++fmem_top;
}

This is similar to how the compiler and linker arrange to store an ordinary variable in real memory.

Yeah sure.

The second member function is the overloaded & operator. It simply returns the pointer (really the index, type int) value in addr.

int operator&() { return addr; }

Discussion

There's no indication of the intended use of pmemory, so it's unclear what ought to be done with it. It doesn't make a lot of sense really.

Overall, the interface for Float doesn't provide any clean abstraction for the code using it: the stored values' indices can be retrieved with &, but the caller still needs to know about fmemory to find the actual float value.

I hope the course improves....

Upvotes: 1

Related Questions