Problem with STL C List

I have a pointer in c:

list<int> * pointer = (list<int> *)malloc(sizeof(list<int>));

when i try:

pointer->push_back(1);

I get an error because malloc does not call the list constructor. I know to do this in C ++ with:

list<int> * pointer = new list<int>();

but do i need this in c?

Does anyone know a solution for this?

+3
source share
2 answers

No, because these are different languages. Just because only one text string “++” after a common letter in the name means nothing is the functional equivalent of trying to use a Java container in Python.

If you want to use STL, you need to use the C ++ compiler.

+7
source

"" new(). , malloc().

/* allocate memory using malloc */
list<int> * pointer = (list<int> *)malloc(sizeof(list<int>));

/* invoke the C++ constructor using the placement version of new */
pointer = new(pointer) list<int>();
0

Source: https://habr.com/ru/post/1782422/


All Articles