0

I'm having issues while trying to get the typeid of an array. When I type typeid(*ptr[0]).name(), it works perfectly fine. but as soon as I change the 0 to a variable in a loop it gives me this error please help me!

#include "Cellphone.h"
#include "Electronic_device.h"
#include "Laptop.h"
#include "Smartwatch.h"
#include <vector>
#include <typeinfo>

int main()
{
    string name;
    int i = 0;
    Electronic_device** ptr = new Electronic_device*[100];
    Cellphone c1("Samsung", 1023, "black", 250.00, 1);
    Smartwatch s1("IBM", 10, "red", 350.00, 2);
    Laptop l1("HP", 102, "black", 1250.00, 16, true);
    ptr[0] = &c1;
    ptr[1] = &s1;
    ptr[2] = &l1;
    ptr[0]->print();
    
    for (i = 0; i < sizeof(ptr); i++)
    {
        if (typeid(*ptr[i]) == typeid(Cellphone))
        {
            cout << "Cellphone" << endl;
        }
        if (typeid(*ptr[i]) == typeid(Smartwatch))
        {
            cout << "Smartwatch" << endl;
        }
        if (typeid(*ptr[i]) == typeid(Laptop))
        {
            cout << "Laptop" << endl;
        }
    }
    
    ((Cellphone *)ptr[0])->printCellphone();
}
  • 1
    `0xCDCDCDCD` us a common [memory bit pattern](https://www.softwareverify.com/memory-bit-patterns.php) for uninitialized heap-allocated memory. – Some programmer dude Apr 08 '21 at 05:53
  • `sizeof(ptr)` will be 4 or 8, i.e. 32 or 64 bits, rather than 100. Use the same constant in both places. – user207421 Apr 08 '21 at 05:55
  • A big hint about your problem: `sizeof(ptr)` is the size (in *bytes*) of the *pointer* `ptr`, not the number of elements of any possible array it might point to. Not that it would be okay even if it was, since you don't initialize all elements of the array. Use `std::vector` instead! And `typeid(*ptr[i]) == typeid(Cellphone)` will not work, better use polymorphism and virtual functions instead. – Some programmer dude Apr 08 '21 at 05:56
  • And if you ever feel the need to use C-style casting (as in `(Cellphone *)ptr[0]`) then you should generally take that as a sign you're doing something wrong. Use `static_cast` here instead. – Some programmer dude Apr 08 '21 at 05:57
  • 1
    sizeof(ptr) doesn't give you how big the arrays is, it gives you size of the pointer in bytes – AndersK Apr 08 '21 at 05:59
  • 0xCD means clean memory. [When and why will a compiler initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?](https://stackoverflow.com/a/370362/995714) – phuclv Apr 08 '21 at 07:11
  • 1
    In general when you see a number that is extremely repetitious or looks sort of like a word or phrase, odds are really good tht the program is trying to tell you something and you should look up that number. When you get a weird number in decimal, turn it into hex and see if it is more recognizable/repetitious. – user4581301 Apr 08 '21 at 07:54

1 Answers1

0

You are reading uninitialized memory. First, initialize your array to zero, so that all pointers are null:

Electronic_device** ptr = new Electronic_device*[100]();

Secondly, you must check that the value is not null before you dereference the pointer (call *ptr[i]), for example:

for (i = 0; i < 100 && ptr[i] != nullptr; i++)

If you use dynamic_cast to check the derived class, then you don't have to dereference the pointer at all in your example:

if (dynamic_cast<Cellphone*>(ptr[i]) != nullptr) {
    cout << "Cellphone" << endl;
}
VLL
  • 9,634
  • 1
  • 29
  • 54
  • his array does not contain nullptr, since he never assigns it. for your solution to work he would have do a ptr[3] = nullptr as well. – AndersK Apr 08 '21 at 06:01
  • 1
    @AndersK That's why I instructed to initialize array to zero. Zero is equal to nullptr. See https://stackoverflow.com/questions/7546620/operator-new-initializes-memory-to-zero – VLL Apr 08 '21 at 06:12
  • I guess I didn't see that, I mostly look at code and gloss over the text :) bad habit maybe but I think it would have made your answer more clear. – AndersK Apr 09 '21 at 11:01
  • Thank you so much! By adding () to my array initialization and ptr[i] != nullptr to my for loop, it stopped giving the memory leak and was able to finish my assignment. – Michael Younes Apr 09 '21 at 16:19