Click here to Skip to main content
15,884,472 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionvolatile issue - repost Pin
Vaclav_22-May-18 5:24
Vaclav_22-May-18 5:24 
QuestionRe: volatile issue - repost Pin
Richard MacCutchan22-May-18 8:26
mveRichard MacCutchan22-May-18 8:26 
AnswerRe: volatile issue - repost Pin
CPallini22-May-18 10:11
mveCPallini22-May-18 10:11 
AnswerRe: volatile issue - repost Pin
leon de boer22-May-18 19:12
leon de boer22-May-18 19:12 
GeneralRe: volatile issue - repost Pin
CPallini22-May-18 21:39
mveCPallini22-May-18 21:39 
QuestionRe: volatile issue - repost Pin
CPallini22-May-18 10:11
mveCPallini22-May-18 10:11 
AnswerRe: volatile issue - repost Pin
Vaclav_22-May-18 11:54
Vaclav_22-May-18 11:54 
GeneralRe: volatile issue - repost Pin
leon de boer22-May-18 15:44
leon de boer22-May-18 15:44 
Your current code as posted will print some random value because you haven't set the variable base to a start value.
Specifically it will be what junk is the memory address it gets assigned, basic rules of C.

Could you please try this code
#include <stdio.h>
int main (void){
   int *base = 0;
   int offset = 1;
   volatile int *result;
   result = base + offset;
   printf("Volatile result pointer = %p\n", result);
   if ((int)result != sizeof(int)) printf("compiler is broken\n");
}

It should return 2, 4 or 8 for the result depending on the size of int on your compiler
As you are doing pointer addition the offset is defined as the sizeof(int) by C standards.

If you see the message "compiler is broken" there are 10 different gcc compilers for the Pi use another one or change version of your current.
GCC being a public domain support compiler does get bugs in versions from time to time.

As a last comment the above possible multiple answers is specifically why you shouldn't use int for hardware registers.
The registers on the Pi have to be accessed as 32bit, so the only safe way to do it is uint32_t

The following code will give an answer of 4 on EVERY C COMPILER, it is safe and portable and even works on the Pi3 64bit compilers
My advice is DO IT THIS WAY.
#include <stdint.h>
#include <stdio.h>
int main (void){
   uint32_t *base = 0;
   uint32_t offset = 1;
   volatile uint32_t *result;
   result = base + offset;
   printf("Volatile result pointer = %p\n", result);
}

In vino veritas


modified 22-May-18 22:32pm.

GeneralRe: volatile issue - repost Pin
Vaclav_22-May-18 17:44
Vaclav_22-May-18 17:44 
GeneralRe: volatile issue - repost Pin
leon de boer22-May-18 18:13
leon de boer22-May-18 18:13 
GeneralRe: volatile issue - repost SOLVED Pin
Vaclav_23-May-18 3:17
Vaclav_23-May-18 3:17 
QuestionRe: volatile issue - repost SOLVED Pin
CPallini23-May-18 3:28
mveCPallini23-May-18 3:28 
GeneralRe: volatile issue - repost SOLVED Pin
Richard MacCutchan23-May-18 3:32
mveRichard MacCutchan23-May-18 3:32 
GeneralRe: volatile issue - repost SOLVED Pin
Peter_in_278023-May-18 3:44
professionalPeter_in_278023-May-18 3:44 
GeneralRe: volatile issue - repost SOLVED Pin
leon de boer23-May-18 6:55
leon de boer23-May-18 6:55 
GeneralRe: volatile issue - repost SOLVED Pin
Vaclav_24-May-18 5:53
Vaclav_24-May-18 5:53 
GeneralRe: volatile issue - repost SOLVED Pin
supercat931-May-18 12:48
supercat931-May-18 12:48 
GeneralRe: volatile issue - repost SOLVED Pin
leon de boer4-Jun-18 21:05
leon de boer4-Jun-18 21:05 
GeneralRe: volatile issue - repost SOLVED Pin
supercat98-Jun-18 10:31
supercat98-Jun-18 10:31 
QuestionReturn a local 2d Array Pin
kinderu18-May-18 8:59
kinderu18-May-18 8:59 
AnswerRe: Return a local 2d Array Pin
Richard MacCutchan18-May-18 9:13
mveRichard MacCutchan18-May-18 9:13 
GeneralRe: Return a local 2d Array Pin
kinderu18-May-18 9:45
kinderu18-May-18 9:45 
GeneralRe: Return a local 2d Array Pin
leon de boer18-May-18 16:34
leon de boer18-May-18 16:34 
GeneralRe: Return a local 2d Array Pin
Richard MacCutchan18-May-18 20:46
mveRichard MacCutchan18-May-18 20:46 
AnswerRe: Return a local 2d Array Pin
jfbode102930-May-18 4:46
jfbode102930-May-18 4:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.