473,769 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Smart Pointers: Is there something similar to smart pointers in C?

Hi Experts,
I've just joined this group and want to know something:
Is there something similar to smart pointers in C or something to
prevent memory leakages in C programs.

Regards
MotoK

Sep 12 '06
59 5136
Martin Ambuhl wrote:
jacob navia wrote:
>C is for "macho" programmers that drink beer and
are just backwards.


More to the point, comp.lang.c is for the C programming language. It is
not, as Jacob imagines, here for him to advertise the virtues of the
non-C language his product supports.
>>
This is of course YOUR opinion. I beg to differ.


Of course, it is your own asinine statement with which you "beg to
differ." Ascribing it to others is only another instance of your
dishonesty. No one other than Jacob navia has asserted 'C if for
"macho" programmers that drink beer and are just backwards.' Jacob is
arguing with himself. Both Jacobs will, of course, lose.
Saying that somebody that uses a GC should not program in C is
this "macho" attitude, that thinks that avoiding tools,
relying on the programmer never making an error, is THE
right way to develop software.

This attitude (only use malloc/free) closes itself to
technical improvements of the run time environment
without any reason or argument, "just because I never do any
mistakes"...

jacob
Sep 12 '06 #11
Richard Heathfield wrote:
Rod Pemberton said:

>>"Bill Pursell" <bi**********@g mail.comwrote in message
news:11****** *************** *@i42g2000cwa.g ooglegroups.com ...
>>>If you are programming at a level where
you want a garbage collector, then you shouldn't be
programmin g in C. (My opinion.) The thing that takes
the place of a "smart pointer" in C is a "smart programmer".
You keep track of these things yourself.

Everyone here says that. I like that ideology and want to agree with
that.


Feel free.

>>But, apparently, it is a falsehood. If programmers were responsible, no
one here would be complaining about fgets and sprintf buffer overflows,


Wrong. The responsible programmers are the ones who know about these issues
(not that fgets is particularly vulnerable to buffer overflows as long as
you tell it the truth), and they can frequently be heard warning other
people about those issues, but they don't /complain/ about them. They write
their code defensively.
Yes of course, and they never make mistakes, never forget to free
a buffer and free it only once, never forget anything always right
like that programmer wonder Mr Dan Pop that asserted that he had
never had a crash ...

Well, I am not that kind of person. I DO make mistakes,
I find mind-numbing chores like keeping track of each and every
buffer in my programs a BORING and STUPID activitty that
can be much better done by the machine!!!
>
>>needing restrict pointers,


I've never seen a good justification for such a need.
Of course, easy of programming just doesn't arrive to your
consciousness as a REAL NEED
>
>>free not setting the pointer to NULL,


It would be nice, but it's no big deal.

>>ptrdiff_t's insufficient range,


Since I hardly ever use it, why should I care?
You never use ptrdiff_t, never forget anything when using free()
yes, YOU are perfect Heathfield, I am not.

>
>>undefined behavior for out-of-bounds pointers,


The solution to that is easy. Keep your pointers under control.
So easy said but so difficult to do in practice Heathfield.

Let's take a BUG of yours then:

In your book "C unleashed" you assume that sizeof(int) == sizeof(void *)
in page 352 and following. And you did not realize it till now maybe.

I have the 2000 edition, maybe you did get a new one but in any case
that BIG BUG is in there, without any disclaimer Heathfield.

Please, do not assume that you are perfect.
>
>>or any
other limitation, bug, or idiosyncratic feature of the C language.


Oh, come on - asking C programmers not to complain about C would be like
asking Formula 1 drivers not to complain about Formula 1 cars.
>>There'd
be no need for snprintf, strlcat, or garbage collection.


I have not yet seen a convincing argument that any of these is needed.
Certainly I've never needed them.
You do NOT need snprintf... nice. How do you do snprintf then?

(just curious)
<snip>
Sep 12 '06 #12
jacob navia said:

<snip>
Eliminating 100% of all malloc/free problems is extremely
difficult
Not for everybody.

