Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

struct and classes always as a pointer and reference disappear...

64 views
Skip to first unread message

Rosario19

unread,
May 7, 2016, 1:33:23 PM5/7/16
to
basic types should be as now

signed
int32_t
int8_t
int16_t
etc
unsigned
types for 32 8 16 bit etc

and should be values
so
int32_t a;
and a is a value as now


but structs and classes have to be address for example

clasA w;

than w is a pointer to the obj defined from the clasA

[if that obj has not memory for to be create from the system
than this if print no mem for w
if(w==0) print("No Mem for w");
]

this can be ok in using of operators as in

a=b+c;

so there is no reason for the reference....

Rick C. Hodgin

unread,
May 7, 2016, 3:23:43 PM5/7/16
to
On Saturday, May 7, 2016 at 1:33:23 PM UTC-4, Rosario19 wrote:
> but structs and classes have to be address for example
>
> clasA w;
>
> than w is a pointer to the obj defined from the clasA
>
> [if that obj has not memory for to be create from the system
> than this if print no mem for w
> if(w==0) print("No Mem for w");
> ]

That's the difference between using pointers and native objects. With
a pointer there is an allocation step, and it does introduce the potential
for a failed allocation. When you use a native object, it is already
allocated by the time you are able to use it in your program.

By reference variables give you the same ability. By the time you're
able to use them, you know they are valid because the error would've
already occurred before you were able to use them in your line of source
code.

> this can be ok in using of operators as in
>
> a=b+c;
>
> so there is no reason for the reference....

What does this mean (a=b+c) when a, b, or c are something other than
a fundamental data type? In that case their use requires the class
definition to make it have contextual sense. And as such, the needs of
that mechanism at runtime require that a reference to the object be
available for the context, otherwise they'd be converted to pointers
which does introduce the possibility of a NULL and does then require
the extra protection against NULL pointers, which is one of the things
the use of references was created to avoid.

-----
There are cases where you know you have valid data. There's no reason
to then use it as a pointer because it makes the syntax more complex,
and it requires that you consistently guard against NULL values in the
pointer. With references, all of that disappears and your code is both
cleaner, and faster, because you can remove the requisite NULL checks.

Best regards,
Rick C. Hodgin

Rosario19

unread,
May 7, 2016, 3:59:49 PM5/7/16
to
On Sat, 7 May 2016 12:23:30 -0700 (PDT), "Rick C. Hodgin" wrote:
>On Saturday, May 7, 2016 at 1:33:23 PM UTC-4, Rosario19 wrote:
>> but structs and classes have to be address for example
>>
>> clasA w;
>>
>> than w is a pointer to the obj defined from the clasA
>>
>> [if that obj has not memory for to be create from the system
>> than this if print no mem for w
>> if(w==0) print("No Mem for w");
>> ]
>
>That's the difference between using pointers and native objects. With
>a pointer there is an allocation step,

allocation step is done from C++ constructor
basically the constructor has one arg &w
and fill it with the address right [if memory exist for that] or 0 in
the other case.

>and it does introduce the potential
>for a failed allocation.

memory infinite not exist but with this the program can see if obj is
0 [memory error]
if it is 0 the program can run and report the error without segfault

>When you use a native object, it is already
>allocated by the time you are able to use it in your program.
>
>By reference variables give you the same ability. By the time you're
>able to use them, you know they are valid because the error would've
>already occurred before you were able to use them in your line of source
>code.
>
>> this can be ok in using of operators as in
>>
>> a=b+c;
>>
>> so there is no reason for the reference....
>
>What does this mean (a=b+c) when a, b, or c are something other than
>a fundamental data type?

b is address where b is
c is address where c is
doing b+c assign the result to t
copy t to a
free t

Rosario19

unread,
May 7, 2016, 4:11:06 PM5/7/16
to
On Sat, 7 May 2016 12:23:30 -0700 (PDT), "Rick C. Hodgin" wrote:

>There are cases where you know you have valid data.

0,1%

>There's no reason
>to then use it as a pointer because it makes the syntax more complex,

if it would be for me all would be a pointer

>and it requires that you consistently guard against NULL values in the
>pointer.

only in the start of the function or operator

>With references, all of that disappears

but the finite memory problem remain

>and your code is both
>cleaner, and faster,

i prefer the checks

Rick C. Hodgin

