Author Topic: should this compile ?  (Read 4248 times)

0 Members and 1 Guest are viewing this topic.

Offline snarkysparkyTopic starter

  • Frequent Contributor
  • **
  • Posts: 441
  • Country: us
should this compile ?
« on: December 27, 2019, 02:52:30 pm »
seems like this should not be allowed to compile...   

Code: [Select]

void event_sti_set(enum event_sti_number num, unsigned long *eventp)
{
eventp[0] |= event_conv_table[num].events[0];
        eventp[1] |= event_conv_table[num].events[1];
}






I think this would compile but the intention would be clearer.
Code: [Select]
*(eventp + 0)  |= event_conv_table[num].events[0];
*(eventp + 1)  |= event_conv_table[num].events[1];



What do you "c" experts think??
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17842
  • Country: fr
Re: should this compile ?
« Reply #1 on: December 27, 2019, 02:59:51 pm »
This is exactly the same, and both are conforming C. The a[k] operator has the exact same meaning as *(a + k). Guaranteed.

 

Offline ralphrmartin

  • Frequent Contributor
  • **
  • Posts: 519
  • Country: gb
    • Me
Re: should this compile ?
« Reply #2 on: December 27, 2019, 05:02:10 pm »
This is exactly the same, and both are conforming C. The a[k] operator has the exact same meaning as *(a + k). Guaranteed.

And a serious design flaw, in my opinion. Any intelligent student who "knows" that CPUs address memory in bytes does the intelligent thing, and if they have a array of 32 bit integers (say), "knows" that the next such integer in the array is 4 bytes further along so writes *(a + 4) instead of *(a+1) for the next integer. It's always the smart students who do this. Both the language designer and the student try to fix the same issue, so it gets fixed twice.
 

Offline GromBeestje

  • Frequent Contributor
  • **
  • Posts: 310
  • Country: nl
Re: should this compile ?
« Reply #3 on: December 27, 2019, 05:28:05 pm »
They are equivalent. I don't think the intention would be clearer in one syntax or the other. I'd say it's a matter of personal preference.
 

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1803
  • Country: us
Re: should this compile ?
« Reply #4 on: December 27, 2019, 05:34:51 pm »
seems like this should not be allowed to compile...   
Code: [Select]

void event_sti_set(enum event_sti_number num, unsigned long *eventp)
{
eventp[0] |= event_conv_table[num].events[0];
        eventp[1] |= event_conv_table[num].events[1];
}
What do you "c" experts think??
What makes you think it shouldn't compile? I think that form is clearer and more idiomatic C than the second form.

