Are these programs correct ?
#include <stdio.h>
unsigned char a[2] = {1,2};
int main(void) {
unsigned char j;
for(j=1; j<=2; ++j)
printf("%u\n", *( a+j-1 ));
return 0; }
#include <stdio.h>
unsigned char a[2] = {1,2};
int main(void) {
unsigned char j;
for(j=1; j<=2; ++j)
printf("%u\n", *( a-1+j ));
return 0; }
#include <stdio.h>
unsigned char a[2] = {1,2};
int main(void) {
unsigned char j;
for(j=1; j<=2; ++j)
printf("%u\n", *( a+(j-1) ));
return 0; }
rot-13 spoiler: bayl gur ynfg bar vf pbeerpg.
One compiler I use generates correct and tight code for the
second form; but it fails for *(-1+a+j) [not due to an
architectural restriction, but due to a plain compiler bug].
Isn't it a shame C programming is so complex ? Given that on
most platforms, pointer arithmetic is associative, and so many
programs out there assume it blindly, is not it time for a
future C standard to waive these restrictions on pointer
associativity in at least a branch of the standard, say with
a predefined constant __ASSOCIATIVE_POINTERS__ ?
Francois Grieu
[reposted with fixed spoiler !] 6 2250
Francois Grieu <fg****@francenet.fr> a écrit*: Isn't it a shame C programming is so complex ?
Yes it is, but is there a better solution ?
Given that on most platforms, pointer arithmetic is associative, and so many programs out there assume it blindly, is not it time for a future C standard to waive these restrictions on pointer associativity in at least a branch of the standard, say with a predefined constant __ASSOCIATIVE_POINTERS__ ?
Did you thought to all implications of your proposition.
It means that you should define a notion of something like
'non-expression', because, you goal for associativity
is to be able to write
char*p ="foo";
*(p-1+1)
It means that p-1 is no more an expression (invalid),
but p+1-1 is one...
That is to say, p-1+1 does not mean (p-1)+1 but
p+(-1+1).
And what about (--p+1), (-1+p+1), (1+p-1) ?
Marc Boyer
Francois Grieu wrote: Are these programs correct ?
#include <stdio.h> unsigned char a[2] = {1,2}; int main(void) { unsigned char j; for(j=1; j<=2; ++j) printf("%u\n", *( a+j-1 )); return 0; }
No problems here, it is acceptable to calculate a pointer to one element
past the end of an array.
#include <stdio.h> unsigned char a[2] = {1,2}; int main(void) { unsigned char j; for(j=1; j<=2; ++j) printf("%u\n", *( a-1+j )); return 0; }
This one is wrong, it is not acceptable to calculate a pointer to one
element before the beginning of an array.
#include <stdio.h> unsigned char a[2] = {1,2}; int main(void) { unsigned char j; for(j=1; j<=2; ++j) printf("%u\n", *( a+(j-1) )); return 0; }
This one is fine, you never calculate an invalid pointer at all.
rot-13 spoiler: bayl gur ynfg bar vf pbeerpg.
Undeciphered: only the last one is correct.
No, the first one is fine too.
One compiler I use generates correct and tight code for the second form; but it fails for *(-1+a+j) [not due to an architectural restriction, but due to a plain compiler bug].
There is no "correct" code for the second; both the second and *(-1+a+j)
have undefined behaviour, for the same reason.
Isn't it a shame C programming is so complex ? Given that on most platforms, pointer arithmetic is associative, and so many programs out there assume it blindly, is not it time for a future C standard to waive these restrictions on pointer associativity in at least a branch of the standard, say with a predefined constant __ASSOCIATIVE_POINTERS__ ?
When you speak of the associativity of "pointer arithmetic", you are
really conflating two things: integer arithmetic and pointer arithmetic.
Pointer arithmetic cannot be associative, because the syntax does not
even allow constructs like
pointer1 op pointer2 op pointer3
(where both 'op's are the same operator). If op were '+', then it is a
constraint violation as two pointers cannot be added together. If op
were '-', the result of two pointers being subtracted is an integer,
from which you cannot subtract a pointer.
Remember that there are no restrictions on the behaviour of a program
with undefined behaviour; it may behave as you expect, or not.
--
Simon.
On 2005-11-14, Simon Biber <ne**@ralmin.cc> wrote: Francois Grieu wrote: Are these programs correct ?
#include <stdio.h> unsigned char a[2] = {1,2}; int main(void) { unsigned char j; for(j=1; j<=2; ++j) printf("%u\n", *( a+j-1 )); return 0; }
No problems here, it is acceptable to calculate a pointer to one element past the end of an array.
#include <stdio.h> unsigned char a[2] = {1,2}; int main(void) { unsigned char j; for(j=1; j<=2; ++j) printf("%u\n", *( a-1+j )); return 0; }
This one is wrong, it is not acceptable to calculate a pointer to one element before the beginning of an array.
Note that a-(1-j), however, would be correct, i believe.
[more stuff snipped] Isn't it a shame C programming is so complex ? Given that on most platforms, pointer arithmetic is associative, and so many programs out there assume it blindly, is not it time for a future C standard to waive these restrictions on pointer associativity in at least a branch of the standard, say with a predefined constant __ASSOCIATIVE_POINTERS__ ?
When you speak of the associativity of "pointer arithmetic", you are really conflating two things: integer arithmetic and pointer arithmetic. Pointer arithmetic cannot be associative, because the syntax does not even allow constructs like pointer1 op pointer2 op pointer3 (where both 'op's are the same operator). If op were '+', then it is a constraint violation as two pointers cannot be added together. If op were '-', the result of two pointers being subtracted is an integer, from which you cannot subtract a pointer.
Remember that there are no restrictions on the behaviour of a program with undefined behaviour; it may behave as you expect, or not.
It would be simple enough, though, to force compilers to collate all the
integer components of a pointer arithmetic expression into a single
value which is then added to the pointer, which is really what he is
requesting.
i.e. for arbitrary
i1-i2+p+i3-i4, force that to become p+(i1-i2+i3-i4)
Jordan Abel wrote: It would be simple enough, though, to force compilers to collate all the integer components of a pointer arithmetic expression into a single value which is then added to the pointer, which is really what he is requesting.
i.e. for arbitrary
i1-i2+p+i3-i4, force that to become p+(i1-i2+i3-i4)
Conversely, it's simple enough to ask programmers to follow this rule
anyway, always putting the pointer at the beginning of such an expression.
Anyone who wrote i1-i2+p+i3-i4 in a real program should be shot. Make it
clear and self-descriptive. Having four different offsets added to a
pointer is a little excessive.
My worst so far is something like:
unsigned short *
get(unsigned short *array, int x, int y, int z)
{
return array + x
+ y * mapWidth
+ z * mapWidth * mapHeight;
}
Actually, it was C++ code and used references instead of pointers, but
you get the idea.
--
Simon.
Simon Biber wrote: Jordan Abel wrote:
i1-i2+p+i3-i4, force that to become p+(i1-i2+i3-i4)
Conversely, it's simple enough to ask programmers to follow this rule anyway, always putting the pointer at the beginning of such an expression.
Actually,
placing the pointer after a sequence of integer additions
avoids problems better.
(i1 + i2 + p) only depends on the final sum
(p + i1 + i2) also depends on i1 being
neither negative nor excessively large.
--
pete
pete <pf*****@mindspring.com> writes: Simon Biber wrote: Jordan Abel wrote:
i1-i2+p+i3-i4, force that to become p+(i1-i2+i3-i4)
Conversely, it's simple enough to ask programmers to follow this rule anyway, always putting the pointer at the beginning of such an expression.
Actually, placing the pointer after a sequence of integer additions avoids problems better.
(i1 + i2 + p) only depends on the final sum (p + i1 + i2) also depends on i1 being neither negative nor excessively large.
p + (i1 + i2) is an improvement on both.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ceo |
last post by:
Hi,
Following is a program that doesn't give the expected output, not sure
what's wrong here. I'm adding the size of derived class to the base
class pointer to access the next element in the...
|
by: Alex Fraser |
last post by:
From searching Google Groups, I understand that void pointer arithmetic is a
constraint violation, which is understandable. However, generic functions
like qsort() and bsearch() must in essence do...
|
by: Pieter Claassen |
last post by:
Ok, I have something that works, but I don't understand why.
I use libpcap to get data of the wire and then initially I casted the
packet to ethernet, then marched along the memory in chunks of...
|
by: Francois Grieu |
last post by:
Are these programs correct ?
#include <stdio.h>
unsigned char a = {1,2};
int main(void) {
unsigned char j;
for(j=1; j<=2; ++j)
printf("%u\n", *( a+j-1 ));
return 0; }
|
by: randomtalk |
last post by:
hello everyone!
Well, recently i've been trying to pick up c and see what is pointer
all about (been programming in lisp/python for the better part of my
last two years)..
mmm.. I'm currently...
|
by: barikat |
last post by:
int a;
int *Ptr1, *Ptr2;
Ptr1 = a;
Ptr1++;
Ptr2 = a;
printf("Ptr1 : %p\n", Ptr1);
printf("Ptr2 : %p\n\n", Ptr2);
|
by: Bill Reid |
last post by:
Bear with me, as I am not a "professional" programmer, but I was
working on part of program that reads parts of four text files into
a buffer which I re-allocate the size as I read each file. I...
|
by: jerry |
last post by:
I need to modify the code of a command-line tool. The source code
starts out like this:
int main(int argc, char *argv)
{
int ch, A, B ;
while ((ch = getopt(argc, argv, "AB")) != -1)...
|
by: =?Utf-8?B?RGlwZXNoX1NoYXJtYQ==?= |
last post by:
Hi all,
I am porting my code in VC++ to VC.net i.e managed. I have seen somewhere
that i need to convert my char* to String*, but String * doesnt perform
pointer arithmetic like String* p; *p++='...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
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....
|
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...
|
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...
| |