<snip>
The GC is not a cure-all for all memory management problems, and its
use depends of the application, but it is an ERROR to
dismiss it at the start, simply because the C standard doesn't
mention it.
Nobody here is dismissing automatic garbage collection, and it is an error
for you to claim that they are. What is being dismissed is the claim that C
provides automatic garbage collection, because the claim is false.
The C standard mentions gets() asctime() and many other functions that
shouldn't be used at all.
True, but irrelevant.
This "backwards" view of the C language is promoted here
by some people, even if, as you point out, it has never worked
well.
This "backwards" view of comp.lang.c is promoted here by some people even
if, as I point out, it has never worked well.
I have another opinion.
Gosh.
My compiler system is deningrated here by those same people because
of its forward looking nature
No, your compiler system is not denigrated here. What is denigrated here is
your apparent inability to separate the idea of "C" from the idea of
"lcc-win32". The distinction is an important one.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 12 '06 #13
jacob navia said:
Richard Heathfield wrote:
>[...] The responsible programmers are the ones who know about these
issues (not that fgets is particularly vulnerable to buffer overflows as
long as you tell it the truth), and they can frequently be heard warning
other people about those issues, but they don't /complain/ about them.
They write their code defensively.

Yes of course, and they never make mistakes, never forget to free
a buffer and free it only once, never forget anything always right
like that programmer wonder Mr Dan Pop that asserted that he had
never had a crash ...
Every programmer makes mistakes. But, as mistakes go, mistakes involving
dynamic memory allocation are amongst the easier ones to fix.
Well, I am not that kind of person. I DO make mistakes,
So does everybody.
I find mind-numbing chores like keeping track of each and every
buffer in my programs a BORING and STUPID activitty that
can be much better done by the machine!!!
Then by all means use automatic garbage collection, if you wish. Your right
to do that is not in question. What is in question is your claim that C
provides it.
>>
>>>needing restrict pointers,

I've never seen a good justification for such a need.

Of course, easy of programming just doesn't arrive to your
consciousness as a REAL NEED
I find programming reasonably easy without restrict pointers. But I'm open
to persuasion. What value do they add, that will make my life easier?
>>>free not setting the pointer to NULL,

It would be nice, but it's no big deal.
>>>ptrdiff_t' s insufficient range,


Since I hardly ever use it, why should I care?

You never use ptrdiff_t,
Do you really not understand the difference between "hardly ever" and
"never"?
never forget anything when using free()
I have never claimed that. I do claim, however, that it's not a huge
problem.
yes, YOU are perfect Heathfield, I am not.
I have not claimed perfection, but it is gratifying to see that one person,
at least, thinks I am perfect.
>>>undefined behavior for out-of-bounds pointers,

The solution to that is easy. Keep your pointers under control.

So easy said but so difficult to do in practice Heathfield.
Why?
Let's take a BUG of yours then:
By all means. I'm always willing to learn.
In your book "C unleashed" you assume that sizeof(int) == sizeof(void *)
in page 352 and following. And you did not realize it till now maybe.
I'm looking at page 352. I can't see anywhere on that page where I assume
sizeof(int) == sizeof(void *). Please feel free to point it out.
I have the 2000 edition, maybe you did get a new one but in any case
that BIG BUG is in there, without any disclaimer Heathfield.
Which bug? I'm not seeing it. I'm not perfect, though, so perhaps I'm simply
overlooking it. Could you explain, please?
Please, do not assume that you are perfect.
I have never done so.

<snip>
>>>There'd
be no need for snprintf, strlcat, or garbage collection.

I have not yet seen a convincing argument that any of these is needed.
Certainly I've never needed them.

You do NOT need snprintf... nice.
Right.
How do you do snprintf then?
I don't. I don't need to. Didn't I just say that?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 12 '06 #14
jacob navia wrote:
Bill Pursell a écrit :
>jacob navia wrote:
>>MotoK a écrit :

I've just joined this group and want to know something:
Is there something similar to smart pointers in C or something to
prevent memory leakages in C programs.

There is something much better:
a garbage collector.


