dynamic memory errors

/*Hi I am new to C++ language and I am still learning. It is difficult but I'm slowly getting there. I'm doing basic stuff to better understand dynamic memory. I was wondering why I keep getting memory issues.*/

#define SIZE 15
class word {
char* str;
word(const char[]);
//str[SIZE] = NULL; <----------I still had memory errors
strcpy(str, "Welcome");
word::word(const char s[]) {
int len = strlen(s);
//str[SIZE] = NULL; <----------had memory issues
strcpy(str, s);
word::~word() {
delete [] str;
int main() {
word w1, w2("Making One");
return 0;
You're not creating any dynamic memory here.

'str' is always just a pointer. With str[SIZE] you would point to byte 15 of the allocated memory block, but there is no memory block that's why you get the access violation.

Use 'new' to allocate the memory and THEN strcpy() to copy the string into the allocated memory block.

And, change delete [] str; to just delete str;.

BTW: to "clean" the pointer it's sufficient to write str = NULL; but it's not really necessary at all.
Topic archived. No new replies allowed.