Похожие чаты

Hey everyone, after using delete[] to free memory of dynamic

array i've got this error. what im doing wrong?

13 ответов

7 просмотров

can you copypaste your code somewhere (pastebin, hastebin), it is very small on this screenshot to read it from phone

probably calling delete[] on an already deleted array.

by the way, why don't you use std::string?

German- Автор вопроса
Pavel
can you copypaste your code somewhere (pastebin, h...

https://pastebin.com/FX7n5BJN https://pastebin.com/FSrBd3Z2 https://pastebin.com/D3VSaPKB

German- Автор вопроса
German
https://pastebin.com/FX7n5BJN https://pastebin.co...

here are 3 file of my code. 2 for class and 1 for main

German
https://pastebin.com/FX7n5BJN https://pastebin.co...

Your string created on stack as array of char (name1 in main()) then you call delete on it in the destructor of Player. And that because of this code in the constructor char* buffer = name; int name_size = strlen(buffer); name = new char [name_size + 1]; this->name = buffer; this->symbol = symbol; You assign name (input parameter) to buffer and then assign buffer to this->name.

German- Автор вопроса
Pavel
by the way, why don't you use std::string?

i cannot use std::string on this project (course rules...)

German
i cannot use std::string on this project (course r...

Ah, that's bad. Because in C++ we don't write code like this usually, it's very error prone (and extra verbose).

German- Автор вопроса
Pavel
Your string created on stack as array of char (nam...

Which way is good to fix it without using std::string?

German
i cannot use std::string on this project (course r...

what about smart pointers like std::shared_ptr<std::array<char>> ?

German
Which way is good to fix it without using std::str...

If feels like the code in the constructor is not entirely correct, you probably wanted to assign result of new directly to this->name? Also, pass name as const char* not char*, then compiler will not let you modify it accidentally

German
Which way is good to fix it without using std::str...

also, it seems like you need to do strcpy there, not assign string one to another

Похожие вопросы

Обсуждают сегодня

Какой-то там пердун в 90-х решил, что есть какая-то разная типизация. Кого вообще это волнует?
КТ315
49
Hi. Do we have a raid bot? Why nobody doing raids on X? Even RH mentioned this and nobody paying attention...whats the channel for hex memes? If mods cant run raids just insta...
H
31
Подскажите, а есть vault lite или ченить такое?) А то нужен вольт для похода в вольт, но весит он ~500 мб) как-то многовато для парочки запросов ))
Alexandr Orloff
17
блеать, почему так?? где в роутере это исправляется?
Арсен Маньяков 🇦🇲
16
void terminal_scroll() { memmove(terminal_buffer, terminal_buffer + VGA_WIDTH, buffer_size - VGA_WIDTH); memset(terminal_buffer + buffer_size - VGA_WIDTH, 0, VGA_WIDTH); ...
Егор
47
🌊 Ocean Nodes Dashboard Update 🚀 Hey, Oceaners! First off, a massive round of applause 👏to all of you for the amazing engagement since we launched Ocean Nodes. In just a few ...
KreigDK | Never DM first🌊
3
Всем привет! Подскажите, пожалуйста, в чем ошибка? Настраиваю подключение к MySQL. Либы лежат рядом с exe. Все как по "учебнику"
Евгений
16
А можете как-то проверить меня по знаниям по ассемблеру?
A A
132
Здравствуйте! У меня появилась возможность купить книгу "Изучай Haskell во имя добра!". Но я где-то слышал, что эта книга устарела. Насколько это правда??
E
22
люди, которые используют flameshot, к вам вопрос. Можно-ли поставить хоткей на создание скриншота? В программе есть отдел "горячие клавиши", но там все для редактирования, скр...
ThunDer104
11
Карта сайта