unread,
May 7, 2016, 4:15:34 PM5/7/16
to
On Saturday, May 7, 2016 at 3:59:49 PM UTC-4, Rosario19 wrote:
> On Sat, 7 May 2016 12:23:30 -0700 (PDT), "Rick C. Hodgin" wrote:
> >On Saturday, May 7, 2016 at 1:33:23 PM UTC-4, Rosario19 wrote:
> >> but structs and classes have to be address for example
> >>
> >> clasA w;
> >>
> >> than w is a pointer to the obj defined from the clasA
> >>
> >> [if that obj has not memory for to be create from the system
> >> than this if print no mem for w
> >> if(w==0) print("No Mem for w");
> >> ]
> >
> >That's the difference between using pointers and native objects. With
> >a pointer there is an allocation step,
>
> allocation step is done from C++ constructor
> basically the constructor has one arg &w
> and fill it with the address right [if memory exist for that] or 0 in
> the other case.

That's true of pointers to class objects, but not of objects which are
on the stack. They occupy local stack space and physically exist.
There is no allocation step, however their constructors are called,
and their destructors also upon exit.

> >and it does introduce the potential
> >for a failed allocation.
>
> memory infinite not exist but with this the program can see if obj is
> 0 [memory error]
> if it is 0 the program can run and report the error without segfault

That's the point, with references you do don't need to. IF you are
on the line of source code where you are using it, it _already_ exists
and _is not_ NULL.

That's one of their features.

> >When you use a native object, it is already
> >allocated by the time you are able to use it in your program.
> >
> >By reference variables give you the same ability. By the time you're
> >able to use them, you know they are valid because the error would've
> >already occurred before you were able to use them in your line of source
> >code.
> >
> >> this can be ok in using of operators as in
> >>
> >> a=b+c;
> >>
> >> so there is no reason for the reference....
> >
> >What does this mean (a=b+c) when a, b, or c are something other than
> >a fundamental data type?
>
> b is address where b is
> c is address where c is
> doing b+c assign the result to t
> copy t to a
> free t

Not necessarily. Operator overrides change the meaning to custom
applications, and it doesn't have to mean anything like that, though
it typically might.

References are then used here for one or both sides of each operator.
Internally, the compiler will issue steps to handle certain things
based on operations, but it doesn't have to be that way. It depends
on what code exists and is in use.

Paavo Helde

unread,
May 7, 2016, 4:17:48 PM5/7/16
to
On 7.05.2016 22:23, Rick C. Hodgin wrote:
> On Saturday, May 7, 2016 at 1:33:23 PM UTC-4, Rosario19 wrote:
>> but structs and classes have to be address for example
>>
>> clasA w;
>>
>> than w is a pointer to the obj defined from the clasA

>> [if that obj has not memory for to be create from the system
>> than this if print no mem for w
>> if(w==0) print("No Mem for w");
>> ]
>
> That's the difference between using pointers and native objects. With
> a pointer there is an allocation step, and it does introduce the potential
> for a failed allocation. When you use a native object, it is already
> allocated by the time you are able to use it in your program.

Rosario19 did not have any pointers or allocations, so talking about
NULL pointers or failed allocations was not appropriate.

BTW, pointers are native data types in C/C++. What you are talking about
is the difference between dynamically and automatically allocated
storage. This is orthogonal to the "native types" concept, you can
easily have

int* p = new int(42);

for example, where you have an object of native type (int) allocated
dynamically.

Best regards
Paavo

(PS. I never reply to Rosario19 directly because he (or she or it) is
apparently not capable to keep up a coherent conversation.)


Rick C. Hodgin

unread,
May 7, 2016, 4:25:11 PM5/7/16
to
On Saturday, May 7, 2016 at 4:17:48 PM UTC-4, Paavo Helde wrote:
> On 7.05.2016 22:23, Rick C. Hodgin wrote:
> > On Saturday, May 7, 2016 at 1:33:23 PM UTC-4, Rosario19 wrote:
> >> but structs and classes have to be address for example
> >>
> >> clasA w;
> >>
> >> than w is a pointer to the obj defined from the clasA
>
> >> [if that obj has not memory for to be create from the system
> >> than this if print no mem for w
> >> if(w==0) print("No Mem for w");
> >> ]
> >
> > That's the difference between using pointers and native objects. With
> > a pointer there is an allocation step, and it does introduce the potential
> > for a failed allocation. When you use a native object, it is already
> > allocated by the time you are able to use it in your program.
>
> Rosario19 did not have any pointers or allocations, so talking about
> NULL pointers or failed allocations was not appropriate.

