473,796 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

set and reset the least significant bit of a address

I want to set and reset the least significant bit of a address (where a
pointers points to).
I tried this, but it is not correct:
#define BIT 0x1

void foo(){
void *p;
*p = *p & ~BIT
}
Nov 14 '05
52 7076
In <42************ ***********@new s.wanadoo.fr> jacob navia <ja***@jacob.re mcomp.fr> writes:
Flash Gordon wrote:
Unless someone has gone around deleting all copies of the old standard
it still exists. It just is not the official current standard, only the
standard that actually is implemented.

Of course you and the old standard exist.
I wouldn't discuss that.

But "standard C" then, makes no sense.


What you fail to realise is that "standard C" has a context-dependent
meaning, because the *relevant* C standard is different in different
contexts. There is no question that the relevant C standard for
comp.std.c is C99. OTOH, c.l.c is dedicated to portable programming
in the real world, where the C99 standard is still a pipe dream (with
insignificant exceptions).

Furthermore, optional C99 features cannot be used in portable C99
programs, either.
Since, as you have been told before, we deal with portable C it is
important to know which version of the standard is applicable when it is
not the most commonly implemented one.


Ahh not standard C but portable C.


It is adherence to a C standard that makes C code portable.
OK. Then how would you implement portably

<inttype> p = (void *)p;

?????????


By issuing the diagnostic *required* by the (C89/C99) standard.

BTW, implementations need not be portable (and I'm not aware of
any portable implementations , merely of implementations that can be
easily ported).

Dan
--
Dan Pop <Da*****@ifh.de >
Nov 14 '05 #21
In article <42************ *********@news. wanadoo.fr>,
jacob navia <ja***@jacob.re mcomp.fr> wrote:
If the construction code changed the standards for nails
and my appartment has the old nails, my appartment is then
NOT STANDARD, and when I sell it, or for the insurance claims
I am supposed to change the nails.


That's certainly not the case in this country.

-- Richard
Nov 14 '05 #22
In article <42************ ***********@new s.wanadoo.fr>,
jacob navia <ja***@jacob.re mcomp.fr> wrote:
:Ahh not standard C but portable C.

:OK. Then how would you implement portably

: <inttype> p = (void *)p;

You don't.

I must admit that I fail to understand the point. Even supposing there
is a portable, standards-conforming way to reversibly convert between
an void* and an integral type, as soon as one modifies even a single bit
of the integral representation, one loses all guarantees that the
back-conversion will point to anything. Clearing the last bit of
a pointer could result in a pointer to something before the object
(e.g., a char with automatic storage class is not necessarily stored
at an even address); the resulting pointer would be completely
unusable as far as C is concerned. Setting the last bit of the
integral type and back-converting could result in a pointer
past the object (which cannot be derefenced, but at least can be
compared in some circumstances.)

And of course there is the possibility that the integral representation
of the pointer could be an opaque token. The verbage in the standard
about increasing addresses within an object does -not- imply that
pointers to successive values will have values that are numerically
related to each other. It would, for example, be valid for the
numeric representation of a pointer to be bit reversed and for there
to be segment or type information suffixed. The requirement even when
the conversion exists is only that the conversion be reversible, not
that the converted value have any particular internal structure.
--
Warning: potentially contains traces of nuts.
Nov 14 '05 #23
jacob navia wrote:
Flash Gordon wrote:
Unless someone has gone around deleting all copies of the old standard
it still exists. It just is not the official current standard, only
the standard that actually is implemented.

Of course you and the old standard exist.
I wouldn't discuss that.

But "standard C" then, makes no sense.

You want YOUR particular view of the standard, as you would like
it to be.


No, the standards as published by ISO and ANSI. Also, on rare occasions,
C as it was before the ANSI standard was published.
When implementing the extensions of lcc-win32 I cared to
remain compatible with the standard. But when I discuss them
a holy alliance of people like you start crying

"Standard C, Standard C"

Now, the same people write:

The standard is what I want it to be. I will use this
standard, that standard, etc.
This group allowing discussions of all versions of the standard, and
both hosted and non-hosted implementations , is nothing new. The
difference is that *your* extensions are not covered by *any* C standard
that has been published or by what C was before the first ANSI standard
was published.
Since, as you have been told before, we deal with portable C it is
important to know which version of the standard is applicable when it
is not the most commonly implemented one.


Ahh not standard C but portable C.


C89 is still a standard, just not the latest version of the standard.
OK. Then how would you implement portably

<inttype> p = (void *)p;

?????????


