Re: C++ dynamic structures




googlinggoogler@xxxxxxxxxxx wrote:

Hi,

Im new to C++ and trying to self teach myself whilst I sit at my
unentertaining day job (thought i'd put that across before im accused
of cheating on my homework, im 45...)


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.


Im pretty good at C so the reason for learning is so that I can truely
put C/C++ on my CV instead of just C... but also im interested in the
object orrientated aspect.


Could I use a class instead of a structure? whats best? where am I
going wrong?


Thanks


Heres my Code -


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


using namespace std;


struct TOKEN
{
string linetoken;



};


int main()
{

TOKEN *Tokens;


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


while(getline(ifs,line))
{


Tokens = new TOKEN[counter + 1];


Tokens[counter].linetoken = line;


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


cout << "AAAAAAAA";


delete Tokens;
return 0;
}

Hi there,

There are a couple of things here:
- your 'new' call is throwing away the old array of TOKENs each time
through the loop, so only your most recent token is ever valid.
- you need to 'delete []', not just 'delete'

However, I think the real reason for your crash is indeed using a
struct where you want to use a class. IIRC, I don't think that 'new'
calls the ctors of structs when it allocates them, whereas it does call
the ctor for classes. Hence, when you access .linetoken, you end up
accessing uninitialised garbage on the heap - causing you to fall over
shortly afterwards (probably chasing a garbage pointer within the
string object).

Just a guess - if I had more time I'd run it.

hth,
Doug

.



Relevant Pages

  • 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)
  • Bytecode source
    ... You can compile bytecode and get Forth code ... But it will probably be fast to compress and to decompress, ... Forth word with the string token followed by the length followed by the ... those must be made to store tokens instead of strings. ...
    (comp.lang.forth)
  • Re: WindowsIdentity - Invalid token; it cannot be duplicated
    ... Dominick Baier ... So, the login method is common and before that we received the Token, ... IntPtr iToken, string domainName, string userName) ... and why do you have to pass tokens around?? ...
    (microsoft.public.dotnet.security)
  • Re: parsing VB code with a regex
    ... Any characters to the right of the comment are ... So, now that we're down to a single string, and a few simple rules: ... All other tokens should be matched. ... regular expression, it is not available for further matching. ...
    (microsoft.public.dotnet.general)
  • Re: WindowsIdentity - Invalid token; it cannot be duplicated
    ... Because the login method will be used by asp.net application and also used ... passing the token to Login method and it tries to get the Priniciple. ... IntPtr iToken, string domainName, string userName) ... and why do you have to pass tokens around?? ...
    (microsoft.public.dotnet.security)