Look at Rosario19's code. It's why I mentioned it.

> BTW, pointers are native data types in C/C++. What you are talking about
> is the difference between dynamically and automatically allocated
> storage. This is orthogonal to the "native types" concept, you can
> easily have

No. I mention that pointers are native types, but that the things
they point to are not always guaranteed to exist, however, the pointer
itself will always exist and can be tested for NULL, for example.

> int* p = new int(42);
>
> for example, where you have an object of native type (int) allocated
> dynamically.

I stated something similar in previous post.

> Best regards
> Paavo
>
> (PS. I never reply to Rosario19 directly because he (or she or it) is
> apparently not capable to keep up a coherent conversation.)

Rosario19

unread,
May 7, 2016, 4:27:57 PM5/7/16
to
On Sat, 7 May 2016 13:15:22 -0700 (PDT), "Rick C. Hodgin" wrote:
>On Saturday, May 7, 2016 at 3:59:49 PM UTC-4, Rosario19 wrote:
>> On Sat, 7 May 2016 12:23:30 -0700 (PDT), "Rick C. Hodgin" wrote:
>> >On Saturday, May 7, 2016 at 1:33:23 PM UTC-4, Rosario19 wrote:
>> >> but structs and classes have to be address for example
>> >>
>> >> clasA w;
>> >>
>> >> than w is a pointer to the obj defined from the clasA
>> >>
>> >> [if that obj has not memory for to be create from the system
>> >> than this if print no mem for w
>> >> if(w==0) print("No Mem for w");
>> >> ]
>> >
>> >That's the difference between using pointers and native objects. With
>> >a pointer there is an allocation step,
>>
>> allocation step is done from C++ constructor
>> basically the constructor has one arg &w
>> and fill it with the address right [if memory exist for that] or 0 in
>> the other case.
>
>That's true of pointers to class objects, but not of objects which are
>on the stack. They occupy local stack space and physically exist.
>There is no allocation step, however their constructors are called,
>and their destructors also upon exit.

i speak the impossible case (for C++ because they define in one other
way)
ClassName a;
a is a pointer to the object so one
sizeof(void*) or one size of pointer

the obj "a" is in the memory returned from malloc() [or one stack
allocator]


for example in
ClassName a;

a==0xFF00FF and from

0xFF00FF to 0xFF00FF+sizeof(obj)
there is the "a" obj

so ((u8*)a)[0..sizeof(obj)] is where the obj is...

>> >and it does introduce the potential
>> >for a failed allocation.
>>
>> memory infinite not exist but with this the program can see if obj is
>> 0 [memory error]
>> if it is 0 the program can run and report the error without segfault
>
>That's the point, with references you do don't need to. IF you are
>on the line of source code where you are using it, it _already_ exists
>and _is not_ NULL.

and if it is NULL the sys what do? it is the programmer that says to
the sys what to do?

Rick C. Hodgin

unread,
May 7, 2016, 4:35:22 PM5/7/16
to
No. The syntax "ClassName a;" is a real object that exists on the
stack. You would have to use "ClassName* a = new ClassName();" to
have a pointer which may or may not exist.

> the obj "a" is in the memory returned from malloc() [or one stack
> allocator]

Only in the case of pointers. When you do not use the "ClassName*"
declarator, it is just like if you had used "int a" in that it always
exists because it uses local stack space memory, but unlike "int a;"
the use of "ClassName a;" will cause the constructor to be called
before any code in the method/function, and the destructor will be
called when it exits.

> for example in
> ClassName a;
>
> a==0xFF00FF and from
>
> 0xFF00FF to 0xFF00FF+sizeof(obj)
> there is the "a" obj
>
> so ((u8*)a)[0..sizeof(obj)] is where the obj is...

Only in "ClassName* a = new ClassName();" does that happen. In the
case of just "ClassName a;" then it exists from &a to for sizeof(a)
bytes, which will be somewhere on the stack on most systems.

> >> >and it does introduce the potential
> >> >for a failed allocation.
> >>
> >> memory infinite not exist but with this the program can see if obj is
> >> 0 [memory error]
> >> if it is 0 the program can run and report the error without segfault
> >
> >That's the point, with references you do don't need to. IF you are
> >on the line of source code where you are using it, it _already_ exists
> >and _is not_ NULL.
>
> and if it is NULL the sys what do? it is the programmer that says to
> the sys what to do?

