Thread: Malloc with structs.

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    3

    Malloc with structs.

    I have a problem where I need to take a number of students, and then enter their names and a grade for each. I have it all working except for the malloc. I'm also using structs.
    Code:
    #define MaxNameLength 512
    #define MinStudents 1
    #define MaxStudents 10
    
    struct Student
    {
    	char*	names;
    	float	pGrades;
    };
    typedef  struct Student  Student;
     
    int main( int argc, char** argv )
    {
    	int		     numStudents = 0;
    	int		     i;
    	char		   longName[MaxNameLength];	
    	int		     nameLength;
    	Student*        students;
    
            Student.names =  malloc( numStudents * sizeof( Student.names* ) );
    	if( !Student.names )	
    	{
    		printf( "Insufficient memory to allocate names array of length %u\n",
    				numStudents * sizeof( char* ) );
    		exit( 1 );
    	}
    That is the first malloc for the students names.

    Code:
    	
            Student.pGrades =  malloc( numStudents * sizeof( Student.pGrades ) );
            if( !Student.pGrades )
            {
                    printf( "Insufficient memory to allocate names array of length %u\n",
                                    numStudents * sizeof( float ) );
                    exit( 1 );
            }
    This is the malloc for their grades.
    Any help would be appreciated, if this doesnt really make sense, I can post all of the code.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Consider the idiom:
    Code:
    p = malloc ( n * sizeof *p );
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    3
    Yes well by that the code would look like this.
    Code:
    struct Student
    {
    	char*	names;
    	float	pGrades;
    };
    typedef  struct Student  Student;
    
    int main( int argc, char** argv )
    {
        int                  numStudents;
        Student*        students;
    
        students = malloc( numStudents * sizeof( Student* )
    }
    This bit of code allocates for the whole of the struct, I want to allocate specifically for names and pGrades.

    Im not trying to allocate for type struct, I want to allocate names and pGrades which are char* and float. Im just not sure on the syntax of allocating something inside the struct. Showing me
    Code:
    p = malloc ( n * sizeof *p );
    doesnt help much.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    So do the same type of thing form the member name. In this case, n would be MaxNameLength?

    And no, it would be
    Code:
    students = malloc( numStudents * sizeof *students)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    Code:
    struct Student
    {
    	char*	names;
    	float	pGrades;
    };
    typedef  struct Student  Student;
    Code:
    Student.pGrades =  malloc( numStudents * sizeof( Student.pGrades ) );
    I think your a little mixed up as to what malloc does. When you allocate room for that structure, it will give you room for a pointer, and room for a float. you need need to allocate space for the pointer to point at (if you plan on using it). But you don't allocate space and try to set the float value to its starting address

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc for an Array of Structs
    By pseudonoma in forum C Programming
    Replies: 3
    Last Post: 03-26-2008, 01:55 PM
  2. malloc for structs and arrays
    By rkooij in forum C Programming
    Replies: 15
    Last Post: 05-04-2006, 07:38 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  4. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM