Re: C++ dynamic structures
- From: "Alf P. Steinbach" <alfps@xxxxxxxx>
- Date: Tue, 29 Aug 2006 22:15:56 +0200
* 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?
.
- References:
- C++ dynamic structures
- From: googlinggoogler
- C++ dynamic structures
- Prev by Date: Re: C++ dynamic structures
- Next by Date: Re: Java or C++ College Path
- Previous by thread: Re: C++ dynamic structures
- Next by thread: Java or C++ College Path
- Index(es):