Re: C++ dynamic structures



* googlinggoogler@xxxxxxxxxxx:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct TOKEN
{
string linetoken;
};

You can avoid some nasty errors by reserving all uppercase names for macros.


int main()
{

TOKEN *Tokens;

Use a std::vector<std::string>.


int counter = 0;
ifstream ifs("data.txt");
string line;

while(getline(ifs,line))
{

OK.


Tokens = new TOKEN[counter + 1];

Throwing away the old pointer value: memory leak.


Tokens[counter].linetoken = line;

OK, but inefficient.


//cout << "[ " << line << " ]" << endl;
cout << Tokens[counter].linetoken << endl;
counter = counter + 1;
}

cout << "AAAAAAAA";

delete Tokens;

When you use new[] you must use delete[].


return 0; }


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.