Re: C++ dynamic structures



googlinggoogler@xxxxxxxxxxx wrote:

Anyway I'm trying to dynamically assign a structure whilst I
read from a file, however my program crashes, and im not sure
why other than that its to do with my memory operations using
new and delete.

You can get pretty far in C++ without ever explicitly managing
memory. I recommend the book Accelerated C++ by Koenig and Moo. In
this case you should be using a container instead of newing arrays,
for instance a vector:

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

using namespace std;

struct TOKEN
{
TOKEN(string line) : linetoken(line) {}
string linetoken;
};

int main()
{
vector<TOKEN> Tokens;
ifstream ifs("data.txt");
string line;
while(getline(ifs,line))
{
Tokens.push_back(line);
cout << Tokens.back().linetoken << endl;
}
cout << "AAAAAAAA";
return 0;
}

I've given TOKEN a conversion constructor to be able to push strings
onto the vector without an intermediate TOKEN object. Note that the
standard mandates that repeated appending to a vector have linear
amortized complexity, but the example still works if you replace both
occurrences of "vector" with "list".

Doug wrote:

IIRC, I don't think that 'new' calls the ctors of structs when it
allocates them, whereas it does call the ctor for classes.

The only difference between struct and class is the default access
level of unqualified members. The distinction you have in mind is
between POD and non-POD (Googler, that's "Plain Old Data", a
technical term from the C++ standard. You can look it up.) As
std::string is non-POD so is TOKEN, which means that new default-
initializes each array element. The implicitly-defined default
constructor of TOKEN in turn default-constructs its members. It's
true that a POD array would stay uninitialized unless the newing line
ended in (), forcing zero-initialization.


Martin

--
Quidquid latine scriptum sit, altum viditur.
.



Relevant Pages

  • Re: Challenging text masking problem
    ... > create an array of integers representing the positions of %x tokens. ... It returns an array. ... When you split the output string into an array, all you need do is loop through ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Challenge - Regular Expression that divides a string at tokens
    ... Thus you would get an array like: ... I need to split a given string into sub-strings ... Now, I can do this in two parts, seperating the tokens from the literals ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Challenge - Regular Expression that divides a string at tokens
    ... I've worked out various solutions to do this with methods on the string ... The input string could begin with a token, and tokens can be nested (see the ... The ending algorithm is recursive object creation, ... Thus you would get an array like: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: function returning array of strings
    ... > I have to write a function which basically takes in a string and ... > returns an unknown number(at compile time) of strings ... /* this tells you how many tokens - allocate an array of pointer to hold ... pulling out all the tokens. ...
    (comp.lang.c)
  • WSE402: The message does not conform to the policy it was mapped t
    ... WSE 2 SP3 webservice that is requiring client side certs and username tokens: ... The message does not conform to the policy it was mapped to. ... expression, SoapEnvelope message, EndpointReference endpoint, String action, ...
    (microsoft.public.dotnet.framework.webservices.enhancements)