-3

I can create a dynamic 2d-array of 3x2 ints and I can delete it without problems. But when doing the same with a 2d-array of strings, deleting it generates the error:

munmap_chunk(): invalid pointer

Why? This lack of homogeneity between ints and strings is preventing me from writing a template that can be instantiated with strings.

I know there are automatic pointers. I know there are better alternatives to primitive language arrays. But I am a teacher and I am trying to introduce the subjects one by one, so I still cannot use those more advanced topics. I am trying to explain abstract types of data with templates.

#include<string>
#include<iostream>

int main()
{
  std::cout << "2d-ARRAY of ints" << std::endl;
  int **a = new int*[3];
  for(int i=0; i<3; i++)
    a[i] = new int[2];
  for(int i=0; i<3; i++)
    delete a[i];
  delete [] a;

  std::cout << "2d-ARRAY of strings" << std::endl;
  std::string **s = new std::string*[3];
  for(int i=0; i<3; i++)
    s[i] = new std::string[2];
  for(int i=0; i<3; i++)
    delete s[i];
  delete [] s;
  return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
angarciaba
  • 93
  • 5
  • 1
    You have to use delete [] instead of the simple delete. For example delete [] a[i]; – Vlad from Moscow Aug 22 '19 at 12:17
  • 6
    You're a teacher... why are you teaching creating 2D arrays like this? Use std::vectors. – The Forest And The Trees Aug 22 '19 at 12:17
  • 4
    use `std::array` for fixed size and `std::vector` otherwise, I dont see a reason to enter such difficulties here – 463035818_is_not_an_ai Aug 22 '19 at 12:17
  • Should be `delete[] a[i]` and `delete[] s[i]`. – VLL Aug 22 '19 at 12:17
  • 2
    "so I still cannot use those more advanced topic" ... using c-arrays and proper manual memory managment is super advanced. Using `std::vector` is easy. – 463035818_is_not_an_ai Aug 22 '19 at 12:19
  • 4
    I'm doubtful that teaching templates before teaching standard containers is a logical order. You don't need to understand templates to understand `vector`s or how to use them. You only need to understand templates to *implement* them. And standard containers should be one of the early things taught. Probably even before dynamic allocation and C arrays. – François Andrieux Aug 22 '19 at 12:20
  • Even if your excuse about being a teacher had any logic, then by that same logic, why is it somehow OK to use `std::string` instead of char arrays? Wouldn't students have a lot more fun and productive times if they were forgetting to free strings and forgetting to add null terminators and using `strtok()` and all that other crappy stuff that C++ should've left behind by now? [rolls eyes] – underscore_d Aug 22 '19 at 12:22
  • 1
    *"I still cannot use those more advanced topics"* - Raw pointers for dynamic allocation are the advanced topic. Virtually no one needs to use them for that purpose - it is only really needed for specialized things. Most people only every need `std::array` and `std::vector`. – Galik Aug 22 '19 at 12:26
  • 1
    There is no lack of homogeneity - your use of `delete` when you should `delete []` has undefined behaviour. – molbdnilo Aug 22 '19 at 12:28
  • 6
    Is it deliberately ironic that instead of "more advanced topics", you have decided to teach "basic" yet clearly error-prone memory management that even you can't get right? :) – Lightness Races in Orbit Aug 22 '19 at 12:30
  • 1
    Also **please** don't teach this kind of data layout. There is no need for a vector of vectors, or the manual equivalent (which this is). An array of pointers to pointers is almost always wrong. I spend a good chunk of each month here undoing the damage that causes. – Lightness Races in Orbit Aug 22 '19 at 12:30
  • 1
    All teaching like this does is to make students leave C++ forever and go onto other languages that they can actually create real programs with. A new student to C++ is not going to get freaked out by seeing `std::vector` since the new student is new -- they have not been biased by seeing `C` syntax with pointers and believing `int *` "looks easier". – PaulMcKenzie Aug 22 '19 at 13:26
  • I recommend watching this advice for teaching C++ https://www.youtube.com/watch?v=YnWhqhNdYyk – Galik Aug 22 '19 at 14:30

1 Answers1

3

Instead of unsafe (as your program has demonstrated) dynamically "manually" allocated arrays C++ suggests the standard container std::vector.

Nevertheless as for your program then you are using an invalid operator delete with dynamically allocated arrays.

Instead of

delete a[i];

and

delete s[i];

you have to use

delete [] a[i];

and

delete [] s[i];

But I am a teacher and I am trying to introduce the subjects one by one, so I still cannot use those more advanced topics. I am trying to explain abstract types of data with templates.

I see nothing bad in this approach of teaching. After seeing what difficulties arise using dynamically allocated arrays the students will better understand the advantages of using standard containers.:)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • _"After seeing"_ Therein lies the rub. Will they see it? Would they even have been aware of it had this program not happened to cause a memory fault? :( – Lightness Races in Orbit Aug 22 '19 at 12:32
  • @LightnessRacesinOrbit What about for example exceptions and forgetting to free a memory or changing the initial address of allocated memory?:) – Vlad from Moscow Aug 22 '19 at 12:33
  • 1
    @VladfromMoscow To see the complexity, you need something to compare it. Otherwise, it looks like the normal thing to do. This is another reason starting with the easier higher level solutions is better. When they get to manual memory management, it sucks for them and they appreciate the cognitive burden that performing memory management yourself imposes. – François Andrieux Aug 22 '19 at 12:34
  • @FrançoisAndrieux I saw two approaches. And even such a construction as std::cout << something; is harder understandable compared with printf.:) – Vlad from Moscow Aug 22 '19 at 12:35