vector push_back crashes application

In my C++ app. code created using VS 2008 I have used vector to store name of comp.The issue I m facing is if comp name is somewhat smaller (lets say "p1") it works fine but if it is like "component_of_part-5909" application crashes at push_back .What could be the reason of this?? here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
vector<char*> compList;

convertPosition()
{
wchar_t newCompName;
compType mdl;
if(compList.size() == 0)
{

COnvertingAPI(mdl,newCompName); //this is specific to my app.
char* charTemp = new char();
ConvertWstringToString(charTemp, newCompName); //convert wchar_t string to the char*
compList.push_back(charTemp); //insert name of component into a vector
}
else
{
COnvertingAPI(mdl,newCompName); 
char* charTemp = new char();
ConvertWstringToString(charTemp, newCompName);
compList.push_back(charTemp); //app crashes here

}

} 
well, the char * new is not allocating any space, well actually just a single char *.
I guess ConvertWstringToString writes to a buffer so it's writing to undefined memory.

why are you converting to char*? why not store a string in the vector?
It's a lot easier and strings on vectors are unbelievably fast.



also if you store pointers on a vector it's probably wise to use
boost::shared_ptr
I modified is as char* charTemp = new char[1000] -atleast crash point is passed what I see is some garbage string is getting inserted in vector causing dependent code to fail.
Any guess why this s happening?
Well what does ConvertWstringToString do? How do you expect us to know? Perhaps it isn't attaching a null character to the end of whatever you are filling the buffer with. This is obviously a very bad design so there could be a lot of problems in the code that we cannot see.
Topic archived. No new replies allowed.