• AVR Freaks

Hot!Pointer not initialized (correctly?)

Page: < 1234 > Showing page 3 of 4
Author
ric
Super Member
  • Total Posts : 24241
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Pointer not initialized (correctly?) 2019/09/03 23:49:00 (permalink)
+1 (1)
LdB_ECM
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;


"oled_buf + 1" points to the second element of the oled_buff array, so 2 bytes higher than the first element.
That is calculated with no knowledge of what sort of pointer is on the left side of the "=".
 

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!
#41
LdB_ECM
Senior Member
  • Total Posts : 149
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/04 00:00:30 (permalink)
-1 (1)
WOW okay so that has like a (void*) cast on it and I guess you use at your own peril like all (void*) casting.
 
Effectively it is shorthand something like
uint8_t* oled_buf_start = (void*)&oled_buf[1];

post edited by LdB_ECM - 2019/09/04 00:03:31
#42
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11404
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/04 08:25:47 (permalink)
+1 (1)
if oled_buf is a struct

 
The "decays to a pointer" rule applies for arrays, not structs or any other types.
 
WOW okay so that has like a (void*) cast on it and I guess you use at your own peril like all (void*) casting.

 
No, it is not like a void pointer.  The fact that the expression on the right of the = is evaluated without concern for what's on the left doesn't mean that anything goes.  If the pointer on the left of the = doesn't match the type of what's on the right, you'll have a warning or error, just like any other pointer assignment.
#43
1and0
Access is Denied
  • Total Posts : 9882
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/04 08:26:41 (permalink)
+2 (2)
From your latest posts, I think I see where your misunderstanding might be at.
 
If the & operator was added to your Post #38

struct oled {
    uint16_t field1;
    uint32_t field2;
} oled_buf = { 0 };
uint8_t *oled_buf_start = &oled_buf + 1;

This pointer points to the address following the end of the struct. <edit> As Jtemples just pointed out, "decay to a pointer" does not apply to structs. </edit>
 
When you add to or subtract from a pointer, the amount by which you do that is multiplied by the size of the type of the pointer. In this case, the one that's added is multiplied by sizeof(struct oled). That is, 1*sizeof(struct oled) bytes from the beginning of the struct, which is the address following the end of the struct.

Now, for your Post #39

uint16_t oled_buf[32];
uint8_t *oled_buf_start = oled_buf + 1;

This pointer points to the address of the 1-st element of the array, NOT the address "after the end of the array" as you have misunderstood before.

Also, oled_buf == &oled_buf == &oled_buf[0] and all three are equal only when oled_buf really is an array. Here, the one that's added is multiplied by sizeof(uint16_t). That is, 1*sizeof(uint16_t) bytes from the beginning of the array, which is the address of the 1-st element of the array.
 
 
<edit> I believe Andersm pointed this out in Post #26 with different examples from the page you linked earlier. 
<edit> For clarification here, 1-st element is the second element of the array, 0-th is the first element. ;)
 
post edited by 1and0 - 2019/09/04 16:02:40
#44
NorthGuy
Super Member
  • Total Posts : 5745
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/04 08:52:54 (permalink)
+2 (2)
1and0
Also, oled_buf == &oled_buf  ...  are equal only when oled_buf really is an array.



"&oled_buf" is not equivalent to "oled_buf". The former has "pointer to array" type, the later has "pointer to int8_t" type. This affects pointer arithmetics:
 
#include <stdio.h>
#include <stdint.h>

uint8_t oled_buf[32];
uint8_t *oled_buf_start = oled_buf + 1;
uint8_t *oled_buf_end = &oled_buf + 1;

void main(){
  printf("start %d\r\n",(int)oled_buf_start - (int)oled_buf);
  printf("end %d\r\n",(int)oled_buf_end - (int)oled_buf);
}


This prints:
 
start 1
end 32

 
and it also shows a warning about incompatible pointer types when assigning to oled_buf_end
 
#45
1and0
Access is Denied
  • Total Posts : 9882
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/04 09:24:15 (permalink)
0
NorthGuy
 
"&oled_buf" is not equivalent to "oled_buf". The former has "pointer to array" type, the later has "pointer to int8_t" type. This affects pointer arithmetics:
...
and it also shows a warning about incompatible pointer types when assigning to oled_buf_end

With XC8 v1.45, it generates "Warning [362]  redundant "&" applied to array" and outputs
start 1
end 1

 
#46
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11404
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/04 09:25:07 (permalink)
+1 (1)
And one more misunderstanding:
 
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.

 
"One element past the end of an array" is well-defined in C, it is not "some random place".  This is perfectly legal:
uint8_t array[10];
uint8_t *ptr = &array[10];
ptr--;   // this now points to the last element of the array.

 
Of course, you're not allowed to dereference the pointer when it is pointing outside the array, but the implementation is required to allow the pointer to point one past the end of it.
#47
NorthGuy
Super Member
  • Total Posts : 5745
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/04 10:05:13 (permalink)
+1 (1)
1and0
With XC8 v1.45, it generates "Warning [362]  redundant "&" applied to array" and outputs
start 1
end 1




That's a bug.
#48
1and0
Access is Denied
  • Total Posts : 9882
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/04 10:53:25 (permalink)
0
NorthGuy
 
