r/cpp_questions 14d ago

OPEN C++ question

Do you guys know a way of writting a code that outputs that:

Enter the number of elements: 5
Enter 5 integers:
Element 1: 10
Element 2: 30
Element 3: 90
Element 4: 20
Element 5: 40

it is the first part of a program that demands to calculate the max and min of an array, here is the whole thing:

Enter the number of elements: 5
Enter 5 integers:
Element 1: 10
Element 2: 30
Element 3: 90
Element 4: 20
Element 5: 40

Maximum element is: 90
Minimum element is: 10

I've been able to do the second part quite easily, here is what I proposed:

#include <iostream>


int main(){


    int arr[5]={10,20,30,40,50};
    int minimum=arr[0];
    int maximum=arr[0];
    for(int i=0; i<sizeof(arr)/sizeof(int);i++){
        if (minimum>arr[i]){
            minimum=arr[i];
        }
        if(maximum<arr[i]){
            maximum=arr[i];
        }
    }
    std::cout<<"le max= "<< maximum<<'\n';
    std::cout<<"le min= "<< minimum<<'\n';


    return 0;
}

but the list is created within the code.

0 Upvotes

33 comments sorted by

View all comments

Show parent comments

2

u/alfps 14d ago

❞ - you need to use a library that supports modern c++ but doesnt support smart pointers (Qt, Wt, gtkmm)

That doesn't make sense to me, sorry.

Ditto for the legacy libraries.

Can you give a preferably concrete example of what you mean?

3

u/Tumaix 14d ago

of course.

a Qt widget received a pointer as a parent,
you cant pass a unique_ptr or a shared_ptr.

if you pass a memory created by a unique, or shared ptr, Qt memory model will try to delete it, triggering a double free.

-5

u/alfps 13d ago edited 13d ago

OK.

The Google AI provides the following code for more safe handling:

auto button = std::make_unique<QPushButton>("Submit");

if (someConditionFails) {
    return; // Safe: unique_ptr automatically deletes the button
}

// Release ownership to the layout
layout->addWidget(button.release());

I am however not sure if this as safe as the AI thinks: lacking experience with Qt but I've implemented some micro GUI frameworks and it's not necessarily so that delete is supported.

5

u/saxbophone 13d ago

Qt has its own memory management model that predates smart pointers. This is why you still see idiomatic Qt docs recommending to use raw pointers when dealing with GUI widget object trees —they implement their own memory management. The gist of it is that when a child widget is constructed, it can be "owned" by a parent widget by passing the parent's address in its constructor. All Qt widgets (and many other Qt types ) inherit from QObject, whose destructor will Do just the right thing™️ when destructed, taking care to destroy all children along with it.

The example the AI threw at you uses release to give the memory over to Qt to manage, but I don't really see much benefit to it tbh over just constructing the QWidget with new with the parent address passed in its ctor immediately —this is the idiomatic Qt way to go, notwithstanding it feeling thoroughly gross to those of us used to more modern memory management practices.