473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

C++ pointers

AJ
//C++

char* p;

while (*p)
{}

/* Access violation. How can I check to see if p is valid? Thanks.*/

Jun 21 '06 #1
6 4471
AJ wrote:
//C++

char* p;

while (*p)
{}

/* Access violation. How can I check to see if p is valid? Thanks.*/


There is no compiler- or platform-independent way to check that
a pointer is "valid". Besides, what's your definition of pointer
validity? And what it is you're trying to do in that code?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 21 '06 #2
AJ wrote:
//C++

char* p;

while (*p)
{}

/* Access violation. How can I check to see if p is valid? Thanks.*/


You can check to see if it's a null pointer, but beyond that there's
nothing standard you can do for an uninitialized or deleted pointer.

Brian
Jun 21 '06 #3
AJ posted:
//C++

char* p;

while (*p)
{}

/* Access violation. How can I check to see if p is valid? Thanks.*/

You check to see if it's valid by opening up your favourite text editor
and going through your code to see when and where you gave it a value (if
you did).

In C++, you can give a pointer a particular value to indicate that it
doesn't really point to anything. This is called the null pointer value.
Here's a taste of it:

char *p = 0;

if (p) DoSomethingWithAddress(p);
If you define a variable within a function without initialising it (and
without subsequently setting its value), then it contains white noise --
and therefore you can't rely on it having the "null pointer value"... it
may in fact point to a boolean value stored in memory which decides if
your hard disk should be formatted.

Jun 22 '06 #4
AJ wrote, On 21.6.2006 21:10:
//C++

char* p;

while (*p)
{}

/* Access violation. How can I check to see if p is valid? Thanks.*/

Use valgrind or some other platform specific tool.

--
VH
Jun 22 '06 #5
In article <1150917030.618581.196250
@m73g2000cwd.googlegroups.com>, aj********@yahoo.com
says...
//C++

char* p;

while (*p)
{}

/* Access violation. How can I check to see if p is valid? Thanks.*/


There is no portable way to check whether a pointer is
valid. For that matter, there's no portable definition of
what constitutes a valid pointer in the first place.

One obvious alterative is to use smart pointer class of
some sort instead of using a raw pointer to start with.
The smart pointer class can include code to check whether
its pointer has been initialized or (more common)
initialize its pointer so it's always been assigned a
value. Of course, this still isn't a panacea -- if you do
something equivalent to:

char *p = (char *)1;

chances are you'll end up with a pointer that's still
invalid -- but for that to happen, you have to do it
explicitly, rather than it happening by default.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 22 '06 #6

"AJ" <aj********@yahoo.com> wrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
//C++

char* p;

while (*p)
{}

/* Access violation. How can I check to see if p is valid? Thanks.*/


char* p = NULL;

while ( p != NULL && *p )
{
}
Jun 23 '06 #7

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

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
92
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,...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
54
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...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.