That my point: it won't ever be NULL if you don't use a pointer, or if
you pass a properly allocated value by its value by reference.

That's why references are so nice. Also because they simplify usage
syntax in called functions, while still maintaining the basic mechanics
of that which you would see were you using a pointer instead.

> >That's one of their features.

Rosario19

unread,
May 7, 2016, 4:35:31 PM5/7/16
to
On Sat, 07 May 2016 23:17:31 +0300, Paavo Helde wrote:

>int* p = new int(42);
>
>for example, where you have an object of native type (int) allocated
>dynamically.

and if the system has no memory what return "new"?
i think return 0 to p...

Rosario19

unread,
May 7, 2016, 4:58:30 PM5/7/16
to
On Sat, 07 May 2016 22:27:49 +0200, Rosario19 wrote:

>i speak the impossible case (for C++ because they define in one other
>way)
>ClassName a;
>a is a pointer to the object so one
>sizeof(void*) or one size of pointer
>
>the obj "a" is in the memory returned from malloc() [or one stack
>allocator]


thru its constructor [that call malloc() or new[] ] as in some C++
constructor

Rosario19

unread,
May 7, 2016, 5:00:09 PM5/7/16
to
On Sat, 07 May 2016 22:58:22 +0200, Rosario19 wrote:

>>so ((u8*)a)[0..sizeof(obj)] is where the obj is...

((u8*)a)[0..sizeof(obj)-1]

Rosario19

unread,
May 7, 2016, 5:01:24 PM5/7/16
to
On Sat, 07 May 2016 23:17:31 +0300, Paavo Helde wrote:

>(PS. I never reply to Rosario19 directly because he (or she or it) is
>apparently not capable to keep up a coherent conversation.)

prrrrrrrrrrrrrrrrrrrr

Rick C. Hodgin

unread,
May 7, 2016, 5:34:25 PM5/7/16
to
On Saturday, May 7, 2016 at 4:58:30 PM UTC-4, Rosario19 wrote:
> On Sat, 07 May 2016 22:27:49 +0200, Rosario19 wrote:
>
> >i speak the impossible case (for C++ because they define in one other
> >way)
> >ClassName a;
> >a is a pointer to the object so one
> >sizeof(void*) or one size of pointer
> >
> >the obj "a" is in the memory returned from malloc() [or one stack
> >allocator]
>
> thru its constructor [that call malloc() or new[] ] as in some C++
> constructor

We can test it. Create this, compile it in your compiler and have it
either generate output assembly, or single-step through the disassembly
at runtime, and you will see where the data for a is allocated:

#include <stdio.h>

class CMyClass
{
public:
CMyClass(int a) { m_a = a; }
~CMyClass() { }
void output(void) { printf("%d\n", m_a); }

private:
int m_a;
};

int main(int argc, char **argv)
{
CMyClass w(2);
w.output();
}

And you see what code is called on the CMyClass w(2); line. You'll see
that it uses allocation on the stack and never calls malloc() or its
equivalents.

> >for example in
> >ClassName a;
> >
> >a==0xFF00FF and from
> >
> >0xFF00FF to 0xFF00FF+sizeof(obj)
> >there is the "a" obj
> >
> >so ((u8*)a)[0..sizeof(obj)] is where the obj is...

The a in "ClassName a;" will reside at (u8*)&a[0..sizeof(a)-1].

Rosario19

unread,
May 7, 2016, 6:01:50 PM5/7/16
to
i already know above in the stack there is here [if i remember well ]
one int in above case...

if the class has in it {.... int m_a,m_b,m_c;}
the class would have 3 int int in the stack etc

but i say somethig as this
#include <stdio.h>

class CMyClass
{public:
CMyClass(int a) {// this is the address of w in the stack
*this=malloc(sizeof(class CMyClass));
// if to this above is assigned the memory
// "int m_a;" begin to exist... so m_a=a
// should be ok
if(*this) m_a=a;
}
~CMyClass() { free(*this); }
void output(void) { printf("%d\n", m_a); }
int m_a;
};

int main(int argc, char **argv)
{
CMyClass w(2);
w.output();
}

but this code not exist nor compile etc

Rick C. Hodgin

