• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

C++ Heap

Joined
Nov 10, 2007
Messages
9
I have an assignment that i just cannot figure out

Consider the following class definition, in a file called "CS240.h":

#define MAX_STUDENTS 70;

class CS240 {
private:
int numStudents;
Student students[MAX_STUDENTS];
public:
CS240();
CS240(int num_students);
};

Now consider the following declaration statement, which creates a single instance of the CS240 class.

CS240 rocks;

a) Write the simplest C++ program you can write, which includes this declaration statement and causes the "rocks" object to be placed on the heap. If the rocks variable, declared this way, would never be placed on the heap, explain why not.


I know that to put an object like this on the heap you need a new constructor such as cs240 rocks = new cs240(); for the default constructor to be made. My professor assures me that there is some roundabout way of doing this by creating a new class but all i can think of is maybe if the new class i create can take the cs240 rocks declaration and manipulate it into cs240 rocks = new cs240(); which would only return a pointer to a cs240 object, but you need to declare an object as new in order to put it in the heap. can anyone suggest any roundabout ways of doing this that would use another class and create the cs240 rocks object on the heap while still having the cs240 rocks declaration?

thanks much for any help!
 
Let's first clear up a couple of errors you're making in your description of the problem. "cs240 rocks = new cs240();" is not a new constructor. That code won't compile, and it isn't a constructor definition. It's a declaration with an initializer.

You don't "declare an object as new" in order to get it on the heap. new is an operator which turns out to be implemented as a function.

On to the problem ... something's wrong. I'm not really sure what the point of this kind of homework is. It seems like the point is to force the student to discover some goofy trick and think through something that isn't quite right in a way that's not normally applicable.

When I read the declaration you've provided, I assume it's a global in a file, or a static in a function. I guess your professor's point is to demonstrate that the same syntactical line of code, given a different context, can end up in a different type of memory allocation for the object.

If we blithely play along, we end up just making that declaration a member of another class:

Code:
class CWrapper
{
public:
   CWrapper() { }
   CWrapper( int num_students ) : rocks(num_students) { }
   CS240 rocks;	// here it is!
};

The line commented "here it is!" shows that declaration, transplanted to be a member of a new, outer class. We can invoke operator new to put that class, and it's CS240 member instance, on the heap:

Code:
CWraper *pWrapper = new CWrapper();

and you've fulfilled the letter of the assignment -- though I can't guess if you've fulfilled its intent.

I'm eager to hear what it specifically is that your prof wanted you to do, and even more interested in hearing what it was he expected you to take away from this assignment.
 
Subscribed because the original post has me a little confused and I want to track this question's progress.
 
ditto to last post.

to the OP: i know it's a little annoying when someone asks you to clear up your speech and use proper terms when describing what you think your code does, but it is essential both for communication purposes and also so you understand what the code is ACTUALLY doing.

i guess what i'm saying is take mikeblas' advice to heart and learn the terminology (and syntax) of the language/system you are programming in/on.
 
Annoying? The point of being a student is to learn, ain't it?
 
just stick in an malloc somewhere, just to annoy c++ people :p
 
off topic, but if you really want to get on c++ purist's bad side, just do a static cast similar to the following..

Code:
(int) y = 9;
 
Such a cast will offend compiler and purist alike; the cast operator doesn't produce an l-value.
 
haha... sure will. sorry, wasn't thinking straight.

better example (because it doesn't violate c++ rules):

Code:
double y = 7.1;
y = (int) 9.6;
 
Im hitting the office hours tommorow or monday, will get back to you guys. He wont reveal the answer but keeps leading me to discovering new information. I'm actually enjoying this hunt for the answer.
 
He didnt give me any answers but from the research it is possible through the implementation of more and more code etc (read above). This is an extremely round about way to do it and should just be declared with new.


thanks for your help though guys!
 
Do you have your professor's email address? I'd like to write him and pursue an answer directly.
 
Generally I've found many academics are fairly out of touch with pragmatic programming; at least where I went to school, I found I enjoyed the content much better if they focused on the concepts and eschewed trying to play with language-dependent features or quirks, or trying to write "clever" solutions to problems that in a more profession setting would be frowned upon.

While teaching C/C++ is a good way to teach students the difference between heap/stack allocation, IMHO the lesson begins to lose its relevance when the student is faced with "brain teaser" or "thought puzzle" type questions that end up being far beyond their technical grasp at that particular point in their education, especially when the problem is so poorly defined. It's unfortunate that you ran up against one of those "puzzles" that in the end doesn't provide enough conceptual ROI versus the confusion it can bring up at the same time.
 
Do you have your professor's email address? I'd like to write him and pursue an answer directly.

If this bears fruit, please fill us in on details. Every time I see an update to this thread I hope it's an answer.
 
Sure will. The ACM has a curriculum advisory committee, and I've been thinking about getting involved in it. I see lots of questions that students post, and see bunches of textbooks ... but I don't get much from the professor's point of view.

As generelz suggests, this question doesn't seem to be too useful -- and was apparently completely lost on cybershock. I've posted code that solves the question as stated here, as far as I can tell. But what does that teach? What was the professor's intention in asking it? And so on....
 
Back
Top