• AVR Freaks

Hot!Pointer not initialized (correctly?)

Page: < 1234 > Showing page 2 of 4
Author
LdB_ECM
Senior Member
  • Total Posts : 159
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/03 08:17:50 (permalink)
-1 (1)
Not in C it has never been like that
https://www.geeksforgeeks...r-array-array-pointer/
 
Note : The pointer that points to the 0th element of array and the pointer that points to the whole array are totally different.

 
That is going to be how that barren pointer is interpreted by the compiler.
 
 
#21
andersm
Super Member
  • Total Posts : 2672
  • Reward points : 0
  • Joined: 2012/10/07 14:57:44
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/03 08:25:35 (permalink)
0
LdB_ECMSeriously basic C

Indeed.
ISO/IEC 9899:TC3A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero).

#22
LdB_ECM
Senior Member
  • Total Posts : 159
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/03 08:31:50 (permalink)
-1 (1)
There are no square brackets [] in the line as written .. my complaint and how I fixed it I didn't write a barren pointer :-)
 
The compiler is getting it wrong for a reason because it can't understand what is being asked. We can argue all day but the problem remains fix it so the compiler does the right thing and the OP can move on.
 
Right or wrong, I know a few compilers that would mince that line and hence I would never write it ... don't be lazy and write the couple of extra characters.
post edited by LdB_ECM - 2019/09/03 08:39:25
#23
andersm
Super Member
  • Total Posts : 2672
  • Reward points : 0
  • Joined: 2012/10/07 14:57:44
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/03 08:43:57 (permalink)
+1 (1)
The key is this equivalence:
E1[E2] is identical to (*((E1)+(E2)))

However, the definitions of the additive operators are also pretty clear:
When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integer expression. In other words, if the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N (where N has the value n) point to, respectively, the i+n-th and i−n-th elements of the array object, provided they exist.

post edited by andersm - 2019/09/03 08:46:11
#24
LdB_ECM
Senior Member
  • Total Posts : 159
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/03 08:51:23 (permalink)
-1 (1)
It really does not matter to me ... I have used compilers that don't obey what you believe is a standard.
 
Thus I have offered the OP a way to write the code that will get thru on those compilers and will likely work on XC8.
 
It will either fix the problem or the XC8 compiler has some random bug which is which means he is no worse off for the cost of 4 characters.
post edited by LdB_ECM - 2019/09/03 08:54:45
#25
andersm
Super Member
  • Total Posts : 2672
  • Reward points : 0
  • Joined: 2012/10/07 14:57:44
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/03 09:02:01 (permalink)
+1 (1)
LdB_ECMIt really does not matter to me ... I have used compilers that don't obey what you believe is a standard.

Or maybe you've just misunderstood things? What you seem to have missed from the page you linked earlier, is that in their example the pointers are of a different type.
    // Pointer to an integer 
    int *p;  
      
    // Pointer to an array of 5 integers
    int (*ptr)[5];  
The value of (p+1) will be ((size_t)p+sizeof(int*)), while the value of (ptr+1) will be ((size_t)ptr+sizeof(int[5])). That's should not be surprising, but it also has no relevance to the OP's issue.
#26
LdB_ECM
Senior Member
  • Total Posts : 159
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/03 09:12:13 (permalink)
-1 (1)
We don't allow barren pointers in our company code repository for a reason.
You can argue whatever you believe till your are old and dead it won't change my view on the code.
I am simply avoiding a stupid argument and if you want me to say you are right then .. okay you are right, and I am wrong.
 
So following your answer I guess the OP has to wait for the patch to XC8 to work as specified and compile the line of code as written because it is correct.
#27
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11422
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/03 09:19:00 (permalink)
+1 (1)
If you claim oled_buf is a pointer then C pointer arithmetic holds and it goes to the byte after the end of the array

 
The standard is clear and there is no compiler anywhere that does what you claim.  From C99 6.3.2.3:
 
Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object

#28
LdB_ECM
Senior Member
  • Total Posts : 159
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/03 09:33:17 (permalink)
+1 (3)
Talk about flogging a dead horse .. I already said you all correct .. I submit.
 
I will say at least you quoted a C standard :-)
 
You have all solved the mystery and the problem is solved and I laid down and got my flogging.
post edited by LdB_ECM - 2019/09/03 09:37:33
#29
1and0
Access is Denied
  • Total Posts : 9982
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/03 16:40:09 (permalink)
+3 (3)
Talk about throwing a tantrum. 
 
What you have suggested in Post #13
uint8_t *oled_buf_start = &oled_buf[1];

is considered as a workaround, provided the compiler does not mess this one up too. This is simply just another way of coding the same thing. Both are valid and should generate the same code.
 
However, in this thread you have also claimed things that are wrong, and they are rebuked and corrected by others, whom I considered experts. Take their constructive criticisms and learn from them to improve your knowledge.
 
The point is this line of code from Post #1
uint8_t *oled_buf_start = oled_buf + 1;

is valid and operates identically to your code line above. If the compiler got it wrong then it is a compiler bug, and Microchip should be notified and get it fixed.
 
post edited by 1and0 - 2019/09/03 16:47:26
#30
dan1138
Super Member
  • Total Posts : 3284
  • Reward points : 0
  • Joined: 2007/02/21 23:04:16
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/03 17:12:34 (permalink)
0
1and0
If the compiler got it wrong then it is a compiler bug, and Microchip should be notified and get it fixed.



All I can say is good luck on that one. Don't hold your breath, this could take awhile.
 
