views:

281

answers:

4

how allocate memory dynamically for the array of stucture.... eg:

class students
{
    struct stud
        {
           int r_no;
           char name[20];
         }*s; 
}

how to allocate memory dynamically for *s...

+2  A: 

Why the extra wrapping of the struct in a class with nothing but a pointer?

Anyway, in C you'd do something like this:

struct stud
{
  int r_no;
  char name[20];
} *s;

size_t num_students = 4711;

s = malloc(num_students * sizeof *s);

Then it'd be prudent to go through and make sure all those individial structs are initialized, of course.

If you mean this to be C++, you should write constructors that take care of that, and use a new[] to allocate an array of structures.

unwind
Or calloc( num_students, sizeof(s) ). Don't forget to free(s) when you are done. If malloc'ed in the constuctor, and free()'ed in the destructor, watch out for the default copy_constructor leading to free()'ing the same pointer twice.
Mr.Ree
True, calloc() works here. Your sizeof has two problems though: no asterisk means it will allocate the size of the pointer, which is probably 4 rather than 24 in this case. Also, it has a pair of unneeded parentheses. :)
unwind
Good point! I meant to say sizeof(stud). (I don't like dereferencing s before a value is assigned to it, even when it is legal for use with sizeof.) As for the unneeded parenthesis: I like them. I think it makes the code more readable.
Mr.Ree
+5  A: 

First of all, this is not the way of doing it, as you could use a vector of stud, for instance. With the code as you have it, it would be something like:

class students
{
public:
    struct stud ... *s;
    students() // ctor
    {
        s = new stud[100]; // say you have 100 students
        // from now on you can use s[0], s[1], etc. in the class
    }
};

However, what you should be using is kind of an STL vector or list:

class students
{
public:
    struct stud ... ;
    std::vector<stud> my_students;
    students() // ctor
    {
        stud aStudent = {0, "Student Name"};
        my_students.push_back(aStudent); // add a new student.
    }
};
Diego Sevilla
2nd example: Why use both a class (students) and a vector?
dirkgently
Well... why not? :) We don't know the context. Maybe he or she wants to add some functionality to the class students...
Diego Sevilla
Because the vector is a tool to contain data, but the concept we want to represent (that hold those data) is a Student.If you think you only need the data and functions from vector, simply typedef instead of making it a class- beware of it's usage as all vector manipulations will be available on it
Klaim
I like this answer, but like Benoît's added point of using a string for name.
Mike
In the first example, you want to put "delete[] s" in the destructor and also implement a copy constructor (and operator=) to avoid accidentally copying "s" and thereby deleting it twice. (You really do want to use std::vector and std::string!)
Mr.Ree
mree, that's right. This why that's not the way to go... at least not the most easy or elegant.
Diego Sevilla
+2  A: 

You should make use of standard components when you can. Here std::string and std::vector would help you.

struct Student
{
    int r_no;
    std::string name;
};
typedef std::vector<Student> StudentList;

With such an approach, there is no point in wondering how to dynamically allocate memory. Everything's taken care of !

EDIT:

I simply typedef'ed StudentList because to me, adding more functionality to it would have been unrelated to the question. Clearly, the last line can be replaced with a true class definition:

class StudentList
{
    public:
        // Add your own functionalities here
    private:
    std::vector<Student> m_students;
};
Benoît
Agreed. But oooh. Your naming strategy. I know I'd mistype Student for Students somewhere and spend forever debugging it. Something like StudentSet or StudentVector or even Student_t would help.
Mr.Ree
Right, i'll change that. It does not bother me, but i can understand why it can :)
Benoît
@Benoît: You should call a spade a spade ;) Calling a vector a list can lead to a lot of unpleasant surprises later in the life cycle of a product?
dirkgently
@dirkgently: I get that a lot, but i only partly agree. People using StudentList should not be interested in knowing which kind of container is being used. But you're right, in the case of a typedef, it's a bit misleading !
Benoît
A: 
class students
{
public:
  students(size_t noOfStudents):m_noOfStudents(noOfStudents)
   {
     s = new stud[m_noOfStudents];
   }

   ~students()
   {
     delete s;
   }

private: 
   struct stud
        {
           int r_no;
           char name[20];
         }*s; 

   size_t m_noOfStudents;  
}
Alien01