473,770 Members | 2,781 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
21 1851

Default User wrote:
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
Oh yes, they most definitely are, through and through. Case in point:
the code above.
And the undefined behaviour that follows.

#include <iostream>
#include <ostream>

int main()
{
int array[1];
std::cout << "array = " << array << std::endl;
int* p = array;
std::cout << "p = " << p << std::endl;

array[1] = 1;
std::cout << "array + 1 = " << array + 1 << std::endl;
std::cout << "array[1] = " << array[1] << std::endl;
std::cout << "p + 1 is " << p + 1 << std::endl;
std::cout << "*(p + 1) = " << *(p + 1) << std::endl;

array[100] = 1;
std::cout << "array + 100 = " << array + 100 << std::endl;
std::cout << "p + 100 = " << p + 100 << std::endl;
std::cout << "array[100] = " << array[100] << std::endl;
std::cout << "*(p + 100) = " << *(p + 100) << std::endl;
}

/*
array = 0x7fff92835240
p = 0x7fff92835240
p + 1 is 0x7fff92835244
array + 1 = 0x7fff92835244
array[1] = 1
*(p + 1) = 1
array + 100 = 0x7fff928353d0
p + 100 = 0x7fff928353d0
array[100] = 1
*(p + 100) = 1
*/

And the compiler doesn't even blink.

Dec 12 '06 #11

Andre Kostur wrote:
"Salt_Peter " <pj*****@yahoo. comwrote in
news:11******** **************@ 73g2000cwn.goog legroups.com:

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

How about v[1]?
sorry, also a pointer: usually implemented with:

reference operator[](size_type n)
{
return *(begin() + n);
}

Dec 12 '06 #12
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]?
I don't believe the [] operator does bounds checking in vectors, while
the at() function does.

Dec 12 '06 #13
>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]?

sorry, also a pointer: usually implemented with:

reference operator[](size_type n)
{
return *(begin() + n);
}
That's stuff controlled by the implementor of the runtime, not the user.
If you want to continue that argument, at is probably implemented as:

reference at(size_type n)
{
if (n < capacity)
return *(begin() + n);

throw std::out_of_ran ge();
}
If you want to continue that argument to it's conclusion, it's impossible
to write a (non-trivial) C++ program without pointers. Everything (class
instances) has a this pointer. std::list<has pointers internally. So
does std::map<and std::auto_ptr<> . Every one of your string literals
decays to a pointer.
Dec 12 '06 #14

"Salt_Peter " <pj*****@yahoo. comwrote in message
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
no naked pointers means no bugs and no aspirins
Whether or not an implementation (or even ALL implementations ) implement an
array as a pointer, is irrelevant. Your statement was regarding "naked"
pointers. Are you now saying "no pointers of any kind, and no arrays, and
nothing which may be implemented as a pointer, means no bugs and no
aspirins"? Or perhaps "no code means no bugs and no aspirins"? :-)

-Howard
Dec 12 '06 #15
Salt_Peter wrote:
>
Default User wrote:
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.
Oh yes, they most definitely are, through and through. Case in point:
the code above.
Nope. In some cases, array names are converted to pointers to the first
element. However, arrays are not pointers.

Look into the sizeof and & operators.


Brian

Dec 12 '06 #16

Andre Kostur wrote:
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]?
sorry, also a pointer: usually implemented with:

reference operator[](size_type n)
{
return *(begin() + n);
}

That's stuff controlled by the implementor of the runtime, not the user.
If you want to continue that argument, at is probably implemented as:

reference at(size_type n)
{
if (n < capacity)
return *(begin() + n);

throw std::out_of_ran ge();
}
If you want to continue that argument to it's conclusion, it's impossible
to write a (non-trivial) C++ program without pointers. Everything (class
instances) has a this pointer. std::list<has pointers internally. So
does std::map<and std::auto_ptr<> . Every one of your string literals
decays to a pointer.
Everything is eventually a pointer or using one somewhere. Thats a
given.
Pointers are far from being useless, programs wouldn't function without
them.
In fact, pointers are quick/fast - assuming they are used correctly.