That's a bug.

You're right.
 
http://c-faq.com/aryptr/aryvsadr.html
 
Let's see what Jeff and others have to say.
#49
aschen0866
Super Member
  • Total Posts : 4505
  • Reward points : 0
  • Joined: 2006/01/08 22:18:32
  • Location: San Diego
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/04 11:19:32 (permalink)
0
The end of this discussion probably goes like this ... once again

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.

#50
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3300
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/04 12:47:53 (permalink)
0
With XC16:
int arr[10];
int *x = arr + 1;
int *y = &arr + 1;
// (*y = address of an address + 1)
 
warning: initialization from incompatible pointer type
 
These warnings can be turned off, you may want the ampersand there for readability or you cannot comprehend that a block of memory can not be manipulated like a pointer.
 
The warning is: stating an address of an address, an illegal syntax in any language.
 
My 2 cents.
 
 
 
post edited by Gort2015 - 2019/09/04 12:49:09

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#51
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11404
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/04 13:00:56 (permalink)
+1 (1)
stating an address of an address, an illegal syntax in any language.

 
It's perfectly legal when you use the correct syntax:
int array[10];
int (*ptr)[10] = &array;

#52
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3300
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/04 13:28:43 (permalink)
0
That's not what I meant, I said that a chunk of memory can not be placed in a register.
 
 

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#53
mad_c
Super Member
  • Total Posts : 1202
  • Reward points : 0
  • Joined: 2010/12/12 17:48:27
  • Location: Brisbane, Australia
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/04 15:50:24 (permalink)
+5 (5)
1and0
Let's see what Jeff and others have to say.

Hi,
 
It looks like Clang (C99 mode) is correctly picking up that the two expressions have different types, but that the code generator is not translating that into different values for the assignment in the oled_buf versus &oled_buf cases. It doesn't look like the old P1 parser (C90 mode) is even picking up the difference in type.
 
@Cyber: Make sure you send in a support request with a copy of the project and device details if you have not done so already. They can take a look at it and create a case.
 
Jeff.
#54
1and0
Access is Denied
  • Total Posts : 9882
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/04 16:01:45 (permalink)
0
Thanks Jeff for popping in. Hope it gets fixed soon. ;)
 
#55
LdB_ECM
Senior Member
  • Total Posts : 149
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/04 17:25:35 (permalink)
0
My only comments are that as is, it is inconsistent and dangerous and if that is because of a bug that makes sense.As I stated it breaches our company coding policy because we have seen this all before, and the situation clearly hasn't improved 20 years down the road (2019 vs C90 spec).
 
My own personal view is it smacks of to many C++ programmers having influence on the C standards because it's nothing but a C++ shortcut it adds zero functionality to the C language. It is a bit like  x = ( ???) ? a : b syntax for "if then else" statements.
post edited by LdB_ECM - 2019/09/04 17:41:41
#56
mlp
boots too small
  • Total Posts : 814
  • Reward points : 0
  • Joined: 2012/09/10 15:12:07
  • Location: previously Microchip XC8 team
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/05 08:17:47 (permalink)
+1 (1)
LdB_ECM
breaches our company coding policy

Which you said earlier includes "no pointers".
You do realise that under the hood any array-element access is a pointer operation (array name decays to pointer-to-element, square-brackets is shorthand for pointer-and-integer arithmetic-then-dereference), right?
 
the situation clearly hasn't improved 20 years down the road (2019 vs C90 spec).

Your arithmetic needs work, too. It has been a long time since 20-years-from-C90.
 

Mark (this opinion available for hire)
#57
LdB_ECM
Senior Member
  • Total Posts : 149
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/05 21:47:31 (permalink)
-5 (7)
Actually I said "No barren pointers"  and it isn't like that C compilers came out the day after the C90 specification.
 
However your response is pretty standard for a keyboard thug on this forum who contribute nothing. Now think was it really worth even posting that junk it isn't as if it was useful .. Get a life.
#58
andersm
Super Member
  • Total Posts : 2666
  • Reward points : 0
  • Joined: 2012/10/07 14:57:44
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/05 22:57:20 (permalink)
+4 (4)
You keep going on about C90 and C++, but this is how C has always worked. Here, go read the chapter on arrays and pointers from the 1978 edition of K&R: https://archive.org/details/TheCProgrammingLanguageFirstEdition/page/n95
 
Additive operators: https://archive.org/details/TheCProgrammingLanguageFirstEdition/page/n195
Arrays, pointers, and subscripting: https://archive.org/details/TheCProgrammingLanguageFirstEdition/page/n217
 
Many coding standards forbid pointer arithmetic, since it's an easy source of bugs, but you should still know how the language works. Educate yourself instead of throwing hissy fits.
#59
NKurzman
A Guy on the Net
  • Total Posts : 17932
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: Pointer not initialized (correctly?) 2019/09/05 23:11:56 (permalink)
+2 (2)
Know who you are responding to. mlp worked for hi-tech/microchip. So he is very familiar with the C spec and how it is implemented in XC8. So he is little more than a keyboard thug.
And FYI Jeff still works for them. So it is not a bug unless he thinks it is.
#60
Page: < 1234 > Showing page 3 of 4
Jump to:
© 2019 APG vNext Commercial Version 4.5