473,378 Members | 1,549 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,378 software developers and data experts.

Messing with intptr_t

Is the following program legal? (I think it is, though it relies on
the existence of intptr_t.)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

int main(void)
{
char *p;
intptr_t i;

if(!(p = malloc(10)))
abort();
strcpy(p, "hello");

i = (intptr_t)(void *)p;
i ^= 63;
p = 0;

/* plugh */

i ^= 63;
p = (char *)(void *)i;

printf("%s\n", p);
return 0;
}

This is suggested, of course, by the thread on garbage collection.
It's just the sort of program that is likely to break with a collector
of the Boehm type, because at the point commented "plugh" there is
probably nothing resembling a pointer to the malloc()ed memory, so if
there were another malloc() there it might re-use the same memory.

-- Richard
Oct 10 '06 #1
2 5139
Richard Tobin wrote:
Is the following program legal? (I think it is, though it relies on
the existence of intptr_t.)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

int main(void)
{
char *p;
intptr_t i;

if(!(p = malloc(10)))
abort();
strcpy(p, "hello");

i = (intptr_t)(void *)p;
i ^= 63;
p = 0;

/* plugh */

i ^= 63;
p = (char *)(void *)i;

printf("%s\n", p);
return 0;
}

This is suggested, of course, by the thread on garbage collection.
It's just the sort of program that is likely to break with a collector
of the Boehm type, because at the point commented "plugh" there is
probably nothing resembling a pointer to the malloc()ed memory, so if
there were another malloc() there it might re-use the same memory.
There's nothing obviously wrong, as far as I can see. The
same thing can be done without reliance on intptr_t by mucking
with the individual `unsigned char' constituents of `p'.

Fans of garbage collection like to label this sort of thing
"perverse," with the implication that no reasonable program would
indulge in such whimsy and so only unreasonable progams would be
broken by their garbage collectors. And yet, there are plenty
of programs that make constructive use of low-order pointer bits
that they "know" will be zeroes, using them to store a handy flag
or a small type code or something. Such programs are already not
portable (the "knowledge" of low-order zeroes is not portable),
but can be extremely practical -- and would be utterly destroyed
by the C-ish garbage collectors I've heard of.

--
Eric Sosman
es*****@acm-dot-org.invalid
Oct 11 '06 #2
ri*****@cogsci.ed.ac.uk (Richard Tobin) wrote:
# Is the following program legal? (I think it is, though it relies on
# the existence of intptr_t.)

Twiddling pointers as integers as a long and hallowed history
in C. And yes it runs havoc with Boehm style collectors.

The classical example is to stored two xored pointers in one
link location. If you have the current cell X and the previous
P and next cell N, one 'optimisation' is to store a single link
field in X which is P^N. Then if you traverse from P to N you
compute N = X->link ^ P; from N to X, P = X->link^N.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
She broke your heart and inadvertently drove men to deviant lifestyles.
Oct 11 '06 #3

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

Similar topics

3
by: D. Alvarado | last post by:
Hi, I have these files /include/db_info.inc /html_root/util_fns.inc /html_root/home_page.php /html_root/admin/admin_home.php The page "util_fns.inc" contains the line
1
by: leegold2 | last post by:
If I try to query ie. search via PHP/MYSQL my db for this string: foo&bar It will not find the string with either FullText or a sub-string search (ie. %NA&SD% ). I think the ampersand is...
10
by: Xah Lee | last post by:
i'm using groups-beta.google.com to post python code. Is there a way to stop google from messing with my format? it seems to have eaten the spaces in my python code. thanks. Xah...
4
by: Trent L | last post by:
Hello, IE6.0 is messing up my lists. If you scroll really fast up and down my page, the links on the left and the right get covered over with the background color. It almost looks like the...
3
by: Anders K. Jacobsen [DK] | last post by:
I simple can't get visual studio 2003 to stop messing with my HTML code. I have unchecked all formating options under Tools / Options / Text editor / HTML/XML / Format I have even tried to...
3
by: Edward Diener | last post by:
I am getting the above warning when compiling a line in a file which assigns an 'intptr_t' result to a 'long' variable. I do not see how intptr_t can possibly be larger than a long unless intptr_t...
5
by: king | last post by:
Isn't intptr_t part of the latest ISO C++? It is part of the C99 standard but C++? Has any new standard for C++ after C++ 2003 which is almost same as C++98 published? Which are the compilers that...
2
by: nikanth | last post by:
Isn't intptr_t part of the latest ISO C++? It is part of the C99 standard but C++? Has any new standard for C++ after C++ 2003 which is almost same as C++98 published? Which are the compilers that...
19
Sheepman
by: Sheepman | last post by:
I still messin' around with functions. This is a variant of the program I was messing up early. I'm trying to reduce the amount of work each function is doing. I figured if could get this to work I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.