Thread: dynamic memory allocation - Please help

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    9

    dynamic memory allocation - Please help

    Code:
    struct name{
    	char user [10];
    	char color [10];
    }
    
    
    ....
    ....
    
    
    name student[5]	// 5 students
    ifstream in;
    int i = 0;
    in.open ("student.da");
    	while (!eof){
    		in.get >> student[i].user;
    		in.get >> student[i].color;
    		i++;
    	}
    
    
    I am trying to make above statements to use
    dynamic memory allocation using pointers.
    Since I am a newbie at this, I came up with
    
    
    struct name{
    	char* user;
    	char* color;
    }
    
    
    ....
    ....
    
    
    name* student = new name[5];	// 5 students
    ifstream in;
    int i = 0;
    in.open ("student.da");
    	while (!eof){
    		in.get >> student[i]->user;
    		in.get >> student[i]->color;
    		i++;
    	}
    Apparently above code is seriously ill. I don't know what I am doing wrong. Can someone please point out where?? Thanks.


    Code tags added by kermi3

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    struct name{
    	char* user;
    	char* color;
    }
    
    
    name* student = new name[5];	// 5 students
    ifstream in;
    int i = 0;
    in.open ("student.da");
    	while (!eof){
    		in.get >> student[i]->user;
    		in.get >> student[i]->color;
    		i++;
    	}
    First off, you need to initilize the pointers in your name structure before you can read anything in.

    Before that, you need to know how much you're reading in... so we create a buffer (pszBuffer) and read UP TO 255 characters OR until a new line is found. Then we read the length of the buffer, allocate enough memory to store the characters in the buffer, and copy them to our new memory.

    Code:
    struct name {
    char *user;
    char *color;
    };
    
    // ...
    
    name student[5];
    ifstream in("student.da");
    char pszBuffer[256];
    
    int count = 0;
    
    while (!in.eof())
    {
         in.getline(pszBuffer, 255, '\n'); // read in a single line
         student[count].user = new char[strlen(pszBuffer) + 1];
         strcpy(student[count].user, pszBuffer);
    
         in.getline(pszBuffer, 255, '\n');
         student[count].color = new char[strlen(pszBuffer) + 1];
         strcpy(student[count].color, pszBuffer);
    
         count++;
    }
    Last edited by Eibro; 11-13-2002 at 01:56 PM.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    9

    Re: Code Tags

    Thank you.
    I will make sure I follow the guideline next time.


    BTW, do you know what my problem is?

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    9
    Thanks Eibro!
    Would you not need to initialize

    name student[5];
    ??

    Originally posted by Eibro
    Code:
    struct name{
    	char* user;
    	char* color;
    }
    
    
    name* student = new name[5];	// 5 students
    ifstream in;
    int i = 0;
    in.open ("student.da");
    	while (!eof){
    		in.get >> student[i]->user;
    		in.get >> student[i]->color;
    		i++;
    	}
    First off, you need to initilize the pointers in your name structure before you can read anything in.

    Before that, you need to know how much you're reading in... so we create a buffer (pszBuffer) and read UP TO 255 characters OR until a new line is found. Then we read the length of the buffer, allocate enough memory to store the characters in the buffer, and copy them to our new memory.

    Code:
    struct name {
    char *user;
    char *color;
    };
    
    // ...
    
    name student[5];
    ifstream in("student.da");
    char pszBuffer[256];
    
    int count = 0;
    
    while (!in.eof())
    {
         in.getline(pszBuffer, 255, '\n'); // read in a single line
         student[count].user = new char[strlen(pszBuffer) + 1];
         strcpy(student[count].user, pszBuffer);
    
         in.getline(pszBuffer, 255, '\n');
         student[count].color = new char[strlen(pszBuffer) + 1];
         strcpy(student[count].color, pszBuffer);
    
         count++;
    }

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    9
    Thanks Eibro!
    Would you not need to initialize

    name student[5];
    ??

    Originally posted by Eibro
    Code:
    struct name{
    	char* user;
    	char* color;
    }
    
    
    name* student = new name[5];	// 5 students
    ifstream in;
    int i = 0;
    in.open ("student.da");
    	while (!eof){
    		in.get >> student[i]->user;
    		in.get >> student[i]->color;
    		i++;
    	}
    First off, you need to initilize the pointers in your name structure before you can read anything in.

    Before that, you need to know how much you're reading in... so we create a buffer (pszBuffer) and read UP TO 255 characters OR until a new line is found. Then we read the length of the buffer, allocate enough memory to store the characters in the buffer, and copy them to our new memory.

    Code:
    struct name {
    char *user;
    char *color;
    };
    
    // ...
    
    name student[5];
    ifstream in("student.da");
    char pszBuffer[256];
    
    int count = 0;
    
    while (!in.eof())
    {
         in.getline(pszBuffer, 255, '\n'); // read in a single line
         student[count].user = new char[strlen(pszBuffer) + 1];
         strcpy(student[count].user, pszBuffer);
    
         in.getline(pszBuffer, 255, '\n');
         student[count].color = new char[strlen(pszBuffer) + 1];
         strcpy(student[count].color, pszBuffer);
    
         count++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM