size_t size = sizeof(int);
printf("%d\n", size);
int i;
for (i = 0; i < size; i++) {
printf("%d ", i);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码(使用gcc)outptus
4
0 1 2 3
size_t size = sizeof(int);
printf("%d\n", size);
int i;
for (i = -1; i < size; i++) {
printf("%d ", i);
}
Run Code Online (Sandbox Code Playgroud)
此代码(i初始化为-1)仅输出4并且循环中没有任何内容.
size_t size = sizeof(int);
printf("%d\n", size);
int i;
for (i = -1; i < (int) size; i++) {
printf("%d ", i);
}
Run Code Online (Sandbox Code Playgroud)
添加强制转换使代码再次运行正常.输出是
4
-1 0 1 2 3
第二个代码出了什么问题?为什么printf在任何地方都不会出错?
将 size_t 转换为 long 有什么缺点吗?因为,我正在编写一个在文件中维护 links_list 的程序。因此,我基于 size_t 遍历到另一个节点,并且还将列表总数记录为 size_t。因此,显然会有一些转换或添加 long 和 size_t。这有什么缺点吗?如果有的话,我会把所有东西都做的一样长,而不是 size_t,甚至是尺寸。请指教。
malloc 定义如下:
void *malloc(size_t size);
Run Code Online (Sandbox Code Playgroud)
http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html
size_t 定义(stddef.h):
size_t: Unsigned integer type of the result of the sizeof operator.
http://pubs.opengroup.org/onlinepubs/009604499/basedefs/stddef.h.html
但根据此页面,最大限制size_t为65535.(其他整数类型的部分限制):
size_t的限制:SIZE_MAX 65535 http://pubs.opengroup.org/onlinepubs/007904975/basedefs/stdint.h.html
这是否意味着当我想尊重C标准时,我不能分配超过65535个字节?
系统:Ubuntu 11.10 x86_64 CUDA:v 2.1
当尝试制作像matrixMul这样的示例程序时,我会收到大量错误,其中大部分是"未知类型名称'size_t'." 我已经确保将/ usr/local/cuda/bin放在我的PATH中,将/ usr/local/cuda/lib放在/etc/ld.so.conf.d中的.conf中.
有什么想法我为什么会收到这些错误?
matrixMul$ make emu=1
In file included from /tmp/tmpxft_00004089_00000000-1_matrixMul.cudafe1.stub.c:5:0,
from matrixMul.cu:196:
/usr/local/cuda//bin/../include/crt/host_runtime.h:192:0: warning: "__device_fun" redefined [enabled by default]
/usr/local/cuda//bin/../include/crt/host_runtime.h:215:0: note: this is the location of the previous definition
/usr/local/cuda//bin/../include/crt/host_runtime.h:194:0: warning: "__device_var" redefined [enabled by default]
/usr/local/cuda//bin/../include/crt/host_runtime.h:217:0: note: this is the location of the previous definition
/usr/local/cuda//bin/../include/crt/host_runtime.h:196:0: warning: "__tex_var" redefined [enabled by default]
/usr/local/cuda//bin/../include/crt/host_runtime.h:219:0: note: this is the location of the previous definition
/usr/local/cuda//bin/../include/crt/host_runtime.h:198:0: warning: "__cudaFatCubin" redefined [enabled by default]
/usr/local/cuda//bin/../include/crt/host_runtime.h:221:0: note: …Run Code Online (Sandbox Code Playgroud) 我一直在批评使用uint而不是使用size_t,但每次检查我正在使用的工具链时,结果都size_t被定义为uint.
是否有任何编译器实现size_t实际上不是uint?批评的理由是什么?
我已经读过stackoverflow上的几篇帖子,比如size_t和uintptr_t,而sizeof(size_t)== sizeof(void*)总是如此?并且理解根据C++标准,SIZE_T和void*的大小可以不同,以考虑诸如16位分段体系结构的体系结构.
所以我想将我的场景平台限制为Windows,无论是x86,x64,WoA(Windows on Arm):在Windows上,如果我在malloc中创建一个void*类型的指针,我想做指针 - 在将它传递给memcpy或其他东西之前的算法,我必须这样:
void* p = malloc(100);
memcpy(reinterpret_cast<void*>((reinterpret_cast<SIZE_T>(p) + offset), p2, 100);
Run Code Online (Sandbox Code Playgroud)
这看起来非常乏味,特别是如果你不得不在各处乱扔这种类型的铸件(我正在处理各种偏移).鉴于这是在Windows平台上,我想知道是否有一些简化可以减少这种类型的样板代码?
谢谢.
在函数中snprintf,如何strlcat,以及strlcpy他们的manpage size_t size在所述函数的参数中提到的内容,究竟是size什么,或者最好的方法是什么size?
strlcpy(3),strcat(3)和printf(3)(添加评论)size_t
strlcpy(char *dst, const char *src, size_t size);
// ^^^^
char *
strncat(char *str, const char *src, size_t n);
// ^
int
snprintf(char *str, size_t size, const char *format, ...);
// ^^^^
Run Code Online (Sandbox Code Playgroud)
我的理解是,size可以(或者,如果strncat,应该?)获得如下:
sizeof dst - strlen(dst) - 1
// sizeof dst buffer, minus the offset of '\0', minus 1 for the null-terminator
Run Code Online (Sandbox Code Playgroud)
但是我记得在某个地方阅读,以获得size …
我遇到了一个令人讨厌的错误,我有一个循环
for (auto i = 0; i < vec.size() -1; ++i) {
//
}
Run Code Online (Sandbox Code Playgroud)
由于vec是一个emtpy std::vector,因此vec.size() - -1评估2^64我的系统.
编写上述循环的正确方法是什么?
std::size_t可以存储任何类型(包括数组)的理论上可能的对象的最大大小.
我知道确切的值是平台相关的.但是谁决定了theoretically possible object编译器或操作系统,甚至计算机制造商的规模?可以计算理论上可能的对象的大小,还是仅仅由人为规则决定?
另外,如果机器是64位,这是否意味着最大对象大小可能是2 ^ 64字节?
我在Windows 10(64位安装)上使用mingw-w64编译器(gcc 8.1.0)。我的期望是获得size_t等于(2 ^ 64-1)的最大值。但是我的程序总是返回SIZE_MAXas 的值(仅2 ^ 32-1)。可能是什么原因?如何在我的C程序中实现最大值(2 ^ 64-1)?
我正在学习C语言,并想查看书中的陈述,即在现代计算机上最大size_t可以为(2 ^ 64-1)。我尝试检查的这段代码如下:
#include <stdio.h>
#include <stdint.h>
int main(){
printf("%d\n", sizeof(size_t));
printf("%zu\n", SIZE_MAX);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
4
4294967295
Run Code Online (Sandbox Code Playgroud)
我-std=c11在编译过程中仅使用一个标志并gcc --version返回:
gcc.exe (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 8.1.0
Run Code Online (Sandbox Code Playgroud)