Why there is difference in the output of pointer size between the C code and the Arduino IDE
            Asked
            
        
        
            Active
            
        
            Viewed 166 times
        
    -2
            
            
        - 
                    1Pointer sizes are system and binary dependent. Usually either 8 bytes or 4 bytes which looks like the case respectively in your examples. – kaylum Mar 10 '22 at 19:40
- 
                    Yes I think that. Is there any way to make them the same size? – lena Mar 10 '22 at 19:45
- 
                    1Firstly, why would you want to make them the same size? If you have a 32 bit processor then it can't run a 64 bit binary. Some 64 bit systems can run 32 bit binaries. If you really must then you can build a 32 bit binary using the appropriate compile flags (e.g `-m32`). – kaylum Mar 10 '22 at 19:48
- 
                    Does that mean if I run the C code on a 32-bit operating system, I will get a result similar to esp32? because the cpu of esp32 is 32-bit? – lena Mar 10 '22 at 20:02
- 
                    1Does this answer your question? [Different int sizes on my computer and Arduino](https://stackoverflow.com/questions/3294816/different-int-sizes-on-my-computer-and-arduino) – gre_gor Mar 11 '22 at 09:30
2 Answers
1
            
            
        I will simplify your problem:
In your computer:
int *int_pointer;
printf("Size of int_pointer: %d", sizeof(int_pointer)); // Output: 8
In your ESP32:
int *int_pointer;
printf("Size of int_pointer: %d\n", sizeof(int_pointer)); // Output: 4
That is the difference.
A pointer stores a memory address so its size will be (at least) the same as the size of an address in its respective processor.
- Your computer is a 64-bit system -> The size of each address is 8 bytes -> The pointer needs to have 8 bytes to be able to store it.
- Your ESP32 is a 32-bit system -> The size of each address is 4 bytes -> The pointer only needs to have 4 bytes to be able to store it.
 
    
    
        KyreX
        
- 106
- 5
- 
                    Can I increase the size of pointer in esp32? in any way ? I need the same output – lena Mar 10 '22 at 20:06
- 
                    As far as I know, you can't. At most, you can either compile the program on a 32-bit system or force it using the option `-m` as @kaylum said. – KyreX Mar 10 '22 at 22:29
0
            
            
        This stuct
typedef struct str
{  
  int* array; 
  int y;
  int z;
} str;
contains a pointer (int * array). The size of pointers depends on where the code is running. On a 32 bit machine they will be 4 bytes, on a 64 bit machine 8. There can be other sizes to (2 - pdp 11, 6 - some mainframes)
So the sizeof operator will return different values on different systems
 
    
    
        pm100
        
- 48,078
- 23
- 82
- 145
- 
                    Can I increase the size of pointer in esp32? in any way ? I need the same output – lena Mar 10 '22 at 21:27
- 
                    @kayla the only way to make them the same - and I have no idea why you would want them the same ,its meaningless - is to run the c code that gives 16 as the output in 32 bit mode, which may be possible depending on the platform. But this is a very odd request and I suspect you are doing something EXTREMELY wrong – pm100 Mar 10 '22 at 21:44