Which doesn't exist in standard C. lcc-win32 may provide
one, but it isn't standard C and it's generally a bad idea
to rely on a GC. If you are programming at a level where
you want a garbage collector, then you shouldn't be
programming in C. (My opinion.)

Yes of course!

C is for "macho" programmers that drink beer and
are just backwards.

This is of course YOUR opinion. I beg to differ.
He didn't claim it was "macho". He simply said that C is likely
the wrong tool if you start desiring a garbage collector for
your programs. I agree with that, and it has nothing to do with
what you claim.

Sep 12 '06 #15
Richard Heathfield wrote:
jacob navia said:
>Richard Heathfield wrote:
>>>
.... snip ...
>
>>>There'd be no need for snprintf, strlcat, or garbage collection.

I have not yet seen a convincing argument that any of these is
needed. Certainly I've never needed them.

You do NOT need snprintf... nice.

Right.
>How do you do snprintf then?

I don't. I don't need to. Didn't I just say that?
Once stdio provides putc and getc, nothing more is really needed
(apart from fopen, fclose, and the ilk). fflush is probably also
necessary. The rest is for convenience.

--
Some informative links:
news:news.annou nce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

--
Posted via a free Usenet account from http://www.teranews.com

Sep 12 '06 #16

jacob navia wrote:
Bill Pursell a écrit :
jacob navia wrote:
>MotoK a écrit :

I've just joined this group and want to know something:
Is there something similar to smart pointers in C or something to
prevent memory leakages in C programs.

There is something much better:
a garbage collector.

Which doesn't exist in standard C. lcc-win32 may provide
one, but it isn't standard C and it's generally a bad idea
to rely on a GC. If you are programming at a level where
you want a garbage collector, then you shouldn't be
programming in C. (My opinion.)

Yes of course!

C is for "macho" programmers that drink beer and
are just backwards.

This is of course YOUR opinion. I beg to differ.
The thing that takes
the place of a "smart pointer" in C is a "smart programmer".
You keep track of these things yourself.

Look dear, I use an automatic drive, and do not care about
passing gears when driving you see?

If we have computers is for THEM to do the mind numbing work,
not me. I do not try to outsmart a computer by
using MY brain to do THEIR work!
Hey, pretty soon you will be able to use the latest API call provided
in MFC :

void WinWriteMyProgr amForMe(void);

This cutting edge technical stuff is right up your alley.
>Using a garbage collector obviates the need for smart pointers,
constructors , destructors, weird language constructs, etc etc.

A garbage collector *is* a "weird language construct".

Excuse me but you are dead wrong:

prototype:

void *GC_malloc(size _t);

usage:

char *buffer = GC_malloc(BUFSI Z);
WHAT is a "weird language construct" in there ???

The garbage collector imposes absolutely no
new language changes at all. You just use GC_malloc
instead of malloc, forget about free and link with
the provided library.

All this is 100% standard C.
>The most popular garbage collector is Boehm's one. Some compilers like
lcc-win32 provide a GC in the standard distribution. Other compilers
support it, notably GCC, and it can be used in any situation.

I'm not aware of gcc support for a garbage collector for C. It
supports
garbage collection for objective-C, but I don't believe it provides
it for C.

The garbage collector is "language agnostic" and will work for C,
C++ or objective C in the same fashion.

--
Bill Pursell
Sep 12 '06 #17

jacob navia wrote:
>
Saying that somebody that uses a GC should not program in C is
this "macho" attitude, that thinks that avoiding tools,
relying on the programmer never making an error, is THE
right way to develop software.

This attitude (only use malloc/free) closes itself to
technical improvements of the run time environment
without any reason or argument, "just because I never do any
mistakes"...
Let me clarify my position. I don't think that someone
who uses a GC shouldn't program in C. I do think that
someone shouldn't rely on a garbage collector
for the functions that they write in C. I use a garbage
collector often. And I program in C. I just don't mix
the two.

When I'm writing at a level for which a garbage collector
is appropriate, I use a language which is appropriate, and
it's not C.

--
Bill Pursell

Sep 12 '06 #18
Bill Pursell wrote:
jacob navia wrote:

