2

I have this class. Whenever I call the constructor, it gets called, for instance, with segments = 4. I get in Segments the address just for the first element instead of 4.

This is from clion debugger:

image

class Image{

    LinkedList<int,int> UnLabeledSegments;
    int* Segments;
    int NumOfSegments;

public:

    explicit Image(int segments);
    ~Image();

};

Image::Image(int segments):Segments(new int[segments]),NumOfSegments(segments){


    for(int i = 0; i < segments;i++){
        this->UnLabeledSegments.insert(i,-1);
        this->Segments[i] = -1;
    }
    
}

This is how I call the constructor the container is simple BST dictionary

void ImageTagger::AddImage(int ImageID) {
    ImageContainer.insert(ImageID,(new Image(ImageSegments)));
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
TomatoRage
  • 23
  • 3
  • 11
    You _do_ get 4 elements, your debugger just doesn't know the length so it can't show it correctly. – tkausl Sep 07 '21 at 19:00
  • See also [array to pointer decay](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay). – Nathan Pierson Sep 07 '21 at 19:03
  • 8
    Avoid the keyword `new` in modern C++! `Image` should contain a `std::vector Segments;` (which knows its own size), and probably `ImageContainer` should contain `Image` objects instead of `Image*` pointers. – aschepler Sep 07 '21 at 19:04
  • I don't know if in Clion if there is an option to tell the debugger to display the rest. In Visual Studio you can do this in the debugger by telling the debugger the number of elements to show. Related to this feature in Visual Studio: [https://stackoverflow.com/questions/75180/how-to-display-a-dynamically-allocated-array-in-the-visual-studio-debugger](https://stackoverflow.com/questions/75180/how-to-display-a-dynamically-allocated-array-in-the-visual-studio-debugger) – drescherjm Sep 07 '21 at 19:05
  • 3
    With your raw pointer usage (instead of `std::vector`), [the rule of 3/5/0](https://en.cppreference.com/w/cpp/language/rule_of_three) is probably going to bite you soon. Your `Image` type is copyable, but you won't like what those copies do. – Drew Dormann Sep 07 '21 at 19:09
  • 1
    *Constructor doesn't allocate* -- The title is not really correct. The constructor is working correctly. It is your debugger that is not behaving. – PaulMcKenzie Sep 07 '21 at 19:35

0 Answers0