Richard said:
Not only did the OP not specify Unix, but this is not guaranteed to work
even on that platform. For example, the library might allocate page-sized
blocks. If so, allocating a three-byte block would result in p + 3 being
invalid in C language terms but acceptable to the OS (but
comp.unix.programmer would know more about that). I just tested this by
allocating three bytes and writing to all four of them (admittedly on Linux
rather than Unix), and the write did not cause a segfault.
We don't know exactly what the OP meant by "valid", but let me guess
Usually when people ask about accessing A+3, they intend to use 32-bit
instructions to manipulate 8-bit data, sometimes with pre-fetch at the
end . A very common case is CRC-ing a network packet, which can be
done very speedily if you allow a little slop. Same with some
pattern-matching operations.
Which means the last time thru the loop the code might pre-fetch up to
3 bytes past the end of data. So all they might want to know is if the
memory is readable without major kabooms.
There isnt a way I know of in the C language to do this, but if you'll
lower yourself to allow a portability hazard....
C doesn't have try/catch.
Well, disingenous at best. The two most common C compilers, gcc and vc
which together cover maybe 80% of the market, DO have try and catch.
How are you going to do that in a portable manner, though? The OP posted in
comp.lang.c, which suggests he wants an answer in C language terms, not a
system call which will vary from system to system.
Well, for gnu and vc IIRC , you need abotu 7 lines of codee, just read
the map file and look for ".data 0x..........."
But the OP did not indicate that he is using an x86.
Seeing as that smelly architecture has more copies out there than
anything else, there's a good chance it meets the OP's needs.
Does that include the implementations used on or for:
* The Mac?
* The 360 series?
* Unisys boxes?
* Concurrents?
* SHARCs?
* Cybers?
very curious that you mention company names, instead of linker names.
The name of the ciompany has nothing to do with the linker.
All the above except for the 60-bit Cybers can run Linux and gcc and
the gcc linker.
Even on other OS's, it's nearly trivial to link in two little stubs
"low.a" and "high.a", one as the first object file, the other as the
last object file, thereby defining the beginning and end symbols for
the rare linker that doe4snt provide this information as link symbols
or in the link map file.
I don't know of a working C implementation for the 60-bit Cybers.
This code must run unmodified on the N4242, due Real Soon Now, for which the
hardware specs are not yet available. Suddenly this technique isn't so
foolproof.
Yep, it's a non-portable technique. Moving the code to a new system
might take, oh, two hours to rename a few API calls. Once that's done
all the bad pointers just jump out at you.
Versus stewing (as I have) for months trying to find unset pointers.
]
You decide whether it's worth porting the code.
And what if p+3 == 0xD29102933? Can you guarantee that p+3 is valid simply
because it is not 0xDEADBEEF?
.... right... it's not foolproof, but it does catch at least 3/4 the
problems. If you see DEADBEEF you know it's (probably) unused heap,
which depending on your point of view is valid (for just reading) or
invalid (for writing). if p+3 isnt DEADBEEF, well, you obviously can
read it.... writing ti may be bad.
Programming involves a lot of analog value judgements. Is it better to
catch 3/4 of the problems, or just claim it's impossible?
Is it okay to save a plane crashing by using a feature that has been
in 92% of the C compilers out there for a handful of years, even though
it's not in any standard, or do we just buy a flower-farm to send to
funerals?
Is it better to spend 2 hrs porting "non-portable" code, or wonder
forever if there's some random memory reference somewhere?
Do we give up, just because the Cyber loader which hasnt been run in
60-bit mode for over 15 years doesnt emit _End_DATA tags, or do we
document that it works on 88.8% of the systems out there?
Things arent always black and white. Many times a little openness to
shades of gray can be very helpful.