Obviously I have absolutely no idea what it is intended to do since it
is just a syntax error in C.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #24
On Wed, 16 Mar 2005 05:52:02 -0500, no*@given.out
<no*@given.ou t> wrote:
Yes, compilers, languages, even some user stuff, should be held to something
of a standard. But shouldn't that standard be a _minimum_ instead of an
exact target? If we all adhere to C-89 where's the growth and improvement
in the language and, by extension, our code? It makes far more sense to me
for compiler writers (and I'm not one) to push the limits always seeking
newer and better ways of doing things. So long as that minimum standard is
met, what's the problem?
The problem is that anyone using the extensions is then locked-in to
that compiler, unless the compiler writer can persuade other compiler
writers to use it.

Take, as an example, GCC's implementation of macros with variable
numbers of arguments. It was a great extension, allowing you to write
things like:

#define eprintf(format, args...) fprintf (stderr, format , ## args)

eprintf("stuff\ n");
eprintf("stuff= %d\n", 42);

The only trouble is, if you try to compile that with any other compiler
(MS, Borland, Intel, etc.) it will fail to compile, because it isn't
standard. In practice, a lot of code uses it because GCC is one of few
compilers with a lot of ports on different platforms and targetting many
processors, but that doesn't mean that it will always be there even in
that compiler (with the C99 __VA_ARGS__ feature the GCC one may become
obsoleted). Or case ranges (a useful feature which didn't get into
C99).
For example... I would love to see a C implementation (not c++ or C#...
basic C99) with real string variables. And why not? So long as the
compiler meets some agreed minimum standard, what's wrong with going beyond
that?


As long as you know that (a) your code is never going to be used on
another compiler and (b) the compiler you use is always going to be
around for as long as that code needs to be compilers, there's nothing
wrong with it. But compilers go out of date and are dropped or not
ported to a modern platform (especially when they are proprietary source
code so if the original maintainer drops it -- or in some cases drops
dead -- it is lost), and you are stuck with source which no longer
compiles because it is using extensions to the language. Or which no
longer compiles for the new processor.

People who use the extensions of a particular compiler tend to forget
after a while that they are extensions, and so become less good at
writing standard-conforming programs (writing C++, for instance, does
this as well, I'm always doing things like trying to declare variables
in the middle of code).

Chris C
Nov 14 '05 #25
no*@given.out wrote:

For example... I would love to see a C implementation (not c++ or C#...
basic C99) with real string variables. And why not? So long as the
compiler meets some agreed minimum standard, what's wrong with going beyond
that?


Lcc-win32 supports a "real" string package, with length delimited
strings, but it needs one extension: operator overloading.

Otherwise, the functions are almost the same:
Strcat, Strcmp, etc.

jacob
Nov 14 '05 #26
no*@given.out wrote:
jacob navia <ja***@jacob.re mcomp.fr> wrote:
Lcc-win32 supports a "real" string package, with length delimited
strings, but it needs one extension: operator overloading.


Yep, saw that in my travels... but alas it also lacks a function I
need in my day to day work... there is no try-finally construct
(or, more accurately, there is no *documented* try-finally
construct).


Then what are you doing here? C doesn't support those either. You
may want either Ada or C++.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #27
no*@given.out wrote:
On Thu, 17 Mar 2005 00:40:31 GMT, CBFalconer <cb********@yah oo.com> wrote:

Then what are you doing here?

Mostly, I'm reading and learning. Is that a problem?

Or have I perhaps enfringed on someone's protectorate?

But not to worry... I'm not fond of handing around places where people have
the wagons so tightly circled that even a wrong nod can get one into
trouble.

Unsubscribing now.


Well, Chuck is special... he doesn't mind it as an offense.

Just do not overreact.
Nov 14 '05 #28
Peter Nilsson wrote:
onsbomma wrote:
.> I want to set and reset the least significant bit of a address
.> (where a pointers points to).
.> I tried this, but it is not correct:
.>
.> #define BIT 0x1
.>
.> void foo(){
.> void *p;
.> *p = *p & ~BIT

Ignoring that p is not initialised, dereferencing a void pointer
is a constraint violation. Use a pointer to unsigned char if you
want to modify a byte.

And how should I do that??
Just making it
void foo(){
char* p;
p = p & ~BIT
}

won't work
Nov 14 '05 #29
In article <11************ ***@seven.kulne t.kuleuven.ac.b e>,
onsbomma <on******@hotma il.com> wrote:
:Peter Nilsson wrote:
:>>onsbomma wrote:
:>>.> void foo(){
:>>.> void *p;
:>>.> *p = *p & ~BIT

:> Ignoring that p is not initialised, dereferencing a void pointer
:> is a constraint violation. Use a pointer to unsigned char if you
:> want to modify a byte.

:And how should I do that??
:Just making it
: void foo(){
: char* p;
: p = p & ~BIT
: }

:won't work

In the first version, you had *p = something so you were dereferencing the
pointer. In this newer version, you are modifying the pointer instead of
what it points to.

It isn't at all clear to me what you are trying to do, since it is
not apparent in the code snippet that p has a value (just declaring
it on the stack doesn't give it a value), and it is not apparently
how you are testing whether changing it has done anything or not.
I don't think that snippet would even compile, since '&' is not a
defined operator on pointers.

If you want to modify a pointer itself in ways other than can
be done by using ++ or -- or += or -=, then you need to convert
the pointer into an arithmetic type. As I discussed earlier, the
C language makes no promise that the converted pointer will have
any resemblence to a meaningful address: at most (C99) it promises
that the conversion is "reversable ". C89 does not even promise that
(though K&R2 does.) The only reversable pointer operation defined
in C89 that I could find, is printing and scaning a pointer with %p
format.

There is, though, one portable thing you can do: if the
object being worked with is a pointer returned by malloc() then
the pointer will be aligned for use with native objects with the
tightest alignment requirements. If you take a copy of that
initial pointer, and later take the difference of that
pointer and the test pointer ('p' in your code), both cast
to (char *), then the difference will be in bytes and the
difference can be tested for evenness or oddness. That will
tell you whether the test pointer is logically even or odd
*if* the platform constrains even one of the primitive types
to even addresses. However, if the platform happens to be
one in which there -are- no alignment restrictions on the
native types, then malloc() could return an odd value and
examining the difference would not be useful.
--
Would you buy a used bit from this man??
Nov 14 '05 #30

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

Similar topics

5
7756
by: Eric Kincl | last post by:
Hey, I was wondering if it's possible to reset the "auto_increment" feature in MySQL. For example, if I have a database with a list that will change every year (the old list be completely flushed out, deleted) and then I insert a new list, it will very quickly reach the max INDEX length... Does it just wrap itself? Is there a way to reset "auto_increment" so that I can flush all the rows, reset auto_increment, and start from scratch? ...
4
11195
by: Donnal Walter | last post by:
On Windows XP I am able to connect to a remote telnet server from the command prompt using: telnet nnn.nnn.nnn.nnn 23 where nnn.nnn.nnn.nnn is the IP address of the host. But using telnetlib, this code returns the traceback that follows: import telnetlib host = 'nnn.nnn.nnn.nnn'
16
20534
by: John Baker | last post by:
Hi: I know this is a strange question, but I have inherited a system where files are copied and records re auto numbered (as an index field) )frequently, and I am wondering how high the number can go without the system crashing. An ancillary question is how one resets an auto number so that the sequence starts again at 1. In the case of this file, the auto number field serves no useful purpose except as an
8
2178
by: spike | last post by:
My prygram goes through a string to find names between '\'-characters The problem is, parts of the name in sTemp remains if the new name is shorter than the old name. code --------------------------------------------------- char sTemp; char sText = "THELONGESTNAME\\samantha\\gregor\\spike\\..."; // and so on...
20
9180
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg., int_fast32_t 4) integer capable of holding a pointer, intptr_t 5) widest integer in the implementation, intmax_t Is there a valid motivation for having both int_least and int_fast?
0
1434
by: Larry Serflaten | last post by:
I recently fired up VS2003 while online which I have done on numerous occasions before. When I opened the toolbox, ZoneAlarm popped up a box saying the IDE wanted to access the internet. This was the first time it tried to connect to the internet, that I can recall, and it was to some address I did not recognise, so I denied it. VS hung, and when I tried to kill the process (Task Manager) all my desktop windows went into tiled view...
3
18750
by: kashif_khan | last post by:
hi everyone, can anybody please tell me why reset button is not working in the following code, i tested it in opera, firefox and konqueror, <form action='edit_links.php' method='POST' name='save_links' id='save_links'> <table cellpadding='5'>
4
7978
by: thegeneralguy | last post by:
Hello, I'm trying to find the least significant set bit (i.e. the first '1' bit in an int) through just bit twiddling, but I can't get it without resorting to a loop. My original idea was to reverse the bits in an int and find the most significant bit then subtract it from 31 to get the position of the LSB. Is there a way to get the LSB of an int using just bit manipulations? e.g., 0b0101 would return 0001, 0b0000 would return...
2
3205
by: DarthPeePee | last post by:
Hello everyone. I am working on a Password Strength Meter and I am running into 1 problem that I would like to fix. When pressing the "Clear Password & Try Again" button, the password clears out of the text box, but the meter will stay at its current position until text is entered back into the textbox. Once text is re-entered, the meter will display the results again. I would like everything to reset when the button is pushed, but I...
0
9685
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
9533
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,...
1
10190
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
7555
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
6796
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
5447
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...
1
4122
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
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
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.