>>Saying that somebody that uses a GC should not program in C is
this "macho" attitude, that thinks that avoiding tools,
relying on the programmer never making an error, is THE
right way to develop software.

This attitude (only use malloc/free) closes itself to
technical improvements of the run time environment
without any reason or argument, "just because I never do any
mistakes".. .


Let me clarify my position. I don't think that someone
who uses a GC shouldn't program in C. I do think that
someone shouldn't rely on a garbage collector
for the functions that they write in C. I use a garbage
collector often. And I program in C. I just don't mix
the two.

When I'm writing at a level for which a garbage collector
is appropriate, I use a language which is appropriate, and
it's not C.

--
Bill Pursell
???
OK, your opinion.

I have written the debugger of lcc-win32 using the GC.
This has enormously simplified the debugger: I can allocate
a buffer anywhere in one of the threads of execution
and forget about it when I no longer need it.

A debugger needs to allocate displays build a lot of complicated
hierarchical data structures and throw them instantly away
when the program arrives somewhere else.

Using malloc/free this is an incredible difficult task. Each
buffer must be accounted for, each pointer that is cached somewhere must
be managed manually for validity, etc etc.

A GC allows you to forget about memory manager, and
concentrate in the task you are programming, in this case
the debugger.

The GC *simplifies* programming in C.

Using your logic I should have written a machine debugger in Java.

No.

C is the right language to write a C debugger. Using a GC it is
much easier, that's all.

Obviously you think that GC is for java/C#/lisp

Why not? I haven't anything against those languages. I am a different
opinion concerning the GC.
Sep 12 '06 #19

MotoK wrote:
Hi Experts,
I've just joined this group and want to know something:
Is there something similar to smart pointers in C or something to
prevent memory leakages in C programs.

Regards
MotoK
IMHO you can't do that in C. C gives you complete freedom to make
copies of pointers, do pointer arithmetic, pass the address of a
pointer, call arbitrary functions written in bizarre languages--- all
things that will screw up smart pointers and garbage collection to a
fare-thee-well, or at least a seg fault.

What I do is write a logging malloc() and free() so at the end of the
program it can print out "37122 unfreed blocks using 293455128 bytes".
And then a list of file names and lines where those blocks were
malloc'ed.

Sep 12 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2685
by: David B. Held | last post by:
I wanted to post this proposal on c.l.c++.m, but my news server apparently does not support that group any more. I propose a new class of exception safety known as the "smart guarantee". Essentially, the smart guarantee promises to clean up resources whose ownership is passed into the function, for whatever defintion of "clean up" is most appropriate for the resource passed. Note that this is different from both the basic and the...
4
2853
by: Matthias Kaeppler | last post by:
Hi, I'm having a hard time figuring out how I can initialize a smart pointer based on a certain condition: if something then ptr = 0; // init with NULL else ptr = new XYZ; // init with a sane value endif
92
5119
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers, but I just don't know. I don't like them because I don't trust them. I use new and delete on pure pointers instead. Do you use smart pointers?
14
18207
by: Ian | last post by:
I am looking at porting code from a C++ application to C#. This requires implementing data sharing functionality similar to what is provided by a smart pointer in C++. I have only recently begun to work in C# and am asking for suggestions/comments of how to implement a similar data sharing technique in C#. A C++ smart pointer can be used to share common information. For example, assume information managed by objects I1, I2, I3,...
33
5083
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
3
2855
by: mati-006 | last post by:
Hi, I think the code will be the best way to explain what I mean: #include "arglib/arg_shared.h" class base { public: base() {} virtual ~base() {} };
54
12019
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining the advantages of smart pointers endlessly (which are currently used in all our C++ software; we use the Boost smart pointers) as I'm seriously concerned that there is a shift to raw pointers. We are not developing system software but rather...
7
2130
by: sip.address | last post by:
I'm using reference counted smart pointers in a small project and it's a breeze, but I'm running into a problem which I don't know how to approach. Consider something like this: class A { public: A() {} void setObserver(counted_ptr<Bobserver) { _observer =
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.