473,803 Members | 3,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

contradiction in TC++PL?

Here's a code fragment from TC++PL Special Edition, p291:

void f1(T a)
{
T v[200];
T* p = &v[0];
p--; // please note (my comment)
*p = a; // oops: 'p' out of range, uncaught
++p;
*p = a; // ok
}

And here's an excerpt from p92:

"The result of taking the address of the element before the initial element
is undefined and should be avoided."

The author says, as I understand, it's ok with ++p after p--, which I think
contradict the statement in the excerpt. What do you think?

--
ES Kim
Jul 22 '05
42 2114
JKop wrote:
std::size_t SIZE = 6;

int array[SIZE];

for(int* ptr = array[0]; ptr != &array[SIZE]; ++ptr)
The init clause should be int *ptr = array; or int* ptr = &array[0];
My fat fingers (and outlook makes a lousy syntax checker).

By the way, the array size needs to be a constant experssion above.

Let's say we've 64MB of RAM. 63MB of RAM are being used. We want to create
an object of a structure, where sizeof(MyStruct ) == .75MB

Not enough memory.

It's got nothing to do with RAM. It has to do with address space.
Go read a book on computers. There doesn't need to be any memory
(virtual or otherwise) backing up the "inaccessib le" values.
Jul 22 '05 #41
JKop wrote:
What the hell ever happened to the whole concept of POD?

Plain Ol' Data

PLAIN OL' DATA!
When I have the likes of:
struct Blah
{
int a;
double b;
int* p_k;

float** p_p_f;
};
I should be able to set it to whatever the hell I please!
PODs aren't types of the 60s era, but of the C90 era.
Checking a pointer to see if it has a valid value (and perhaps throwing an
exception if not) is complete and utter total bullshit. It's inefficient
too. If I wanted such a mechanism, I'd explicitly ask for it:

int* k = ...

CheckPointer(k) ;
I'd like some clarity on the issue. Will the following behave identically on
all implementations , ie. will it output "I'm not a shit implementation! " on
all platforms?:
int main()
{
bool* p_boolean = reinterpret_cas t<bool* const>(92729);

std::cout << "I'm not a **pepper** implementation! ";
}


The above never causes side effects. Assignment of an integer to a
pointer type results in implementation defined behaviour, as far as you
do not dereference it.
"1.3.5 implementation-defined behavior

behavior, for a well-formed program construct and correct data, that
depends on the implementation and that each implementation shall document."

This means that you will never get unexpected effects for that.

On the other hand, pointing with a pointer to an invalid object (usually
with pointer arithmetic), results in undefined behaviour.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #42
JKop wrote:
What's the logic behind being able to have a pointer to one past the end of
an array?

Algorithms. One reason is in loops using a pointer or iterator with p++
incrementing, which at the end of the sequence points one past the last
element, and thus it has to be guaranteed that it is safe to do so.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #43

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

Similar topics

3
1745
by: Guofu Chen | last post by:
Hi, In TC++PL, Chapter 10, there is an exercise which ask us to modify the following program: #include <iostream> int main() { std::cout << "Hello, world!/n" ;
8
2978
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular, operator =, operator, operator(), and operator-must be nonstatic member function; this ensures that their first operands will be lvalues". I know that these operators must be nonstatic member functions, but why this ensure their first operands will...
0
9703
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
10548
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
10295
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
7604
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
6842
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
5500
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
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
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
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.