473,320 Members | 2,147 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,320 software developers and data experts.

Subtracting 0 from one-past-end pointer

For the following code:

int main(void)
{
short a[100], *ptr;

ptr = a + 100;
ptr -= 0;
}

a runtime bounds-checker I use gives an error "Pointer
underrun" on the ptr -= 0 line. Is there something wrong
with that statement?
Nov 14 '05 #1
7 1672
ol*****@inspire.net.nz (Old Wolf) writes:
int main(void)
{
short a[100], *ptr;

ptr = a + 100;
ptr -= 0;
}

a runtime bounds-checker I use gives an error "Pointer
underrun" on the ptr -= 0 line. Is there something wrong
with that statement?


No, not as far as I can tell. (I'm surprised that the compiler
emits any code for that statement though.)
--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms
Nov 14 '05 #2
Old Wolf wrote:
For the following code:

int main(void)
{
short a[100], *ptr;

ptr = a + 100;
ptr -= 0;
}

a runtime bounds-checker I use
gives an error "Pointer underrun" on the ptr -= 0 line.
Is there something wrong with that statement? cat main.c int main(int argc, char* argv[]) {
short a[100], *ptr = a + 100;
ptr -= 0;
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c
./main


It works fine for me.

Your run time bounds checker is probably buggy.
Nov 14 '05 #3
On Tue, 16 Nov 2004 18:05:11 -0800, Old Wolf wrote:
For the following code:

int main(void)
{
short a[100], *ptr;

ptr = a + 100;
ptr -= 0;
}

a runtime bounds-checker I use gives an error "Pointer
underrun" on the ptr -= 0 line. Is there something wrong
with that statement?


I don't see anything wrong with any part of the code.

Perhaps for some reason your bounds-checker does not like the statement
before the one in question where you assign ptr to point to one past the
last element of a. Do you experience the same error if you change it to
"ptr = a + 99"?

Rob Gamble
Nov 14 '05 #4
On Tue, 16 Nov 2004 18:27:25 -0800, E. Robert Tisdale wrote:
Old Wolf wrote:
For the following code:

int main(void)
{
short a[100], *ptr;

ptr = a + 100;
ptr -= 0;
}

a runtime bounds-checker I use
gives an error "Pointer underrun" on the ptr -= 0 line.
Is there something wrong with that statement?

> cat main.c

int main(int argc, char* argv[]) {
short a[100], *ptr = a + 100;
ptr -= 0;
return 0;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main


It works fine for me.

Your run time bounds checker is probably buggy.


$ cat test7.c
int main(void)
{
short a[100], *ptr;

ptr = a + 1000;
ptr -= 0;
}

$ gcc -Wall --std=c99 -pedantic test7.c
$ ./a.out

Works fine for me.

The point here of course being that just because your compiler didn't
produce any warnings, or even that the resulting program does not crash,
does not imply that the code exhibits no undefined behavior.

Rob Gamble
Nov 14 '05 #5
Ben Pfaff wrote:
Old Wolf writes:
int main(void)
{
short a[100], *ptr;

ptr = a + 100;
ptr -= 0;
}

a runtime bounds-checker I use
gives an error "Pointer underrun" on the ptr -= 0 line.
Is there something wrong with that statement?
No, not as far as I can tell.
(I'm surprised that
the compiler emits any code for that statement though.)

cat main.c int main(int argc, char* argv[]) {
short a[100], *ptr = a + 100;
ptr -= 0;
return 0;
}
gcc -Wall -std=c99 -pedantic -S main.c
cat main.sold

.file "main.c"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $232, %esp
andl $-16, %esp
movl $0, %eax
addl $15, %eax
addl $15, %eax
shrl $4, %eax
sall $4, %eax
subl %eax, %esp
leal -216(%ebp), %eax// a
addl $200, %eax // a + 100;
movl %eax, -220(%ebp)// ptr = a + 100
movl $0, %eax // return 0;
leave
ret
.size main, .-main
.section .note.GNU-stack,"",@progbits
.ident "GCC: (GNU) 3.4.1"

My compiler doesn't.
Nov 14 '05 #6
In article <pa****************************@gmail.com>,
Robert Gamble <rg*******@gmail.com> wrote:
On Tue, 16 Nov 2004 18:05:11 -0800, Old Wolf wrote:
For the following code:

int main(void)
{
short a[100], *ptr;

ptr = a + 100;
ptr -= 0;
}

a runtime bounds-checker I use gives an error "Pointer
underrun" on the ptr -= 0 line. Is there something wrong
with that statement?
I don't see anything wrong with any part of the code.


Nor do I. One past the end of an array is a valid pointer for any use
other than dereferencing it, and adding or subtracting 0 to/from a valid
pointer should be a no-op (and, in particular, give you back a (the same)
valid pointer).

Perhaps for some reason your bounds-checker does not like the statement
before the one in question where you assign ptr to point to one past the
last element of a. Do you experience the same error if you change it to
"ptr = a + 99"?


If that's the case, then the bounds-checker is wrong. It should only
trap if the OP's code tries to dereference ptr, not if it subtracts a
nonnegative value from it. (Well, nonnegative and not greater than 100.)
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca

Silly, yes. But have we shown that it is unhelpful?
--Daniel Fox in comp.lang.c
Nov 14 '05 #7
In <84**************************@posting.google.com > ol*****@inspire.net.nz (Old Wolf) writes:
For the following code:

int main(void)
{
short a[100], *ptr;

ptr = a + 100;
ptr -= 0;
}

a runtime bounds-checker I use gives an error "Pointer
underrun" on the ptr -= 0 line. Is there something wrong
with that statement?


Nope, the standard explicitly allows it:

If both the pointer operand and the result point to elements
of the same array object, or one past the last element of the
array object, the evaluation shall not produce an overflow;

The definition of C makes *reliable* bound checking at run time incredibly
difficult, but your case can be trivially handled by a run time bound
checker.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #8

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

Similar topics

1
by: Jesse O | last post by:
I have two date fields, start_date and end_date. I'd like to subtract the two dates, and come up with a number (the number of difference between the two dates). What function is there to do...
8
by: simpleman | last post by:
Hi, I have an assignment that requires me to subtract two very large unsigned integers using linked list.AFAIK I have to manipulate data as character. But I dont know how to get input into the...
11
by: Laery | last post by:
Hi, I'm currently adding a new module to an old borland C3.1 application (dos). And I need to calculate a date by subtracting the number of days from a given date. I know I could use an...
8
by: Ifollowhim | last post by:
I have a (installation) date in a table that I put in manually. It is the date we want to complete a job. In a form that uses data from that table I want to add a text box that is bound to the...
31
by: Spiro Trikaliotis | last post by:
Hello, I have a question regarding subtracting a pointer from another one. Assume I have two pointers p1 and p2, which both point to a memory area obtained with malloc(). Assume p1 = p2 + some...
3
by: Martin Joergensen | last post by:
Hi, Just a small problem (about taken the previous unsigned value in a 1D- array and copying it to the current value). Suppose there's an outer loop over the line shown below, so...
2
by: Paulers | last post by:
hello all, how does one go about subtracting seconds from a DateTime object? for example I have a date time of "06/04/2007 10:31" and I need to subtract 12 seconds from that. all help is...
2
by: parkc | last post by:
I want to return the difference number of records. Here are the queries: --subtracting columns with columns and descriptions (columns - columns and descriptions) --difference of 30 records...
3
by: nicekul | last post by:
I am having problems subtracting from unitsInStock for all records on my table, for instance; I have Quantity sold, Unit Price,UnitsInStock,Total sales, Stock Balance. I declared Dim num1 Dim...
14
by: Krumble Bunk | last post by:
Hi all, I have an elementary question, of which I never grasped, and i'd like to finally put it to rest. I have the following code, which although makes use of the string library, and could...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: 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: 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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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

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.