How to write a convertible code, 32 bit / 64 bit?

Question c ++. So I read the question of what the 32 bit / 64 bit program does, and the anwser it received was something like this (sorry, I cannot find the question, sometime I looked at it, and I cannot find it again :(): Until you make any "pointer assumptions", you only need to recompile it. So my question is: what are the pointer assumptions? As far as I understand, there is a 32-bit pointer and 64-bit pointers , so I believe this is due to this. Please show the difference in the code between them. Any other good habits that you should keep in mind when writing code that helps him easily convert between them are also welcome :) tho, please share examples with them

Ps. I know there is this post: How do you write code compatible with both 32-bit and 64-bit? but I wanted to say that it was something like a general without good examples, for new programmers like me. Like what is a 32-bit storage block ect. Kinda jumps to break it a little more (pun intended ^^) ds.

+5
source share
5 answers

( , ++ undefined), ++ , . ( , ) . , . , 32- 64- .

() , , :

int main()
{
  std::cout << sizeof(void*) << std::endl;
  return 0;
}

, , 32- 64- ( ). sizeof(void*) . , , , , , :

int main()
{
  int size = sizeof(void*);
  if (size != 4) {
    size = 4;
  }
  std::cout << size << std::endl;
  return 0;
}

4, , , . , int size = 4;, , .

, : .

:

  • , , ++. a char 8 , short int - 16 ..

  • ( ).

  • unsigned char* char ( ).

  • reinterpret_cast.

  • , . , -.

  • .

  • void*.

undefined . . , , 32- 64- .

+3

, sizeof() ( ), ( ).

- , , , , / , (, intptr_t).

, , , . , .

, (-, , ) , uint32_t.

+6

" " - , , . int copy_of_pointer = ptr; - int - 32- , 64- , .

, .

" ", 32- 32 64- 64-. , , . [ x86 "" "" , .]

, , , , , MOST - , , .

+3

32- 64- , C/++ , .

, , /, .

: 64- , , SDL, 64-, , .

, ELF 64- , 32-, .

? , .

/ .

, .

: 64- ELF 32- .

+1

32-/64- :

, sizeof (void *) == 4 * sizeof (char). , , ( " 20 , 80 " ), 64-, .

"-", int x = (int) & something; ( , void * ptr = (void *) some_int). , sizeof (int) == sizeof (void *). , - 32 , .

, ( // ), ; UN * X, , time_t, size_t, off_t int Windows, HANDLE, void * ..

/ (. ). C/++ , 32- 64- - - (32 x86, , , 64 x86 ). , 32- , 64- . , , , .

32- 64- (- / / ) "". , (int = 0; < 1000000; ++ i) sleep (0); , 32- 64-...

, ABI (Application Binary Interface). 64- 32- , ... "" 64- , IL32P64 (, Win64 - int long - int32_t, uintptr_t/void * - uint64_t, ) LP64 ( UN * X - int is int32_t, long is int64_t uintptr_t/void * is uint64_t), "" - , , , . 32- Linux , 64- Linux - , . , bith sizeof (struct {...}) / 32- 64- , . / /, . - 32- , . struct {char a; int b; char c, long d; double e} , 64 , . (char, int, long ..), , , , / , size_t, off_t, time_t, HANDLE, , /union/class... - ,

, , . (SSE/SSE2/...); 32 64 () , ; , , , , , SSE2, 32- , / , 64- .

, 32- 64-, / ; , , " , 32 ", , / / , , .. - 64 , "" . 64- , , - " ", -32- .

, , / , , , , /. 32-, 64- , 64-? - , / , 64- ? , 32- ? 32- ? , ? , - " "...

+1
source

All Articles