Re: STL vector push_back bug????

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: using virtual function in c
    ... Here, the machine will convert &ii (a word pointer) to a byte pointer, ... If there is only one "virtual function" for a given data structure, ...
    (comp.lang.c)
  • Re: "Mastering C Pointers"....
    ... A pointer is a kind of variable that can "point to" some object. ... has a type (pointer to int), and a value of some kind. ... You may know that you can access these integers by using array notation ... The function will take one argument, a string, and will return the length ...
    (comp.lang.c)

Loading