Some guy submitted a bug with MPLINK in this thread that Microchip has allegedly "known" about for over a year.
#31
ric
Super Member
  • Total Posts : 24575
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Pointer not initialized (correctly?) 2019/09/03 17:30:48 (permalink)
+1 (1)
I suspect that MPASM is way down on their priority list these days.
They do seem to be a lot more active in XC8 updates.
(Still, a clear bug in MPASM like that should have been as easy fix)

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#32
1and0
Access is Denied
  • Total Posts : 9982
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/03 17:39:26 (permalink)
+1 (1)
dan1138
All I can say is good luck on that one. Don't hold your breath, this could take awhile.

Never hold my breath for Microchip. But I hope Jeff will see this. Anyway, if someone submitted a ticket, then we have done our parts at least. The ball is in their court then.
 
 
Some guy submitted a bug with MPLINK in this thread that Microchip has allegedly "known" about for over a year.

That is why I coded in absolute mode. ;)
 
#33
LdB_ECM
Senior Member
  • Total Posts : 159
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/03 17:54:45 (permalink)
-1 (1)
However, in this thread you have also claimed things that are wrong, and they are rebuked and corrected by others, whom I considered experts.

You can consider them experts, sorry I don't as they breach several coding guides I enforce. They probably say the same of me but then everyone is an expert in keyboard warrior land.
 
As you said the truth is both codes are the same ... that I think we all agree.
 
I admit I have a definite preference for which way it is written for code in my control and I ban one way even if it is legal because historically it has given issues.
 
Your experts prefer the other way and rebuke me that it is legal, fine I accepted it's legal and they prefer it that way. One has even tested it on every C compiler so I am happy it works on every known C compiler.
 
That does not remotely mean I am ever going to use that code form and learn their way ... that isn't a tantrum it is a fact.  
post edited by LdB_ECM - 2019/09/03 17:57:04
#34
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11422
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/03 18:05:24 (permalink)
+1 (1)
they prefer it that way

 
I don't see anyone "preferring" it, I just see people telling you that it is valid code and both approaches have the same effect and will most likely generate the same code.
#35
NKurzman
A Guy on the Net
  • Total Posts : 18033
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/03 18:45:51 (permalink)
+1 (1)
 
 
uint8_t *oled_buf_start = &oled_buf[1];
vs
uint8_t *oled_buf_start = oled_buf + 1;
 
IF it is a compiler bug, then trying different C constructs that do the same thing is a recommended tactic.
A single Bug may not affect them all.  Optimization bugs are very tricky this way.  slight changes can cuase the compiler to look at it differently.
But if the OP wants it fixed, then it must be submitted since Jeff has not popped in.
 
 




#36
1and0
Access is Denied
  • Total Posts : 9982
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/03 18:59:55 (permalink)
+4 (4)
LdB_ECM
 
You can consider them experts, sorry I don't as they breach several coding guides I enforce.

Yes I do, sorry it's your loss.
 

As you said the truth is both codes are the same ... that I think we all agree.


It's just that. You did not say both codes are the same. In fact,
  • In Post #13, you said, "Sorry the second line of code is junk ... different compilers based on promotion could do different things with that."
  • In Post #19, you said, "If you claim oled_buf is a pointer then C pointer arithmetic holds and it goes to the byte after the end of the array ... aka some random place."
  • In Post #23, you said, "The compiler is getting it wrong for a reason because it can't understand what is being asked."
  • In Post #23, you also said, "I know a few compilers that would mince that line and hence I would never write it."
 
 

Your experts prefer the other way and rebuke me that it is legal, fine I accepted it's legal and they prefer it that way. One has even tested it on every C compiler so I am happy it works on every known C compiler.

I also don't see anyone here "preferring" it.
 

That does not remotely mean I am ever going to use that code form and learn their way ... that isn't a tantrum it is a fact.  

It's your choice. Noone is forcing you.
#37
LdB_ECM
Senior Member
  • Total Posts : 159
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/03 22:48:58 (permalink)
-1 (1)
I did not say they were same initially totally agree, but once the shorthand was shown as a pointer to the first element then I agree they are the same. I will happily accept they don't "prefer" if it is important, can I use the words "find it acceptable".
 
Since I can't use this shorthand there is something that does intrigue me, if oled_buf is a struct
 
say something like
struct {
     uint16_t field1;
     uint32_t field2;
} oled_buf = { 0};

 
Where is the pointer set for this?
uint8_t *oled_buf_start = oled_buf + 1;

 
post edited by LdB_ECM - 2019/09/03 23:00:31
#38
1and0
Access is Denied
  • Total Posts : 9982
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/03 23:25:59 (permalink)
+2 (2)
LdB_ECM
 
... there is something that does intrigue me, if oled_buf is a struct
 
say something like
struct {
 
     uint16_t field1;
 
     uint32_t field2;
 
} oled_buf = { 0};

 
Where is the pointer set for this?
uint8_t *oled_buf_start = oled_buf + 1;


oled_buf_start is a pointer to uint8_t. oled_buf is a struct, not an uint8_t. So that initialization is not valid and it will result in an illegal types conversion error.
#39
LdB_ECM
Senior Member
  • Total Posts : 159
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/03 23:38:27 (permalink)
-1 (1)
Well I guess I agree that should be illegal but it begs the question is this illegal for same reason
 
uint16_t oled_buf[32];
uint8_t *oled_buf_start = oled_buf + 1;

#40
Page: < 1234 > Showing page 2 of 4
Jump to:
© 2019 APG vNext Commercial Version 4.5