473,761 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A question about pointer

As long as I write c++ code, I am worry about the
pointer. The more the system is, the dangerous the pointer
is I think.
I must pass pointers erverywhere, and is there a way to
ensure that every object pointered by any pointer will
be deleted and only be deleted once?
Dec 12 '06 #1
21 1848
Bo Yang wrote:
As long as I write c++ code, I am worry about the
pointer. The more the system is, the dangerous the pointer
is I think.
I must pass pointers erverywhere, and is there a way to
ensure that every object pointered by any pointer will
be deleted and only be deleted once?
No. With freedom comes responsibility, but to reduce the risk of leaks,
you can avoid using raw pointers and arrays in favor RAII techniques:
never allocate a resource (e.g., memory, etc.) without assigning it to
an object that will automatically release it. One class that enables
such techniques is std::tr1::share d_ptr, and another is std::vector
(cf. http://parashift.com/c++-faq-lite/co...html#faq-34.1).

Cheers! --M

Dec 12 '06 #2

Bo Yang wrote:
As long as I write c++ code, I am worry about the
pointer. The more the system is, the dangerous the pointer
is I think.
I must pass pointers erverywhere, and is there a way to
ensure that every object pointered by any pointer will
be deleted and only be deleted once?
Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs and no
aspirins.
Replace any allocation with the appropriate smart pointer and use STL
containers (which support copy semantics).
Replace pointers with references, prefereably - const references where
appropriate.

#include <iostream>
#include <ostream>
#include <vector>
#include <iterator>
#include <boost/shared_ptr.hpp>

template< typename T >
class A {
T t;
public:
// ctor
A(const T& val = T()) : t(val)
{
std::cout << "A()\n";
}
// copy ctor
A(const A& copy)
{
std::cout << "copy A\n";
t = copy.t;
}
// d~tor
~A() { std::cout << "~A()\n"; }
// friend op<<
friend std::ostream&
operator<<(std: :ostream& os, const A& r_a)
{
return os << r_a.t;
}
};

int main() {
{
std::vector< A< double vd(5, 11.1); // 5 elements
std::copy( vd.begin(),
vd.end(),
std::ostream_it erator< A< double (std::cout, "\n") );
}

std::cout << "allocating using shred_ptr...\n" ;
boost::shared_p tr< A< std::string sp_a(new A< std::string >("a
short string"));
std::cout << *sp_a << std::endl;

return 0;
}

/*
A() < -temp
copy A
copy A
copy A
copy A
copy A
~A() <- temp destruction
11.1
11.1
11.1
11.1
11.1
~A() <- 5 automatic destructors
~A()
~A()
~A()
~A()
allocating using shred_ptr...
A()
a short string
~A() <- automatic
*/

And if you have any doubt about the strategy, change
std::vector< A< double vd(5, 11.1); // 5 elements
to
std::vector< A< double vd(1000000); // one million

For the sake of comparison: the equivalent of a const reference is a
const pointer to a const value.
int n;
const r_n&(n);
const p_n* const(&n);

Using pointers is iffy enough, using non-const pointers can be
downright dangerous where these should be immutable.

Dec 12 '06 #3

Bo Yang skrev:
As long as I write c++ code, I am worry about the
pointer. The more the system is, the dangerous the pointer
is I think.
I must pass pointers erverywhere, and is there a way to
ensure that every object pointered by any pointer will
be deleted and only be deleted once?
Why must you pass pointers everywhere? A proper C++ program rarely
needs raw pointers, and memory leaks/invalid pointer access is not a
serious problem in correctly written code.
If you so desire, you can install a garbage collector. In that case you
do not have to maintain memory ressources. But remember that other
resources still must be maintained. I never have had a need to use a
garbage collector, as smart pointers such as those provided by boost,
but I other types of applications might have other needs.

/Peter

Dec 12 '06 #4

"Salt_Peter " <pj*****@yahoo. comwrote in message
news:11******** **************@ 80g2000cwy.goog legroups.com...
>
>
Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs and no
aspirins.
Hmm... no pointers means no bugs, eh? Interesting...

int main()
{
int a[1];
a[1] = 1;
}

No bugs? :-)

-Howard
Dec 12 '06 #5

Howard wrote:
"Salt_Peter " <pj*****@yahoo. comwrote in message
news:11******** **************@ 80g2000cwy.goog legroups.com...

Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs and no
aspirins.

Hmm... no pointers means no bugs, eh? Interesting...

int main()
{
int a[1];
a[1] = 1;
}

No bugs? :-)

-Howard
Aren't arrays really just pointers anyway?

Dec 12 '06 #6
As long as I write c++ code, I am worry about the
pointer. The more the system is, the dangerous the pointer
is I think.
I must pass pointers erverywhere, and is there a way to
ensure that every object pointered by any pointer will
be deleted and only be deleted once?
Bo Yang:

It's a sutil subject. I think there are no magic solutions here. After
~15 years of C/C++ programming, pointers are the minor concern to me.
Perhaps the advise would be to read a couple of good books to learn
good programming techniques and gain experience coding from scratch.

Hope this help.

Dec 12 '06 #7
Th*******@gmail .com wrote:
>
Howard wrote:
"Salt_Peter " <pj*****@yahoo. comwrote in message
news:11******** **************@ 80g2000cwy.goog legroups.com...
>
>
Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs
and no aspirins.
Hmm... no pointers means no bugs, eh? Interesting...

int main()
{
int a[1];
a[1] = 1;
}

No bugs? :-)
Aren't arrays really just pointers anyway?
No.
Brian