Now, what would really bend your noodle (and I don't recommend) is this would also work:
Code: [Select]

void event_sti_set(enum event_sti_number num, unsigned long *eventp)
{
0[eventp] |= event_conv_table[num].events[0];
        1[eventp] |= event_conv_table[num].events[1];
}
because as SiliconWizard and blueskull mention, a[c] === *(a+c), and addition is commutative.
 
The following users thanked this post: newbrain

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 6464
  • Country: nz
Re: should this compile ?
« Reply #5 on: December 27, 2019, 05:37:00 pm »
Who cares? *(a+n)=a[n], this is common knowledge.

What is not so well known is that

eventp[1] == *(eventp+1) == *(1+eventp) == 1[eventp]

 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17842
  • Country: fr
Re: should this compile ?
« Reply #6 on: December 27, 2019, 07:07:23 pm »
This is exactly the same, and both are conforming C. The a[k] operator has the exact same meaning as *(a + k). Guaranteed.

And a serious design flaw, in my opinion. Any intelligent student who "knows" that CPUs address memory in bytes does the intelligent thing, and if they have a array of 32 bit integers (say), "knows" that the next such integer in the array is 4 bytes further along so writes *(a + 4) instead of *(a+1) for the next integer. It's always the smart students who do this. Both the language designer and the student try to fix the same issue, so it gets fixed twice.

No, CPUs do not necessarily "address memory in bytes" (although it's pretty common, some may not). Second, pointers ARE NOT addresses. I know some see C as very low level, but it's not that low level.
This has nothing to do with being intelligent.

Pointer arithmetics is well defined in the standard. Just learn, or avoid it altogether (which some safety-critical rules do.)
 
The following users thanked this post: newbrain

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1803
  • Country: us
Re: should this compile ?
« Reply #7 on: December 27, 2019, 07:42:37 pm »
Second, pointers ARE NOT addresses.
In practice, pointers are addresses.
 

Offline DBecker

  • Frequent Contributor
  • **
  • Posts: 328
  • Country: us
Re: should this compile ?
« Reply #8 on: December 27, 2019, 08:19:46 pm »
Second, pointers ARE NOT addresses.
In practice, pointers are addresses.

Perhaps better worded as "C language pointers are usually implemented as addresses".

Other implementations are possible.
C was developed in an era where byte-addressed processors were common, but in the minority.  Word-addressed machines saved a few bits, back when that was an edge that needed for commercial success.  A C 'char' pointer would commonly have a different implementation than a word-data-type pointer.

 

Offline GromBeestje

  • Frequent Contributor
  • **
  • Posts: 310
  • Country: nl
Re: should this compile ?
« Reply #9 on: December 27, 2019, 08:43:54 pm »
Second, pointers ARE NOT addresses.
In practice, pointers are addresses.

Perhaps better worded as "C language pointers are usually implemented as addresses".

Other implementations are possible.
C was developed in an era where byte-addressed processors were common, but in the minority.  Word-addressed machines saved a few bits, back when that was an edge that needed for commercial success.  A C 'char' pointer would commonly have a different implementation than a word-data-type pointer.

And a byte is not necessary 8 bits in length, even though that's been the standard for many decades. 
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3671
  • Country: gb
Re: should this compile ?
« Reply #10 on: December 27, 2019, 11:57:40 pm »
And a serious design flaw, in my opinion. Any intelligent student who "knows" that CPUs address memory in bytes does the intelligent thing, and if they have a array of 32 bit integers (say), "knows" that the next such integer in the array is 4 bytes further along so writes *(a + 4) instead of *(a+1) for the next integer. It's always the smart students who do this.

Not smart enough to read a book on the C language apparently.
 
The following users thanked this post: donotdespisethesnake, newbrain

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 22435
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: should this compile ?
« Reply #11 on: December 28, 2019, 12:09:18 am »
And a byte is not necessary 8 bits in length, even though that's been the standard for many decades.

Interesting to note that C refuses to standardize some operations (leaving them "implementation dependent" instead), because 1s-complement machines are still covered.  Under/overflow and shift, for example.

Though I forget if non-8-bit-byte or word machines share a similar historical mandate.

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17842
  • Country: fr
Re: should this compile ?
« Reply #12 on: December 28, 2019, 12:22:54 am »
And a byte is not necessary 8 bits in length, even though that's been the standard for many decades.

Interesting to note that C refuses to standardize some operations (leaving them "implementation dependent" instead), because 1s-complement machines are still covered.  Under/overflow and shift, for example.

I think you mean operations on signed integers (OK probably obvious from the "1s-complement", but just to make things clear). Overflow is fully defined with unsigned integers, and just rolls over (which is basically OP (mod 2^N) ).

The situation with signed integers is pretty annoying. Of course what it means is that if you want to write portable code, never make your signed integers "overflow".
I've seen a lot of code making assumptions about this (which happened to work with come compiler on some machine), and that can break beautifully on some other platform...

Though I forget if non-8-bit-byte or word machines share a similar historical mandate.

Yep. Quoting the standard, byte is defined like so:
Quote
byte
addressable unit of data storage large enough to hold any member of the basic character set of the execution environment

and:
Quote
character
bit representation that fits in a byte

The standard associates bytes and "characters", so this completely depends on the underlying "execution environment".
We had to wait until C99 to get fixed-size integers.

Note that this definition of integers as "large enough to hold at least xxx" is not specific to C. The same thing happens with many other "high-level" languages. Interestingly, Pascal and some derivatives cared to define a BYTE (8-bit) type outside of the "CHAR", whereas the typical INTEGER (and I think CHAR) would be treated as in C (with no fixed defined size).
 

Online cv007

  • Super Contributor
  • ***
  • Posts: 1071
Re: should this compile ?
« Reply #13 on: December 28, 2019, 12:55:04 am »
>I think this would compile but the intention would be clearer.

Many times the array version is clearer, so use when needed as there is nothing wrong in doing so.

int main(int argc, char* argv[]){ // -v = verbose
    if( argc > 1 && argv[1][0] == '-' && argv[1][1] == 'v' ) printf("Hello world, how ya doing\n");
    else printf("hi\n");
}

int main(int argc, char* argv[]){
    if( argc > 1 && **(argv+1) == '-' && *(*(argv+1)+1) == 'v' ) printf("Hello world, how ya doing\n");
    else printf("hi\n");
}

https://godbolt.org/z/GoJFVH
I would say the array access is easier to understand. In many cases that is so.


Once you figure out what is 'the same as',  then you have choices and there is usually a better one among them but at least you can choose which one makes the most sense at the point of use. Also note when doing addition or array access on these things, the addition/offset is using the size of the underlying type which can get you in trouble when you start doing math on these numbers.
 

Offline jonroger

  • Regular Contributor
  • *
  • Posts: 72
  • Country: us
Re: should this compile ?
« Reply #14 on: December 28, 2019, 01:05:36 am »
Code: [Select]
void event_sti_set(enum event_sti_number num, unsigned long *eventp)
{
eventp[0] |= event_conv_table[num].events[0];

I agree that consistency is clearer.   For the above, use "unsigned long eventp[]" as the parameter.
I am available for custom hardware/firmware development.
 

Offline andersm

  • Super Contributor
  • ***
  • Posts: 1198
  • Country: fi
Re: should this compile ?
« Reply #15 on: December 28, 2019, 01:56:10 am »
Interesting to note that C refuses to standardize some operations (leaving them "implementation dependent" instead), because 1s-complement machines are still covered.  Under/overflow and shift, for example.
C++20 mandates two's complement, and finally specifies that right-shifts are sign-extending (signed overflow remains undefined behaviour). It looks likely that these changes will be adopted by the next C standard as well.
 
The following users thanked this post: T3sl4co1l, newbrain

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17842
  • Country: fr
Re: should this compile ?
« Reply #16 on: December 28, 2019, 02:46:14 am »
Interesting to note that C refuses to standardize some operations (leaving them "implementation dependent" instead), because 1s-complement machines are still covered.  Under/overflow and shift, for example.
C++20 mandates two's complement, and finally specifies that right-shifts are sign-extending (signed overflow remains undefined behaviour). It looks likely that these changes will be adopted by the next C standard as well.

Yeah, that makes sense for the right shift, not just because it's expected by most, but also because it makes it more consistent with the '/' operator (meaning that with signed integers and this rule, n >> m would be equivalent to n / 2^m)...
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4671
  • Country: us
Re: should this compile ?
« Reply #17 on: December 28, 2019, 06:21:33 am »
Quote
CPUs do not necessarily "address memory in bytes"
CPUs that don't address memory in bytes are a bit hellish to write C compilers for, or use C once you do.
Especially, if you want to run "standard" C applications.
 

Offline GromBeestje

  • Frequent Contributor
  • **
  • Posts: 310
  • Country: nl
Re: should this compile ?
« Reply #18 on: December 28, 2019, 09:25:11 am »
Think about the x86 Real Mode memory model
 

Offline DBecker

  • Frequent Contributor
  • **
  • Posts: 328
  • Country: us
Re: should this compile ?
« Reply #19 on: December 29, 2019, 12:44:50 am »
Interesting to note that C refuses to standardize some operations (leaving them "implementation dependent" instead), because 1s-complement machines are still covered.  Under/overflow and shift, for example.
C++20 mandates two's complement, and finally specifies that right-shifts are sign-extending (signed overflow remains undefined behaviour). It looks likely that these changes will be adopted by the next C standard as well.

My opinion: it might be about the right time to do this.  Perhaps. A decade ago would have been "too soon".  I'm not sure that anyone is going to see a performance gain from the change, and incompatibility has a cost.

C struck a very useful balance between high- and low-level languages.  It could be run on most extant architectures when it was developed, even those that today we consider very odd.  And it did so in a way that that barely had an impact on writing for what later became the canonical architecture... VAX.

Pretty much the only aspect of common instruction sets that C made inaccessible by supporting a more abstract number encoding model was the ability to effectively access the carry status bit.




 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4671
  • Country: us
Re: should this compile ?
« Reply #20 on: December 29, 2019, 02:49:29 am »
Quote
Quote
a bit hellish to write C compilers for, or use C once you do.
Think about the x86 Real Mode memory model
Well, yes, exactly.  "Near" and "far" pointers, and multiple "models" for compilation.  Functions to "normalize" pointers so that you could tell that 100:0 and 0:1000 were "equal" (did I get that right?  It's been a long time.)  And yet, still "byte addressable" in most ways; nothing nearly so bad as a DEC-10 that would normally store strings as 5 7-bit bytes in an (addressable) 36bit word.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->