Dynamic Memory Allocation!!!

Pages: 12
Hello Guys!!!
How to dynamically allocate memory in constructors or setter functions for data members of class? Suppose if we have two data members(ID,Name) and two setter functions (void setID(),void setName()) then how to allocate memory dynamically for these two data members in these setter functions or constructors....Please if anybody knows reply here in detail and help others....Thanks!!!
Last edited on
You don't.
then where we can allocate memory for these variables....???
Last edited on
The obvious place to do that is in the constructor. It would be far better not to allocate the memory dynamically, though. Is there a reason why something like this would not work?

1
2
3
4
class someKindOfObject{
int ID;
string Name;
}


If ID and Name are just simple variables, there is no reason to allocate memory dynamically.
Last edited on
No, these are not just simple variables....there is also one DeleteMember() function that is used to deallocate memory for these variables that's why i am asking you guys. DeleteMember() function only works if we allocate memory dynamically for these two variables and i am totally confused where to allocate memory for these variables (In setters or in constructors).....Confused...... :(
I already told you.

The obvious place to do that is in the constructor.

The only reason not to do it in the constructor is if there is no way as construction time to know how much memory you need.
OK.....Then please tell me is this is the correct method of allocating memory for variables in constructors!!!
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
class User
{
      protected:
      int ID;
      char *Name;
      
      public:
      User(){
      ID=0;
      Name=NULL;
      }
      User(int id,char *name){
      int *ID=new int[10];
      char *Name=new char[25];
      }
      ~User(){
      }
      void setID()
      {
           cout<<"Enter ID: ";
           cin>>ID;
           }
      void setName()
      {
           cout<<"Enter Name: ";
           cin>>Name;
           }

If there is any problem in allocating memory like this for two variables....Please tell me.....Thanks!!!
Those variables are not at all complex. They are very simple and you've chosen to make them complicated. C++ provides many options so that you don't have to do this sort of thing.

If you are forced to do it this way, you have my sympathy.

That said, you've got it wrong. Is ID meant to be one number, or ten numbers?
closed account (S6k9GNh0)
Whether to dynamically allocate memory or not should be left up to the user of the class (or given a facility to choose how the memory is handle i.e. stl containers).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Bob
{
    int dude;
    int wheres;
    int my;
    int car;
};

int main()
{
    Bob* lol = new Bob; //Those variables are now dynamically allocated.
    Bob test; //However, stack is faster and this is more effecient to allocate on stack.
    Bob* wtf = new Bob[150]; //But in an array, Bob can start getting fat and is too big for stack. Stack is limited. 
    std::vector<Bob> hmmm; //Vectors do not allocate on stack (? true ?)
}

Setter functions don't reallocate. They simply change the value of you data. Constructors handle the setting of the creation of variables, not how they're allocated.
Last edited on

No dynamic memory needed.

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
class User
{
      protected:
      int ID;
      string Name;
      
      public:
      User(){
      ID=0;
      Name="";
      }
      
      ~User(){
      }
      void setID()
      {
           cout<<"Enter ID: ";
           cin>>ID;
      }
      void setName()
      {
           cout<<"Enter Name: ";
           cin>>Name;
       }
};
Last edited on
ID is actually a User ID in this User class and Name is the name of user......ID is meant to be ten numbers.....????
.ID is meant to be ten numbers.....????


That's what I'm asking,. You made ID an array of ten int values, and then you tried to treat it as if it were a single int value. If it's just one int value, why did you write this:

int *ID=new int[10];
That's an array of ten int values.
Last edited on
but i already told you i have DeleteMember() function also where i want to delete/deallocate memory for these variables.... that's why dynamic memory allocation is necessary.....
closed account (S6k9GNh0)
The idea of a DeleteMember() function is bad both in practice and in concept. I hope this isn't a homework assignment... I had to deal with stuff like that recently as it was being submitted to me as "valid" when it was nowhere close.
Last edited on
Sorry ten int values like 1234567890.....That's what i want. that's why i write this
int *ID=new int[10];
That's one int value. Just one. Like this:

int x = 1234567890;

Just one number.

I think you need to go back to basics and learn what an int is. It's short for integer, which should help you to understand it.
but i already told you i have DeleteMember() function also where i want to delete/deallocate memory for these variables.... that's why dynamic memory allocation is necessary.....


Are you being forced to do that? Because it is silly and unnecessary. It's extra work that is not needed. There is no need to allocate dynamically for this. No need. If you do, you are making it harder for yourself.
I want to implement my DeleteMember() function in derived class (Members) where i want to delete record of Users(ID,Name)......The DeleteMember() function that i want to implement in Members class is like this:
1
2
3
4
5
void DeleteMember()
{
delete[] ID;
delete[] Name;
}


That's it...
i want to delete record of Users(ID,Name)


You do know that the delete keyword won't actually delete the member object? The pointers will still exist and they will still point to the same place. The member object will still exist. The data will not be magically removed.

Here is the constructor you need for what you want to do; I point out again that what you want to do is silly and makes no sense at all and is making this much harder for yourself than it needs to be.


1
2
3
4
5
6
7
8
9
10
      User(){
      ID = new int;
      Name = new char[25];
      }

void DeleteMember()
{
delete ID;
delete[] Name;
}
1
2
ID = new int;
delete ID;

I receive errors on these two lines......Errors are:
invalid conversion from `int*' to `int'
type `int' argument given to `delete', expected pointer

Why i get these errors? I also tried before but same errors....Can you explain about these errors.....
Pages: 12