Re: STL vector push_back bug????

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Tobias Güntner (fatbull_at_users.sourceforge.net)
Date: 07/29/04


Date: Fri, 30 Jul 2004 01:37:40 +0200

MakisGR wrote:
> TCITEM item;
> char sBuffer[50];
...
> item.pszText = sBuffer;
> item.cchTextMax = sizeof(sBuffer)/sizeof(char);
...
> orgTabs.push_back(item);

sBuffer will be destroyed when your function returns. Since only a
pointer to a string is stored in TCITEM::pszText, you end up with
several invalid pointers in your vector.
You should define your own data structure and copy all data, e.g.

struct MySafeTcItem
{
std::string title;
...
};
MySafeTcItem safe_item;
safe_item.title = item.pszText;
orgTabs.push_back(safe_item);

-- 
Regards,
Tobias


Relevant Pages

  • Re: Increasing efficiency in C
    ... > You don't know where the pointer will end pointing to. ... > representation of a C string. ... Wow Dan, ... My whole point is that data structure development should ...
    (comp.lang.c)
  • Re: Displaying the contents of pointers in cobol...
    ... > example both as a string of characters and as a string of digits. ... > passing variables by reference are taken care of in the linkage ... > AFAIK, COBOL does not have an operator which de-references a pointer, ... So if you are actually passing a data structure which ...
    (comp.lang.cobol)
  • Re: Strings in C are less optimal than in (say) Pascal - correct?
    ... in some sort of 'header' data structure, and that if a programmer wants to know the length of such a string, the resultant discovery is therefore very fast. ... a pointer" is generating a new string that is a slice of the existing string. ... A naive implementation would indeed be much slower in these cases, however -- of course, so would a naive implementation of repeated string concatenation in a terminator system be. ...
    (comp.lang.c)
  • Re: Efficency and the standard library
    ... loop will dereference a null pointer if argument strInstring is ... this code, out of adversarial hatred, envy and malice. ... Some C programmers in these threads would suggest const ... the output string. ...
    (comp.lang.c)
  • Re: ptrdiff_t overflow/underflow
    ... satisfy the vast majority of trim() use cases. ... It may point to a valid string, ... If any pointer can be converted to a 'void *', assuming there isn't any meta-data outside of the pointer's object representation, and assuming an implementation must not allow two 'void *' values to represent two separate objects, it seems like a fair upper bound. ... And if a pointer value includes bounds information, perhaps the implementation would be kind enough to document either the representation or how to access the information. ...
    (comp.lang.c)