Click here to Skip to main content
15,883,873 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Can anyone plz help to convert the following stack declarations to heap declarations
C++
class student
{
   protected:
   int r;
   char sta,m;
   char student_roll[15];
   char name[50];
   char sex[10];
   char status[50];
   float slc,p2,m1,m2,m3,m4,m5,m6,m7,m8;
   float tp,p1,rem,pa;
   char dob[20];
   public:
   char roll_no[15];
};


Please help....its a C++ class...
Posted
Updated 12-May-14 1:06am
v3
Comments
nv3 15-Oct-13 11:32am    
So far you have done neither a stack nor a heap declaration, but just declared a class. If you inside one of your functions write:

student s;

then you have allocated an object of type student on the stack. If however you write

student* pStud = new student;

you have allocated an object of type student on the heap. In the latter case, don't forgat the corresponding delete statement.

Hope that helps.
Ho
Sergey Alexandrovich Kryukov 15-Oct-13 12:25pm    
Would make a fair answer.
—SA
nv3 15-Oct-13 12:48pm    
Thank you, Sergey.
AR547 15-Oct-13 12:26pm    
thanks alot dear... that gave me a clue ;)
nv3 15-Oct-13 12:47pm    
You're welcome.

Why?

This is an acceptable and "safe" class declaration; you do not want to keep unsafe pointers just for the fun of it.

One way to make it better would be to replace the char array with std::string.

Replace the sex string with an enum (and have code to "print" the representation instead of keeping a string (male/female).

Use proper descriptive variable names.

C++
class student
{
  enum SEX {
    Female = 0,
    Male
  };
 
   protected:
   int r;
   char sta,m;
   std::string student_roll;
   std::string name;
   SEX sex;
   std::string status;
   float slc,p2,m1,m2,m3,m4,m5,m6,m7,m8;
   float tp,p1,rem,pa;
   std::string dob;
   public:
   std::string roll_no;
};
 
Share this answer
 
v2
Comments
AR547 15-Oct-13 11:44am    
can't get your point.. except the creation of enum in the class...is this a heap declaration which I can accept, ???
Maximilien 15-Oct-13 11:52am    
No there is no heap in there. all member of the class will be safely allocated when the class itself is allocated.

If you really want to allocate the class on the heap :
student* aStudent = new student; // do not forget to call delete.
or better yet (if you use c++11 )
std::unique_ptr<student> aStudent(new student); // memory will be freed when going out of scope.
AR547 15-Oct-13 11:59am    
ok bro got u...now inshort my question is that kindly tell me how can i declare those variables in heap mode at initial point as they are declared i dont want to declare like this... I think u understand my question :)
Albert Holguin 15-Oct-13 13:54pm    
BTW, don't use the heap if you don't have to... the allocation process is relatively slow and you'll end up slowing your application for no good reason if you could have used the stack.
You correctly answered nv3!
Where memory is allocated depends on how you create an object of your class
For example:
C++
{
student s; // allocated on stack 
}

but
C++
student* pStud = new student; // allocated on heap

But perhaps you're wondering how to describe a class that can always be allocated ONLY on heap... Please use the "Named Constructor Idiom".
See here : http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Named_Constructor

Here new version of your class (if you want allocate a memory for object of class only on heap):
C++
class student
{
public:
    // The create() methods are the "named constructors":
   static student* create() { return new student(); }

protected:
   int r;
   char sta,m;
   char student_roll[15];
   char name[50];
   char sex[10];
   char status[50];
   float slc,p2,m1,m2,m3,m4,m5,m6,m7,m8;
   float tp,p1,rem,pa;
   char dob[20];
   public:
   char roll_no[15];

private:
   // The constructor IS private !!!!
  student();
};

C++
 //Now exist only one way to create a student objects => a student::create() 
int main()
{
   student* p = student::create();
   ...
   delete p;
   ...
} 
 
Share this answer
 
Comments
AR547 15-Oct-13 15:01pm    
okay bro just found the answer for my question... thanks alot
Volynsky Alex 15-Oct-13 15:11pm    
You're welcome AR547 :)
I recommend that you read up about how storage on the stack differs from storage on the heap (in terms of how it is allocated and how it is addressed - does it have a fixed address, or is its likely address relative to some other value ... perhaps a value in a register in the CPU).

You should also study what kind of declarations in your C++ program lead to one or the other kind of storage, and you should learn about the difference between types (classes) that you declare in your program and instances of those types (that are variables in a running program, that actually occupy memory).
 
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