Dec 12 '06 #8

Howard wrote:
"Salt_Peter " <pj*****@yahoo. comwrote in message
news:11******** **************@ 80g2000cwy.goog legroups.com...

Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs and no
aspirins.

Hmm... no pointers means no bugs, eh? Interesting...

int main()
{
int a[1];
a[1] = 1;
}

No bugs? :-)

-Howard
Thanks for the perfect example: in the above - a - decays to a pointer.
Try:

std::vector< int v(1);
v.at(1) = 1; // std::range_erro r thrown

Dec 12 '06 #9
"Salt_Peter " <pj*****@yahoo. comwrote in
news:11******** **************@ 73g2000cwn.goog legroups.com:
>
Howard wrote:
>"Salt_Peter " <pj*****@yahoo. comwrote in message
news:11******* *************** @80g2000cwy.goo glegroups.com.. .
>
>
Your goal is to write an entire program without using a pointer
directly. Not even one.
Note that references do just fine if polymorphism is required.
Its easier than you think. Since no naked pointers means no bugs
and no aspirins.

Hmm... no pointers means no bugs, eh? Interesting...

int main()
{
int a[1];
a[1] = 1;
}

No bugs? :-)

-Howard

Thanks for the perfect example: in the above - a - decays to a
pointer. Try:

std::vector< int v(1);
v.at(1) = 1; // std::range_erro r thrown
How about v[1]?
Dec 12 '06 #10

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

Similar topics

2
3056
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers to have an easier time understanding what I do. Therefore this weekend I'm going to spend 3 days just writing comments. Before I do it, I thought I'd ask other programmers what information they find useful. Below is a typical class I've...
11
25768
by: Dmitry D | last post by:
Hi, I'm new to C++ (started learning in the beginning of this summer), and I have the following question (sorry if it sounds stupid): In many code samples and source files, I see NULL expression being used (for example, int* someInt=NULL; ). I used similar initialization myself, and it works fine even if I don't define NULL. But what is "NULL" exactly? Is it a constant defined by compiler? Is there any difference between the following two...
9
1522
by: wongjoekmeu | last post by:
Hello All, I am learning C++ at the moment. Going through the book of SAM of learning C++ in 21 days I have learned about pointers that it is good custome to always initialise them and to use delete before you try to give it a new address. Can anyone explain to me why this code below which seems to voilate this rule works ? ------------------- int main()
7
2216
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ http://www.eskimo.com/~scs/C-faq/faq.html but it doesn't seem to answer my questions... So, I've made an example behind, with some included questions...
7
2489
by: sunglo | last post by:
My doubt comes from trying to understand how thread return values work (I know, it's off topic here), and I'm wondering about the meaning of the "void **" parameter that pthread_join expects (I think this is topical, since it's a C question, but please correct me and apologies if I'm wrong). I suppose that it's this way to allow for "generic" pointer modification, but this implies dereferencing the "void **" pointer to get a "void *"...
7
2195
by: Yuri_Юрий | last post by:
I'm confused about the VARIABLE LENGTH ARRAYS. {scanf("%d",&n);float a;} In which compiler can I use it? I tried VC++6.0 SP6,but it's reported error:CONSTANT EXPRESSION! Another question, What the differences between: (1)ElemType array (2)array=(ElemType*)malloc(N*size of(ElemType)) or array=(ElemType*)calloc(N,size of(ElemType))
14
2415
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I think I need some "template". Sorry for my coding experience in c++
68
15705
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting; some may find it off-topic or confusing. Disclaimers at end. The code samples are intended to be nearly minimal demonstrations. They are *not* related to any actual application code.
4
1741
by: Deep | last post by:
I'm in doubt about what is smart pointer. so, please give me simple description about smart pointer and an example of that. I'm just novice in c++. regards, John.
17
2395
by: DiAvOl | last post by:
Hello everyone, merry christmas! I have some questions about the following program: arrtest.c ------------ #include <stdio.h> int main(int agc, char *argv) {
0
9554
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
9377
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,...
1
9925
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,...
1
7358
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
5266
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
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3913
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
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
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.