user-0 提问: C 语言是否可以实现在固定的虚拟地址分配内存

回复:
user: user-1
content: 引用 8 楼 Z_R_Z_798205 的回复:引用 7楼赵4老师 的回复:听说指针操作符可以重载的。请提供实例，谢谢 不过操作符重载是C++的内容。 https://www.baidu.com/s?wd=CPP%20%E6%8C%87%E9%92%88%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD&rsv_spt=1&rsv_iqid=0xe9b3121000006af4&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=0&oq=cpp%20%E6%8C%87%E9%92%88%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD&rsv_t=3177BPpKGrWD59QCy79HFeNk%2FZmaF58%2Bj5lkLxnb9BQrl3ho63z5aXH%2FKEbl7uEkK1q6&rsv_pq=bc51d27500007875&inputT=44844&rsv_sug4=44844  运算符重载是语法糖。 语法糖越甜，编译调试查错越苦！ 把有限的生命浪费在品尝/品鉴无穷多种的语法糖中，我认为不值当。

user: user-0
content: 引用 7楼赵4老师 的回复:听说指针操作符可以重载的。请提供实例，谢谢

user: user-1
content: 听说指针操作符可以重载的。

user: user-0
content: 引用 13 楼 a174787252 的回复:逻辑地址 和 物理地址 你要的是哪个?? 我要匿名映射的虚拟地址(这里虚拟地址就是你所说的逻辑地址) 不要文件映射的虚拟地址.

user: user-2
content: 逻辑地址 和 物理地址 你要的是哪个??

user: user-0
content: 引用 11楼sand6174 的回复:可以是可以，但我不知道有什么api。自己写系统就可以，内存分页就有。我想看看标准 C 库能否支持我想要的功能，不能的话，我就只能自己写代码从内核去支持这个功能了

user: user-3
content: 可以是可以，但我不知道有什么api。自己写系统就可以，内存分页就有。

user: user-0
content: 引用 9楼赵4老师 的回复:引用 8 楼 Z_R_Z_798205 的回复:[quote=引用 7楼赵4老师 的回复:]听说指针操作符可以重载的。请提供实例，谢谢 不过操作符重载是C++的内容。 https://www.baidu.com/s?wd=CPP%20%E6%8C%87%E9%92%88%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD&rsv_spt=1&rsv_iqid=0xe9b3121000006af4&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=0&oq=cpp%20%E6%8C%87%E9%92%88%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD&rsv_t=3177BPpKGrWD59QCy79HFeNk%2FZmaF58%2Bj5lkLxnb9BQrl3ho63z5aXH%2FKEbl7uEkK1q6&rsv_pq=bc51d27500007875&inputT=44844&rsv_sug4=44844  运算符重载是语法糖。 语法糖越甜，编译调试查错越苦！ 把有限的生命浪费在品尝/品鉴无穷多种的语法糖中，我认为不值当。 [/quote]谢谢你的回复，我只需要针对运行在 Linux 下的 C 解决办法，谢谢

user: user-0
content: 引用 3 楼 zhao4zhong1 的回复:void * mmap(void *start, size_t length, int prot , int flags, int fd, off_t offset);  int mlock(const void *addr, size_t len); 我使用了该函数后,该函数只能是文件映射,不能实现匿名映射的方法.这样的结果就是当我直接使用这部分内存时,不能通过指针直接使用,而只能使用read 或 write 区进行操作. 我想要的就是使用匿名内存操作的方式.通过指针直接操作内存. 谢谢赵4老师

user: user-0
content: 引用 3 楼 zhao4zhong1 的回复:void * mmap(void *start, size_t length, int prot , int flags, int fd, off_t offset);  int mlock(const void *addr, size_t len); 已经试了,可以符合要求,谢谢

user: user-4
content: 虽然是虚拟模式，显存地址也还是可以直接拿来用的

user: user-1
content: void * mmap(void *start, size_t length, int prot , int flags, int fd, off_t offset);  int mlock(const void *addr, size_t len);

user: user-0
content: 引用 1楼赵4老师 的回复:VirtualAllocEx The VirtualAllocEx function reserves, commits, or both, a region of memory within the virtual address space of a specified process. The function initializes the memory it allocates to zero, unless the MEM_RESET flag is set.   The difference between the VirtualAllocEx function and the VirtualAlloc function is that VirtualAlloc allocates memory within the address space of the calling process, while VirtualAllocEx lets you specify a process.   LPVOID VirtualAllocEx(   HANDLE hProcess,  // process within which to allocate memory   LPVOID lpAddress, // desired starting address of allocation   DWORD dwSize,     // size, in bytes, of region to allocate   DWORD flAllocationType,                     // type of allocation   DWORD flProtect   // type of access protection );   Parameters hProcess  Handle to a process. The function allocates memory within the virtual address space of this process.  You must have PROCESS_VM_OPERATION access to the process. If you do not, the function fails.   lpAddress  Pointer that specifies a desired starting address for the region of pages that you want to allocate.  If you are reserving memory, the function rounds this address down to the nearest 64-kilobyte boundary.   If you are committing memory that is already reserved, the function rounds this address down to the nearest page boundary. To determine the size of a page on the host computer, use theGetSystemInfo function.   If lpAddress is NULL, the function determines where to allocate the region.   dwSize谢谢你的答复，但我的代码跑在 Linux 下。谢谢

user: user-1
content: VirtualAllocEx The VirtualAllocEx function reserves, commits, or both, a region of memory within the virtual address space of a specified process. The function initializes the memory it allocates to zero, unless the MEM_RESET flag is set.   The difference between the VirtualAllocEx function and the VirtualAlloc function is that VirtualAlloc allocates memory within the address space of the calling process, while VirtualAllocEx lets you specify a process.   LPVOID VirtualAllocEx(   HANDLE hProcess,  // process within which to allocate memory   LPVOID lpAddress, // desired starting address of allocation   DWORD dwSize,     // size, in bytes, of region to allocate   DWORD flAllocationType,                     // type of allocation   DWORD flProtect   // type of access protection );   Parameters hProcess  Handle to a process. The function allocates memory within the virtual address space of this process.  You must have PROCESS_VM_OPERATION access to the process. If you do not, the function fails.   lpAddress  Pointer that specifies a desired starting address for the region of pages that you want to allocate.  If you are reserving memory, the function rounds this address down to the nearest 64-kilobyte boundary.   If you are committing memory that is already reserved, the function rounds this address down to the nearest page boundary. To determine the size of a page on the host computer, use theGetSystemInfo function.   If lpAddress is NULL, the function determines where to allocate the region.   dwSize