int to int* conversion

P

Pallav singh

It expressedly says that there might not be one (in a
parenthese).


I've used implementations where a pointer was bigger than a
ptrdiff_t.  (For that matter, I've used implementations where
there wasn't any integral type as large as a pointer.  Now that
long long exists, and requires at least 64 bits, I imagine that
the case will be rare.)

The next version of the standard will include intptr_t and
uintptr_t, in <stdint.h> (or <cstdint>, if you prefer).  But the
type is still "optional", and will only be present if such a
type exists.

--
James Kanze (GABI Software)             email:[email protected]
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

int * ptr = 0;
cout << ptr++;
cout << *ptr++;

whats will be output ?

Thanks
Pallav Singh
 
P

Pascal J. Bourguignon

Pallav singh said:
int * ptr = 0;
cout << ptr++;
cout << *ptr++;

whats will be output ?

Implementation dependant for the first cout (on a normal processor it
may print 0 in some format), undefined for the second (on a unix
system will probably crash with a SIGSEGV).


For example, on a 64-bit Intel processor with Linux:

#include <iostream>
using namespace std;
int main(void){
int * ptr = 0;
cout << ptr++ << endl;
cout << *ptr++ << endl;
return(0);
}
/*
-*- mode: compilation; default-directory: "/tmp/" -*-
Compilation started at Wed May 13 11:31:24

g++ --version ; SRC="/tmp/p.c++" ; EXE="p" ; g++ -g3 -ggdb3 -o ${EXE} ${SRC} && ./${EXE} && echo status = $?
g++ (Gentoo 4.3.3-r1 p1.1, pie-10.1.5) 4.3.3
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

0
/bin/bash: line 1: 7302 Segmentation fault ./${EXE}

Compilation exited abnormally with code 139 at Wed May 13 11:31:24
*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top