2

This is an extremely stupid question, but here goes...

I've got a class with this code:

class ArraySection {
public:
    unsigned char *array;
    int start, stop, k;   
};

All I want to do is create an instance of this class in my main function. I'm trying to do it with this (where length & n are predefined):

ArraySection *ASarrayPrimes = nullptr;
ASarrayPrimes->array[length];
ASarrayPrimes->start = stop;
ASarrayPrimes->stop += length;
ASarrayPrimes->k = 0;

But I'm guessing this isn't the proper way to do it...? We never really covered this in class. (And if you're wondering, I'm supposed to use threads to parallelize the sieve algorithm to find prime numbers.) What I'm trying to do is take the massive array, break it down into smaller arrays (created with this ArraySection class given by the professor), then have threads work on finding primes in those separate, smaller threads.

edit: I can't change the class, guys. I can only work with what the professor gave.

2
  • What do you mean by predefined in where length & n are predefined. Also note that an array is not a pointer, and a pointer is not an array. Commented Sep 15, 2012 at 2:28
  • 1
    In your class you're learning about threads before you learn about arrays, vectors, classes etc. Wow. Commented Sep 15, 2012 at 6:03

4 Answers 4

2

You're never allocating any memory for you object. Either allocate the memory with new, or keep it on the stack.

You can allocate you array inside the class with either option.

ArraySection *ASarrayPrimes = new ArraySection; // On the heap
ASarrayPrimes->array = new char[SIZE]; // New character array on the heap
delete [] ASarrayPrimes->array;
delete ASarrayPrimes; // Make sure you free the memory

ArraySection ASarrayPrimes; // On the stack
ASarrayPrimes.array = new char[SIZE]; // New character array on the heap

Just make sure you free that array memory too! A safer method would be to use a std::vector<char> or indeed. a std::string instead of the member char* array as these will handle allocation/deallocation for you.

Sign up to request clarification or add additional context in comments.

Comments

0

All you did was put a pointer in your class. To put an array, you could to this:

class ArraySection {
public:
    unsigned char array[100];
    int start, stop, k;
};

Comments

0

Consider:

class ArraySection {
public:
    string str; // or vector<type> for the type of the number
    int start;
    int stop;
    int k;
};

This avoids all the array business and errors.

If you need to, you can access str.c_str() to read-only access the data in the string C-style, but you never have to worry about actual length.

Comments

0

Take a look at this tutorial to learn C++.

But to answer your questions, there are 2 ways to allocate memory of your object:

1- Allocate in on the stack:

ArraySection arrayInstance;

2- Or on the heap:

ArraySection  *arrayInstance = new ArraySection();

Then you need to allocate memory to the unsigned char *array variable;

1-

arrayInstance.array = new unsigned char[1234];

2-

arrayInstance->array = new unsigned char[1234];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.