473,700 Members | 2,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5165
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
1689
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
1676
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 messing things up? If you were to implement a search via PHP of MYSQL records what would you do to make this string searchable. I'm not sure how. Thanks, Lee G.
10
1357
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 xah@xahlee.org http://xahlee.org/PageTwo_dir/more.html
4
1686
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 page is tearing: http://www.spacerook.com The site validates according to the HTML Validator and the CSS Validator. It looks fine in Mozilla and Opera.
3
1396
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 uncheck every assitence i could see remotly related to this. But it just keeps messing it up. eg <td>text</td> is very often converted to <td>text</TD>
3
3416
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 is a 64 bit value and I know I am not compiling for 64 bit Windows.
5
10194
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 supports it completely? on gcc (GCC) 3.3.3 (SuSE Linux) I dont get any error when using intptr_t even after specifying -std=c89 http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=223&rl=1
2
10493
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 supports it completely? on gcc (GCC) 3.3.3 (SuSE Linux) I dont get any error when using intptr_t even after specifying -std=c89 http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=223&rl=1
19
2878
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 can edit the original program to this format. It's generating the random numbers, creating the user named file but not writing to it. How do I get the print function, echoPrint, to print data from the random number generator and later on from...
0
8721
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
8642
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
9069
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8964
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
6558
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
4653
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3085
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
2385
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2024
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.