Take the OP's quest as a matter in point. Can you see his dilemna?
I can, and their are solutions - thats my point.

I'll bet that if we were to dissect his code he's got plain mutable
pointers all over the place instead of constant pointers to immutable
variables. I'll bet he's using naked pointers where he should implement
constant references. I'm also willing to bet that he can replace all or
most allocations with smart_pointers and/or load elements into STL
templated containers.

The issue is not wheter we have the choice to use them or not. We
don't.
The choice we do have, is how we use them.

Dec 13 '06 #17

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

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

no naked pointers means no bugs and no aspirins

Whether or not an implementation (or even ALL implementations ) implement an
array as a pointer, is irrelevant. Your statement was regarding "naked"
pointers. Are you now saying "no pointers of any kind, and no arrays, and
nothing which may be implemented as a pointer, means no bugs and no
aspirins"? Or perhaps "no code means no bugs and no aspirins"? :-)

-Howard
Let me answer your question this way, after all, you brought arrays
into the topic for some reason.

If a client asked me to implement a safe solution that must absolutely
use arrays, i'ld probably wrap a primitive array in a class and
implement range-checking through some exception mechanism. I'm certain
you'ld do the same. Why? Cause its a relatively simple solution which
will/should provide the safety requirement.

In other words, to answer the OP's question, there are alternatives
other than having to manage a bunch of pointers directly. Those
alternatives, for the most part, will not complicate the code. In many
cases, they will simplify it. Example: boost::shared_p tr<or
std::deque, etc.

That doesn't mean don't use pointers. Thats impossible. That doesn't
mean you can't use them safely.

Instead of the all-too common:
void foo(int* p_n) {... }
....use...
void foo(int const * const p_n) { ... }
....or in the case *p_n needs to be mutable...
void foo(int* const p_n) { ... }
.... and even better ...
void foo(const int& r_n) { ... }
void foo(int& r_n) { ... }

And we can go on and on.

Dec 13 '06 #18
Apart from all the solutions suggested above, You can use Smart
Pointers, you can make your own class of pointers with constructors
and destructors and prevent memory leaks.

Iterators too are a good alternative .
While designing classes you can associate iterators with them.

Iterators are much safer way of referrencing and dereferrencing things
in a c++ program.
You can use the following link to read about iterators.

http://cppreference.com/iterators.html

I suggest you to search google for "smart pointers" and " iterators" .
Hope this helps.

Saurabh :-)

Dec 13 '06 #19

"Salt_Peter " <pj*****@yahoo. comwrote in message
news:11******** **************@ j72g2000cwa.goo glegroups.com.. .
>
Howard wrote:
>"Salt_Peter " <pj*****@yahoo. comwrote in message
news:11******* *************** @73g2000cwn.goo glegroups.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

>no naked pointers means no bugs and no aspirins

Whether or not an implementation (or even ALL implementations ) implement
an
array as a pointer, is irrelevant. Your statement was regarding "naked"
pointers. Are you now saying "no pointers of any kind, and no arrays,
and
nothing which may be implemented as a pointer, means no bugs and no
aspirins"? Or perhaps "no code means no bugs and no aspirins"? :-)

-Howard

Let me answer your question this way, after all, you brought arrays
into the topic for some reason.
Geez... You said, and I quote again: "no naked pointers means no bugs and no
aspirins". So I presented a simple program with _no_ naked pointers, but an
obvious bug. I then followed that with a "smiley face", indicating I was
joking with you, pointing out the (to me) obvious humor inherent in such a
statement. If you don't get it by now, forget it.

-Howard


Dec 13 '06 #20

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
25771
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
1523
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
2196
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
2416
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
15712
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
1742
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
2396
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
9425
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
10228
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...
1
10002
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
9869
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...
1
7415
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
6676
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();...
0
5312
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...
2
3575
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
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.