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

Pointing outside the heap


Is the following code invalid C:

char *a = malloc(1);
a--;
a[1] = 3;

because there is an intermediate pointer that points outside the array?

--
Dr Jon D Harrop, Flying Frog Consultancy
The OCaml Journal
http://www.ffconsultancy.com/product...ournal/?usenet
Jul 14 '07 #1
5 1428
Jon Harrop wrote:
Is the following code invalid C:

char *a = malloc(1);
a--;
a[1] = 3;

because there is an intermediate pointer that points outside the array?
Correct. There is an exception that allows pointers to point just after the
end of an array, but there is no similar exception for a pointer to point
just before the start of an array.
Jul 14 '07 #2
Jon Harrop wrote:
Is the following code invalid C:

char *a = malloc(1);
a--;
a[1] = 3;
Yes; it's invalid in the sense that the effect of
executing it is left completely open ("undefined")
by the Standard.

Implementations may do what they will with it, and may
assume that the programmer hasn't been wicked enough to
do things like it.
because there is an intermediate pointer that points
outside the array?
Because it attempts to compute a pointer that lies outside
an allocated object. (There's no such "intermediate pointer",
because trying to compute it leaves you in limbo.)

--
Far Hedgehog
"Who do you serve, and who do you trust?" /Crusade/

Jul 14 '07 #3
Jon Harrop <jo*@ffconsultancy.comwrites:
Is the following code invalid C:

char *a = malloc(1);
a--;
The above two lines by themselves invoke undefined behavior.
a[1] = 3;

because there is an intermediate pointer that points outside the array?
The three lines of code as a group also invoke undefined
behavior, yes, and that's the reason: pointer arithmetic is only
allowed within an array or one-past-the-end of an array.
--
"I hope, some day, to learn to read.
It seems to be even harder than writing."
--Richard Heathfield
Jul 14 '07 #4

"Jon Harrop" <jo*@ffconsultancy.comwrote in message
news:46**********************@ptn-nntp-reader02.plus.net...
>
Is the following code invalid C:

char *a = malloc(1);
a--;
a[1] = 3;

because there is an intermediate pointer that points outside the array?
It's illegal, though it will work as you think it ought on the vast majority
of machines.
Consider a segmented architecture system and a malloc() that likes to return
blocks starting on segment boundaries. The compiler would need a special
patch to compute a - 1 correctly and then offset by one again, which is more
trouble to implement than it is worth. Therefore the standard allows it to
calculate an address pointing somewhere into the wilds, or to trap, hence
undefined behaviour.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Jul 14 '07 #5
Jon Harrop wrote:
Is the following code invalid C:

char *a = malloc(1);
a--;
a[1] = 3;

because there is an intermediate pointer that points outside the array?
This is Question 6.17 in the comp.lang.c Frequently Asked
Questions (FAQ) list at <http://c-faq.com/>.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jul 14 '07 #6

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

Similar topics

0
by: ANt | last post by:
Hi, we have some major GC issues at present with a system we're trying to put live. It's a live calculation engine that's distributed across about 30 Java server processes. A set of processes...
14
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not...
23
by: Gautam | last post by:
this piece of code assigns an int pointer(evident) to a char, and when i try to access the ascii value of the char through the integer pointer(p) , what i get is a junk value or not i...
31
by: Andrej Prsa | last post by:
Hi! What happens to a globally defined pointer, e.g. void *value; that points to a particular type in a function: int dummy_func (int a) {
10
by: joint52 | last post by:
Hello This seems to work in my system, until variable 'i' reaches 15985, then the program crashes. My question is, why won't it crash sooner? How does it work? And why am I able to set the...
0
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. ...
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
0
by: wildman | last post by:
RE: ASP 1.0 site, pointing to ASP 2.0 site. SSL and authentication obsticles?? First off, Is there any way to simple development on an asp.net 1.0 site using vs 2005, if you don't have the...
4
by: ggoubb | last post by:
The purpose of the Insert function is to add a new integer in the Heap assuming that it is not already full. If Heap capacity has been reached, it attempts to double the current capacity. If capacity...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.