Problem with string in dynamic allocated struct

Hello,
I need help with allocating structures

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
struct vertex
{
	string vertex_name;
	int vertex_numb;
};

int main(void) 
{
	vertex *importsArray;
	
	importsArray=(vertex *)malloc((1)*sizeof(vertex));
	importsArray[0].vertex_name="x";
	importsArray[0].vertex_numb=1;


	for(int i=1;i<30;i++)
	{

		importsArray=(vertex *)realloc(importsArray,(i+1)*sizeof(vertex));
		importsArray[i].vertex_name="xx";
		importsArray[i].vertex_numb=i;
	
	}
        return 0;
}


I dont know, why this dont working. Thank you.
You cannot allocate C++ classes such as std::string using C allocation functions (malloc, realloc). This is why C++ has new and delete that supersede these.

So bottom line: If you have a non-POD struct, you cannot use malloc/realloc/calloc.
Problem solved. I put my struct into vector, thanks
Topic archived. No new replies allowed.