Help with dynamically allocated array of pointer to a structure.

This is a discussion on Help with dynamically allocated array of pointer to a structure. within the C++ Programming forums, part of the General Programming Boards category; Code: bool AddRecord(StudentRecord**& Records, int& numRecords){ struct StudentRecord { char* firstName; // student key char* lastName; // student name int ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    18

    Angry Help with dynamically allocated array of pointer to a structure.

    Code:
    bool AddRecord(StudentRecord**& Records, int& numRecords){	
    	struct StudentRecord
    {
    	char* firstName;	// student key
    	char* lastName;	// student name
    	int id;		//student id
    	float mark;	// course mark
    };
    
                    StudentRecord**Temp; /*Declare array of pointers to a struct*/
    	Temp=new StudentRecord*[1000]; /*dynamically allocate memory*/
    	Temp[0]->firstName=new char[1000]; /*crashes here, why?*/
    
    }

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Code:
            StudentRecord**Temp; /*Declare array of pointers to a struct*/
    	Temp=new StudentRecord*[1000]; /*dynamically allocate memory*/
    at this point in your code you've allocated an array of pointers. Temp[0] is a pointer, but it's not pointing to anything yet so this line
    Code:
    Temp[0]->firstName=new char[1000];
    crashes because you are trying to access uninitialised memory.

    you need to do this first.
    Code:
            StudentRecord**Temp; /*Declare array of pointers to a struct*/
    	Temp=new StudentRecord*[1000]; /*dynamically allocate memory*/
    	Temp[0] = new StudentRecord; // allocate space for student record
    	Temp[0]->firstName=new char[1000]; // should work fine now
    I'm not sure why you're using an array of pointers anyway. A vector would be easier and safer or if you really need a container of pointers use a boost::ptr_vector (see my sig)
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The pointers in the array don't point to any StudentRecord structs yet. You have to create some StudentRecord structs and assign their addresses to the pointers in the array--before you can acesss the structs.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    18
    Quote Originally Posted by ChaosEngine
    Code:
            StudentRecord**Temp; /*Declare array of pointers to a struct*/
    	Temp=new StudentRecord*[1000]; /*dynamically allocate memory*/
    at this point in your code you've allocated an array of pointers. Temp[0] is a pointer, but it's not pointing to anything yet so this line
    Code:
    Temp[0]->firstName=new char[1000];
    crashes because you are trying to access uninitialised memory.

    you need to do this first.
    Code:
            StudentRecord**Temp; /*Declare array of pointers to a struct*/
    	Temp=new StudentRecord*[1000]; /*dynamically allocate memory*/
    	Temp[0] = new StudentRecord; // allocate space for student record
    	Temp[0]->firstName=new char[1000]; // should work fine now
    I'm not sure why you're using an array of pointers anyway. A vector would be easier and safer or if you really need a container of pointers use a boost:tr_vector (see my sig)
    Thank you it works!!

  5. #5
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Glad it works, but for the record this is a much more C++ way of coding
    Code:
    struct StudentRecord
    {
    	std::string firstName;	// student key
    	std::string lastName;	// student name
    	int id;		//student id
    	float mark;	// course mark
    };
    
    std::vector<StudentRecord> Temp(1000);
    Temp[0].firstName = "Chaos"
    shorter and safer, and uses a lot less memory (unless you know many people with 1000 character first names! )
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    18
    Quote Originally Posted by ChaosEngine
    Glad it works, but for the record this is a much more C++ way of coding
    Code:
    struct StudentRecord
    {
    	std::string firstName;	// student key
    	std::string lastName;	// student name
    	int id;		//student id
    	float mark;	// course mark
    };
    
    std::vector<StudentRecord> Temp(1000);
    Temp[0].firstName = "Chaos"
    shorter and safer, and uses a lot less memory (unless you know many people with 1000 character first names! )
    I don't understand this code. It is an assignment requirement to program it with array of pointers to struct. see http://ca.geocities.com/web8146@rogers.com assignment0 for your reference.

  7. #7
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Ok if you have to code it like that, fair enough. And I have to say it's refreshing to see someone asking for help with a problem in a decent way rather than asking us to do their assignment for them. Keep it up.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 6
    Last Post: 05-15-2009, 08:38 AM
  3. Dynamically Allocated Array
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-17-2007, 08:40 AM
  4. passing a 2dim dynamically allocated array to a funct
    By agerealm in forum C++ Programming
    Replies: 3
    Last Post: 03-10-2004, 05:55 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21