473,890 Members | 1,430 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 5161
Ancient_Hacker wrote:
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.
You are wrong what GC is concerned.
Normal C programs do not need a lot of care to use the GC,
and unless you explicitely hide the pointers from the GC
garbage collection will work perfectly with C.
Sep 12 '06 #21
jacob navia <ja***@jacob.re mcomp.frwrites:
[...]
You are wrong what GC is concerned.
Normal C programs do not need a lot of care to use the GC,
and unless you explicitely
.... or implicitly ...
hide the pointers from the GC
garbage collection will work perfectly with C.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 12 '06 #22
On Tue, 12 Sep 2006 08:52:06 +0200, in comp.lang.c , jacob navia
<ja***@jacob.re mcomp.frwrote:
>Bill Pursell a écrit :
>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.)

C is for "macho" programmers that drink beer and
are just backwards.
Thats not what he said, and as usual you post a silly reply which
serves no purpose except to damage the rest of your post and set
people against you.
>This is of course YOUR opinion. I beg to differ.
You could have posted this bit alone, and sounded rational instead of
stupid.
>Look dear, I use an automatic drive, and do not care about
passing gears when driving you see?
Statistically, you're 14% more likely to have an accident in bad
weather in an automatic*.
>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.
I sincerely doubt this. Gathering garbage created by programmers
forgetting to delete objects or call destructors is somewhat different
to that created by programmers forgetting to call free, or pointing
pointers around randomly.

*or so a bloke in the pub told me. He's probably as reliable as
anyone.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Sep 12 '06 #23
Mark McIntyre wrote:
<ja***@jacob.re mcomp.frwrote:
.... snip ...
>
>Look dear, I use an automatic drive, and do not care about
passing gears when driving you see?

Statistically, you're 14% more likely to have an accident in bad
weather in an automatic*.
The next time you run out of fuel and stall on a railroad crossing,
and want to walk the car out of the way of the train with the
starter motor, please describe how you do that when equipped with a
slush box.

--
"I was born lazy. I am no lazier now than I was forty years
ago, but that is because I reached the limit forty years ago.
You can't go beyond possibility." -- Mark Twain

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

Sep 13 '06 #24
CBFalconer wrote:
Mark McIntyre wrote:
>><ja***@jacob. remcomp.frwrote :

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

Statistically , you're 14% more likely to have an accident in bad
weather in an automatic*.


The next time you run out of fuel and stall on a railroad crossing,
and want to walk the car out of the way of the train with the
starter motor, please describe how you do that when equipped with a
slush box.
slush box?

--
Ian Collins.
Sep 13 '06 #25
CBFalconer said:
Mark McIntyre wrote:
><ja***@jacob.r emcomp.frwrote:
... snip ...
>>
>>Look dear, I use an automatic drive, and do not care about
passing gears when driving you see?

Statisticall y, you're 14% more likely to have an accident in bad
weather in an automatic*.

The next time you run out of fuel and stall on a railroad crossing,
Having driven for a great many years, I am pleased to report that I have
never, ever run out of fuel. This is not mere luck. It is the result of
forethought.

Having driven for a great many years, I am pleased to report that I have
never, ever stalled on a level crossing. This is not mere luck. It is the
result of forethought.

The chance of my doing either of these is vanishingly small, and the chance
of doing both simultaneously is epsilon squared.

On the other hand, you wouldn't catch me driving an automatic, at least not
through choice.

--
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 13 '06 #26
Hello All,
Well i got some inputs like GC and logging utility.Thanks to all.

Sep 13 '06 #27
MotoK wrote:
Hello All,
Well i got some inputs like GC and logging utility.Thanks to all.
You are off topic. This is a thread about "slush boxes".

Sep 13 '06 #28
Richard Heathfield wrote:
>
On the other hand, you wouldn't catch me driving an automatic, at least not
through choice.
Ahhh OK. I understand now why you are against the GC.

I drive an automatic, relaxed, no effort, the machine works for
me, not the other way around...

jacob
Sep 13 '06 #29
jacob navia said:
Richard Heathfield wrote:
>>
On the other hand, you wouldn't catch me driving an automatic, at least
not through choice.

Ahhh OK. I understand now why you are against the GC.
What makes you think I'm against automatic garbage collection? What I'm
against is the claim that C provides it.
I drive an automatic, relaxed, no effort,
There's nothing particularly arduous about using a manual gearbox, and you
have more control over the machine. There's nothing particularly arduous
about managing memory, either. But if you don't want to do it, fine, use
automatic garbage collection by all means. I don't have a problem with
that. But C does not provide automatic garbage collection.

--
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 13 '06 #30

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

Similar topics

14
2692
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
2863
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
5163
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
18226
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
5105
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
2861
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
12055
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
2135
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
9826
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
11234
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
10829
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...
1
10925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7171
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
3282
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.