Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #86031 > unrolled thread
| Started by | JiiPee <kerrttuPoistaTama11@gmail.com> |
|---|---|
| First post | 2022-08-24 20:00 +0300 |
| Last post | 2022-08-26 16:08 -0500 |
| Articles | 20 on this page of 27 — 13 participants |
Back to article view | Back to comp.lang.c++
Is it possible to test if a raw pointer is pointing to a valid object? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-08-24 20:00 +0300
Re: Is it possible to test if a raw pointer is pointing to a valid object? Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-24 20:24 +0300
Re: Is it possible to test if a raw pointer is pointing to a valid object? Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-24 20:28 +0300
Re: Is it possible to test if a raw pointer is pointing to a valid object? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-08-24 20:39 +0300
Re: Is it possible to test if a raw pointer is pointing to a valid object? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-24 10:44 -0700
Re: Is it possible to test if a raw pointer is pointing to a valid object? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-08-24 20:51 +0300
Re: Is it possible to test if a raw pointer is pointing to a valid object? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-08-24 20:52 +0300
Re: Is it possible to test if a raw pointer is pointing to a valid object? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-24 11:59 -0700
Re: Is it possible to test if a raw pointer is pointing to a valid object? Manfred <noname@add.invalid> - 2022-08-26 02:26 +0200
Re: Is it possible to test if a raw pointer is pointing to a valid object? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-08-24 20:41 +0300
Re: Is it possible to test if a raw pointer is pointing to a valid object? Louis Krupp <lkrupp@invalid.pssw.com.invalid> - 2022-08-24 13:51 -0600
Re: Is it possible to test if a raw pointer is pointing to a valid object? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-08-24 12:45 -0700
Re: Is it possible to test if a raw pointer is pointing to a valid object? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-08-24 23:17 +0300
Re: Is it possible to test if a raw pointer is pointing to a valid object? scott@slp53.sl.home (Scott Lurndal) - 2022-08-24 20:54 +0000
Re: Is it possible to test if a raw pointer is pointing to a valid object? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-08-24 14:57 -0700
Re: Is it possible to test if a raw pointer is pointing to a valid object? Vir Campestris <vir.campestris@invalid.invalid> - 2022-08-25 12:52 +0100
Re: Is it possible to test if a raw pointer is pointing to a valid object? Sam <sam@email-scan.com> - 2022-08-24 20:21 -0400
Re: Is it possible to test if a raw pointer is pointing to a valid object? Bonita Montero <Bonita.Montero@gmail.com> - 2022-08-25 07:39 +0200
Re: Is it possible to test if a raw pointer is pointing to a valid object? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-08-25 03:37 -0700
Re: Is it possible to test if a raw pointer is pointing to a valid object? Sam <sam@email-scan.com> - 2022-08-25 09:37 -0400
Re: Is it possible to test if a raw pointer is pointing to a valid object? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-08-25 17:28 +0300
Re: Is it possible to test if a raw pointer is pointing to a valid object? Sam <sam@email-scan.com> - 2022-08-25 19:39 -0400
Re: Is it possible to test if a raw pointer is pointing to a valid object? Paul N <gw7rib@aol.com> - 2022-08-26 05:49 -0700
Re: Is it possible to test if a raw pointer is pointing to a valid object? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-08-26 19:35 +0300
Re: Is it possible to test if a raw pointer is pointing to a valid object? Louis Krupp <lkrupp@invalid.pssw.com.invalid> - 2022-08-26 12:25 -0600
Re: Is it possible to test if a raw pointer is pointing to a valid object? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-08-25 11:38 -0700
Re: Is it possible to test if a raw pointer is pointing to a valid object? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-08-26 16:08 -0500
Page 1 of 2 [1] 2 Next page →
| From | JiiPee <kerrttuPoistaTama11@gmail.com> |
|---|---|
| Date | 2022-08-24 20:00 +0300 |
| Subject | Is it possible to test if a raw pointer is pointing to a valid object? |
| Message-ID | <te5lf8$3camt$1@dont-email.me> |
First the code. The question below it:
------------------------
class Parent
{
public:
void foo2() {}
};
class Child
{
public:
Parent* m_parent{nullptr};
void foo() {
// can the validity of the raw pointer m_parent be checked?
m_parent->foo2();
}
};
void main()
{
Parent* parent = new Parent;
Child child;
child.m_parent = parent;
delete parent;
child.foo(); // any way this call knowing parent is now deleted?
}
------------------
Is there any way to check if a raw pointer is pointing to a valid object?
Because I have situations where I can only store raw pointers.
I know it can be done with shared pointers, but how about raw pointers?
Assuming I cannot change the Parent class in the above example.
Or is there any pattern applied to Child class... again assuming we
cannot change the Parent class at all.
[toc] | [next] | [standalone]
| From | Paavo Helde <eesnimi@osa.pri.ee> |
|---|---|
| Date | 2022-08-24 20:24 +0300 |
| Message-ID | <te5mrp$3ceup$1@dont-email.me> |
| In reply to | #86031 |
24.08.2022 20:00 JiiPee kirjutas:
> First the code. The question below it:
>
> ------------------------
> class Parent
> {
> public:
> void foo2() {}
> };
>
> class Child
> {
> public:
> Parent* m_parent{nullptr};
> void foo() {
> // can the validity of the raw pointer m_parent be checked?
> m_parent->foo2();
> }
> };
>
> void main()
> {
> Parent* parent = new Parent;
> Child child;
> child.m_parent = parent;
> delete parent;
> child.foo(); // any way this call knowing parent is now deleted?
No, there isn't any way. At best you can check if a pointer points to
memory page which is readable or writable for the process, but that does
not help much.
>
> }
>
> ------------------
>
> Is there any way to check if a raw pointer is pointing to a valid object?
> Because I have situations where I can only store raw pointers.
> I know it can be done with shared pointers, but how about raw pointers?
> Assuming I cannot change the Parent class in the above example.
> Or is there any pattern applied to Child class... again assuming we
> cannot change the Parent class at all.
The tedious way is to not delete parent without informing all the
children that the parent is dead.
A smarter pattern is smart pointers. You do not need to modify Parent
class for using smart pointers on it.
class Child
{
public:
std::weak_ptr<Parent> m_parent;
void foo() {
if (auto parent = m_parent.lock()) {
parent->foo2();
}
}
};
void main()
{
std::shared_ptr parent = std::allocate_shared<Parent>();
Child child;
child.m_parent = parent;
parent = nullptr; // deletes parent
child.foo(); // OK
}
[toc] | [prev] | [next] | [standalone]
| From | Paavo Helde <eesnimi@osa.pri.ee> |
|---|---|
| Date | 2022-08-24 20:28 +0300 |
| Message-ID | <te5n4n$3cfq1$1@dont-email.me> |
| In reply to | #86032 |
24.08.2022 20:24 Paavo Helde kirjutas: > std::shared_ptr parent = std::allocate_shared<Parent>(); Sorry, this should have been std::make_shared<Parent>();
[toc] | [prev] | [next] | [standalone]
| From | JiiPee <kerrttuPoistaTama11@gmail.com> |
|---|---|
| Date | 2022-08-24 20:39 +0300 |
| Message-ID | <te5nns$3cgqn$1@dont-email.me> |
| In reply to | #86032 |
On 24/08/2022 20:24, Paavo Helde wrote: > The tedious way is to not delete parent without informing all the > children that the parent is dead. Yes but then would need to change the Parent. Which I cannot really do. > > A smarter pattern is smart pointers. You do not need to modify Parent > class for using smart pointers on it. Yes I know this... and this is obviously best way to do it if can.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2022-08-24 10:44 -0700 |
| Message-ID | <874jy1y1ss.fsf@nosuchdomain.example.com> |
| In reply to | #86032 |
Paavo Helde <eesnimi@osa.pri.ee> writes:
> 24.08.2022 20:00 JiiPee kirjutas:
>> First the code. The question below it:
>> ------------------------
>> class Parent
>> {
>> public:
>> void foo2() {}
>> };
>> class Child
>> {
>> public:
>> Parent* m_parent{nullptr};
>> void foo() {
>> // can the validity of the raw pointer m_parent be checked?
>> m_parent->foo2();
>> }
>> };
>> void main()
>> {
>> Parent* parent = new Parent;
>> Child child;
>> child.m_parent = parent;
>> delete parent;
>> child.foo(); // any way this call knowing parent is now deleted?
>
> No, there isn't any way. At best you can check if a pointer points to
> memory page which is readable or writable for the process, but that
> does not help much.
There's no portable way to do even that.
[...]
> void main()
Of course main returns int, not void. (I know that was in the parent
article.)
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */
[toc] | [prev] | [next] | [standalone]
| From | JiiPee <kerrttuPoistaTama11@gmail.com> |
|---|---|
| Date | 2022-08-24 20:51 +0300 |
| Message-ID | <te5oet$3cjid$1@dont-email.me> |
| In reply to | #86038 |
On 24/08/2022 20:44, Keith Thompson wrote: >> void main() > Of course main returns int, not void. (I know that was in the parent > article.) but its shorter to type, hehe
[toc] | [prev] | [next] | [standalone]
| From | JiiPee <kerrttuPoistaTama11@gmail.com> |
|---|---|
| Date | 2022-08-24 20:52 +0300 |
| Message-ID | <te5oh4$3cjid$2@dont-email.me> |
| In reply to | #86039 |
On 24/08/2022 20:51, JiiPee wrote: > On 24/08/2022 20:44, Keith Thompson wrote: >>> void main() >> Of course main returns int, not void. (I know that was in the parent >> article.) > > but its shorter to type, hehe actually compiles in visual studio
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2022-08-24 11:59 -0700 |
| Message-ID | <87zgftwjq2.fsf@nosuchdomain.example.com> |
| In reply to | #86040 |
JiiPee <kerrttuPoistaTama11@gmail.com> writes:
> On 24/08/2022 20:51, JiiPee wrote:
>> On 24/08/2022 20:44, Keith Thompson wrote:
>>>> void main()
>>> Of course main returns int, not void. (I know that was in the parent
>>> article.)
>> but its shorter to type, hehe
>
> actually compiles in visual studio
Most C++ compilers aren't fully conforming by default. Any conforming
C++ implementation must diagnose `void main()` (and may then either
accept it or reject it).
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */
[toc] | [prev] | [next] | [standalone]
| From | Manfred <noname@add.invalid> |
|---|---|
| Date | 2022-08-26 02:26 +0200 |
| Message-ID | <te940g$1f3u$1@gioia.aioe.org> |
| In reply to | #86041 |
On 8/24/2022 8:59 PM, Keith Thompson wrote: > JiiPee <kerrttuPoistaTama11@gmail.com> writes: >> On 24/08/2022 20:51, JiiPee wrote: >>> On 24/08/2022 20:44, Keith Thompson wrote: >>>>> void main() >>>> Of course main returns int, not void. (I know that was in the parent >>>> article.) >>> but its shorter to type, hehe >> >> actually compiles in visual studio > > Most C++ compilers aren't fully conforming by default. Any conforming > C++ implementation must diagnose `void main()` (and may then either > accept it or reject it). > Curiously, the C standard poses no requirements on main() for freestanding implementations. Apparently the C++ standard does instead: it says that main() may not be present in a freestanding implementation, but, as it is written, if it is present then it must have int return type.
[toc] | [prev] | [next] | [standalone]
| From | JiiPee <kerrttuPoistaTama11@gmail.com> |
|---|---|
| Date | 2022-08-24 20:41 +0300 |
| Message-ID | <te5nru$3cgqn$2@dont-email.me> |
| In reply to | #86031 |
On 24/08/2022 20:29, Stefan Ram wrote: > You could write management functions that take care > of the notifications required: Yes possible, and good if can change parent. But that would need to change the Parent class which I cannot always do.
[toc] | [prev] | [next] | [standalone]
| From | Louis Krupp <lkrupp@invalid.pssw.com.invalid> |
|---|---|
| Date | 2022-08-24 13:51 -0600 |
| Message-ID | <ZivNK.890342$wIO9.605910@fx12.iad> |
| In reply to | #86036 |
On 8/24/2022 11:41 AM, JiiPee wrote: > On 24/08/2022 20:29, Stefan Ram wrote: >> You could write management functions that take care >> of the notifications required: > > Yes possible, and good if can change parent. But that would need to > change the Parent class which I cannot always do. Wild guess: Could you have a class that contains a Parent object and calls its methods while doing notifications and anything else you need? If you don't use pointers to the Parent class itself, you might not have to change it. Louis
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.arthur.mclean@gmail.com> |
|---|---|
| Date | 2022-08-24 12:45 -0700 |
| Message-ID | <857c581a-284f-4c44-8554-a2146f980213n@googlegroups.com> |
| In reply to | #86031 |
On Wednesday, 24 August 2022 at 18:00:43 UTC+1, JiiPee wrote:
> First the code. The question below it:
>
> ------------------------
> class Parent
> {
> public:
> void foo2() {}
> };
>
> class Child
> {
> public:
> Parent* m_parent{nullptr};
> void foo() {
> // can the validity of the raw pointer m_parent be checked?
> m_parent->foo2();
> }
> };
>
> void main()
> {
> Parent* parent = new Parent;
> Child child;
> child.m_parent = parent;
> delete parent;
> child.foo(); // any way this call knowing parent is now deleted?
>
> }
>
> ------------------
>
> Is there any way to check if a raw pointer is pointing to a valid object?
> Because I have situations where I can only store raw pointers.
> I know it can be done with shared pointers, but how about raw pointers?
> Assuming I cannot change the Parent class in the above example.
> Or is there any pattern applied to Child class... again assuming we
> cannot change the Parent class at all.
>
The only way to do it is to overload the new and delete operators to maintain a
table of valid pointers. You can then test the pointers for validity (though note
that memory might be re-used, so you have to have something more sophisticated
than a simple list).
This isn't practical unless you are building a system from the ground up.
[toc] | [prev] | [next] | [standalone]
| From | JiiPee <kerrttuPoistaTama11@gmail.com> |
|---|---|
| Date | 2022-08-24 23:17 +0300 |
| Message-ID | <te610b$3dfj4$1@dont-email.me> |
| In reply to | #86042 |
On 24/08/2022 22:45, Malcolm McLean wrote: > The only way to do it is to overload the new and delete operators to maintain a > table of valid pointers. Yes, that is a new idea. At least can consider...
[toc] | [prev] | [next] | [standalone]
| From | scott@slp53.sl.home (Scott Lurndal) |
|---|---|
| Date | 2022-08-24 20:54 +0000 |
| Message-ID | <wewNK.160857$ze2.74999@fx36.iad> |
| In reply to | #86044 |
JiiPee <kerrttuPoistaTama11@gmail.com> writes:
>On 24/08/2022 22:45, Malcolm McLean wrote:
>> The only way to do it is to overload the new and delete operators to maintain a
>> table of valid pointers.
>
>Yes, that is a new idea. At least can consider...
If every class derives from a common base class, the base class
can contain a magic number that identifies the object as valid.
e.g.
class Object {
static const uint64_t MAGIC_NUMBER = 0x23324abc3242ffaaul;
uint64_t o_magic;
public:
Object(void) : o_magic(MAGIC_NUMBER) {}
bool is_valid(void) const { return o_magic == MAGIC_NUMBER; }
};
class MyObject : Object {
...
};
MyObject *objp = ...;
if (objp->is_valid()) {
this is a valid, initialized object.
}
This approach also allows some degree of introspection if you
add additional member functions to the Object class.
The JAVA object system uses this concept.
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.arthur.mclean@gmail.com> |
|---|---|
| Date | 2022-08-24 14:57 -0700 |
| Message-ID | <425bd249-ca2c-485a-b497-5d3e333f58fdn@googlegroups.com> |
| In reply to | #86046 |
On Wednesday, 24 August 2022 at 21:55:07 UTC+1, Scott Lurndal wrote:
> JiiPee <kerrttuPo...@gmail.com> writes:
> >On 24/08/2022 22:45, Malcolm McLean wrote:
> >> The only way to do it is to overload the new and delete operators to maintain a
> >> table of valid pointers.
> >
> >Yes, that is a new idea. At least can consider...
> If every class derives from a common base class, the base class
> can contain a magic number that identifies the object as valid.
>
> e.g.
>
> class Object {
> static const uint64_t MAGIC_NUMBER = 0x23324abc3242ffaaul;
> uint64_t o_magic;
>
> public:
> Object(void) : o_magic(MAGIC_NUMBER) {}
>
> bool is_valid(void) const { return o_magic == MAGIC_NUMBER; }
> };
>
> class MyObject : Object {
> ...
> };
>
> MyObject *objp = ...;
>
> if (objp->is_valid()) {
> this is a valid, initialized object.
> }
>
>
> This approach also allows some degree of introspection if you
> add additional member functions to the Object class.
>
> The JAVA object system uses this concept.
>
On some systems, if you delete objp, then call is_valid() on it, you
will get a segmentation fault. The implementation efectively has its
own mechanism for checking for valid objects, and "protects" the
program by shutting it down if an invalid object is accessed.
[toc] | [prev] | [next] | [standalone]
| From | Vir Campestris <vir.campestris@invalid.invalid> |
|---|---|
| Date | 2022-08-25 12:52 +0100 |
| Message-ID | <te7npn$3kvh4$1@dont-email.me> |
| In reply to | #86047 |
On 24/08/2022 22:57, Malcolm McLean wrote: > On some systems, if you delete objp, then call is_valid() on it, you > will get a segmentation fault. The implementation efectively has its > own mechanism for checking for valid objects, and "protects" the > program by shutting it down if an invalid object is accessed. That's not reliable unless you can guarantee that an area of memory is never re-used. You can only make that guarantee if you know you will never run out of address space. Remember that this also requires that each object will need to exist in its own page, so their memory allocations will be padded out to a page boundary - typically an average of 2k for every single object. I had this problem when I started my last job, and I re-architected all the pointers to a particular object to be shared pointers. It was a PITA, resulted in senior engineers asking who the hell was I (and luckily getting a calming reply), resulted in a memory leak a bit later - and made that build the most reliable we'd ever had. Andy
[toc] | [prev] | [next] | [standalone]
| From | Sam <sam@email-scan.com> |
|---|---|
| Date | 2022-08-24 20:21 -0400 |
| Subject | Re: Is it possible to test if a raw pointer is pointing to a valid object? |
| Message-ID | <cone.1661386861.462033.186378.1004@monster.email-scan.com> |
| In reply to | #86031 |
JiiPee writes: > Is there any way to check if a raw pointer is pointing to a valid object? No there isn't. End of story.
[toc] | [prev] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2022-08-25 07:39 +0200 |
| Message-ID | <te71ur$3j5pf$1@dont-email.me> |
| In reply to | #86031 |
Am 24.08.2022 um 19:00 schrieb JiiPee: > Is there any way to check if a raw pointer is pointing to a valid object? What's the practical use of that ?
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.arthur.mclean@gmail.com> |
|---|---|
| Date | 2022-08-25 03:37 -0700 |
| Message-ID | <7da49ad0-8cff-40c6-8f74-8e891476021en@googlegroups.com> |
| In reply to | #86051 |
On Thursday, 25 August 2022 at 06:39:56 UTC+1, Bonita Montero wrote: > Am 24.08.2022 um 19:00 schrieb JiiPee: > > > Is there any way to check if a raw pointer is pointing to a valid object? > What's the practical use of that ? > When you don't have good communications between the part of the program which is deleting an object, and a part which holds a reference to it. For example we have a plugin architecture. A plugin can't call another plugin's "update pointers" routine when it deletes a shared object. Fortunately the system provides an "is_valid" method, so you can guard against objects being deleted when it's your turn for the processor.
[toc] | [prev] | [next] | [standalone]
| From | Sam <sam@email-scan.com> |
|---|---|
| Date | 2022-08-25 09:37 -0400 |
| Subject | Re: Is it possible to test if a raw pointer is pointing to a valid object? |
| Message-ID | <cone.1661434629.973816.226663.1004@monster.email-scan.com> |
| In reply to | #86056 |
Malcolm McLean writes: > On Thursday, 25 August 2022 at 06:39:56 UTC+1, Bonita Montero wrote: > > Am 24.08.2022 um 19:00 schrieb JiiPee: > > > > > Is there any way to check if a raw pointer is pointing to a valid object? > > What's the practical use of that ? > > > When you don't have good communications between the part of the program > which is deleting an object, and a part which holds a reference to it. Well, that's the real problem that needs to be solved, and not the one about determining if a pointer is "valid", in some way, or not. That's because the latter is unsolvable in C++, so there aren't any other options.
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.c++
csiph-web