Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #12596

Re: Use of std::string Aborts

From drew@furrfu.invalid (Drew Lawson)
Newsgroups comp.lang.c++
Subject Re: Use of std::string Aborts
Date 2011-12-16 19:58 +0000
Organization Center for Unverified Assertions
Message-ID <jcg7tv$25sa$1@raid.furrfu.com> (permalink)
References <MPG.29554b332d4721d79896de@news.eternal-september.org>

Show all headers | View raw


In article <MPG.29554b332d4721d79896de@news.eternal-september.org>
	mrc2323@cox.net (Mike Copeland) writes:

>struct PHASEREC
>{
>    short   phaseType;
>    short   phaseNum;
>    string  phaseDescr;
>    string  phaseTitle;
>};
>struct DEF_STRUCT
>{
>    string   eventName;
>    PHASEREC Phases[5];
>} *Defs[9];
>
>int main()
>{
>    int iii, jjj;
>    for(iii = 0; iii < 9; iii++)
>    {
>        if(Defs[iii] == NULL) 
>	    Defs[iii] = (DEF_STRUCT*)malloc (sizeof(DEF_STRUCT));

malloc() allocates memory but does not initialize anything within
it.  So you have some memory for a std::string, but the content is
not meaningful.

If not changing anything else, you want:
 	    Defs[iii] = new DEF_STRUCT;

That will result in the constructors executing.

>        Defs[iii]->eventName = "";     // << abort here!!
>    }  // for
>    for(jjj = 0; jjj < 5; jjj++)// initialize Phase info
>    {
>	Defs[jjj]->Phases[iii].phaseNum   = 0; 
>       Defs[jjj]->Phases[iii].phaseType  = 0;
>	Defs[jjj]->Phases[iii].phaseDescr = ""; // << & here!!
>	Defs[jjj]->Phases[iii].phaseTitle = ""; // << & here!!
>    }  // for
>    return true;
>}


-- 
 Drew Lawson               So risk all or don't risk anything
			   You can lose all the same

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Use of std::string Aborts mrc2323@cox.net (Mike Copeland) - 2011-12-16 12:34 -0700
  Re: Use of std::string Aborts Leigh Johnston <leigh@i42.co.uk> - 2011-12-16 19:45 +0000
    Re: Use of std::string Aborts mrc2323@cox.net (Mike Copeland) - 2011-12-16 16:40 -0700
  Re: Use of std::string Aborts drew@furrfu.invalid (Drew Lawson) - 2011-12-16 19:58 +0000
    Re: Use of std::string Aborts mrc2323@cox.net (Mike Copeland) - 2011-12-16 16:42 -0700
  Re: Use of std::string Aborts Christopher <cpisz@austin.rr.com> - 2011-12-16 12:36 -0800
    Re: Use of std::string Aborts mrc2323@cox.net (Mike Copeland) - 2011-12-16 16:38 -0700
    Re: Use of std::string Aborts Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-12-18 17:08 +0000
      Re: Use of std::string Aborts red floyd <no.spam.here@its.invalid> - 2011-12-18 09:59 -0800
        Re: Use of std::string Aborts mrc2323@cox.net (Mike Copeland) - 2011-12-18 20:21 -0700
  Re: Use of std::string Aborts "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-12-16 23:07 +0100
  Re: Use of std::string Aborts Juha Nieminen <nospam@thanks.invalid> - 2011-12-17 07:40 +0000
    Re: Use of std::string Aborts mrc2323@cox.net (Mike Copeland) - 2011-12-18 20:24 -0700

csiph-web