unread,
May 7, 2016, 6:32:40 PM5/7/16
to
If you were only using allocated classes it would work. But the existing
class structure allows class data to exist in the global heap, or in local
stack space, so your proposal won't work because those resources don't
need to be free()'d in the same way.

Mike Stump

unread,
May 26, 2016, 11:30:13 PM5/26/16
to
In article <7aksib9t25vto4al8...@4ax.com>,
It does not, nor, can it and call itself C++.

Öö Tiib

unread,
May 27, 2016, 3:05:06 AM5/27/16
to
That is correct. May be worth to note that after C++11 we have special
non-throwing new:

int* p2 = new (std::nothrow) int(42);

It will return 0 if system does not have memory for int, since it may
not throw 'bad_alloc' like usual new does.


Alf P. Steinbach

unread,
May 27, 2016, 3:18:12 AM5/27/16
to
On 27.05.2016 09:04, Öö Tiib wrote:
>
> May be worth to note that after C++11 we have special
> non-throwing new:
>
> int* p2 = new (std::nothrow) int(42);
>
> It will return 0 if system does not have memory for int, since it may
> not throw 'bad_alloc' like usual new does.

`std::nothrow` was introduced in C++98, the first C++ standard.


Cheers & hth.,

- Alf


Öö Tiib

unread,
May 27, 2016, 3:56:15 AM5/27/16
to
Thanks for correcting.

Mike Stump

unread,
May 28, 2016, 9:45:11 PM5/28/16
to
In article <495c2472-67ba-4bcf...@googlegroups.com>,
ร รถ Tiib <oot...@hot.ee> wrote:
>> >and if the system has no memory what return "new"?
>> >i think return 0 to p...
>>
>> It does not, nor, can it and call itself C++.
>
>That is correct. May be worth to note that after C++11 we have special
>non-throwing new

Wow, you must be new around here. We have a special non-throwing new
in C++98 as well. :-) Sorry, could not resist.

Öö Tiib

unread,
May 29, 2016, 4:21:14 AM5/29/16
to
On Sunday, 29 May 2016 04:45:11 UTC+3, Mike Stump wrote:
> In article <495c2472-67ba-4bcf...@googlegroups.com>,
> Öö Tiib <oot...@hot.ee> wrote:
> >> >and if the system has no memory what return "new"?
> >> >i think return 0 to p...
> >>
> >> It does not, nor, can it and call itself C++.
> >
> >That is correct. May be worth to note that after C++11 we have special
> >non-throwing new
>
> Wow, you must be new around here. We have a special non-throwing new
> in C++98 as well. :-) Sorry, could not resist.

No problem. :-)

I have typed 'new' into C++ code on very limited occasions lately.
That "lately" has lasted for more than decade. Same is with raw pointers.
Naked 'new' feels like dangerous half-operation and raw pointer
like unprotected memory address. Code with lot of 'new's looks like
written in some low level language ... possibly Java or C#.

Downside of it is that apparently I start to forget trivia about 'new'.

Rosario19

unread,
May 29, 2016, 5:37:02 PM5/29/16
to
On Sun, 29 May 2016 01:20:57 -0700 (PDT), 嘱 Tiib wrote:
>On Sunday, 29 May 2016 04:45:11 UTC+3, Mike Stump wrote:
>> In article <495c2472-67ba-4bcf...@googlegroups.com>,
>> 嘱 Tiib <oot...@hot.ee> wrote:
>> >> >and if the system has no memory what return "new"?
>> >> >i think return 0 to p...

the just above line is my
even if it is wrong... etc

Öö Tiib

unread,
May 29, 2016, 7:03:52 PM5/29/16
to
On Monday, 30 May 2016 00:37:02 UTC+3, Rosario19 wrote:
> On Sun, 29 May 2016 01:20:57 -0700 (PDT), Öö Tiib wrote:
> >On Sunday, 29 May 2016 04:45:11 UTC+3, Mike Stump wrote:
> >> In article <495c2472-67ba-4bcf...@googlegroups.com>,
> >> Öö Tiib <oot...@hot.ee> wrote:
> >> >> >and if the system has no memory what return "new"?
> >> >> >i think return 0 to p...
>
> the just above line is my
> even if it is wrong... etc

Yes I know, it was Mike Stump who cut attributions, I just replied to
his post (and did not bother to restore the attributions he cut).
0 new messages