首页 > 其他分享 > size_t in C

size_t in C

时间:2022-11-23 15:48:02浏览次数:56  
标签:used int void unsigned type size

 

size_t is an unsigned integral data type which is defined in various header files such as: 
 

<stddef.h>, <stdio.h>, <stdlib.h>, <string.h>, <time.h>, <wchar.h>

It’s a type which is used to represent the size of objects in bytes and is therefore used as the return type by the sizeof operator. It is guaranteed to be big enough to contain the size of the biggest object the host system can handle. Basically the maximum permissible size is dependent on the compiler; if the compiler is 32 bit then it is simply a typedef(i.e., alias) for unsigned int but if the compiler is 64 bit then it would be a typedef for unsigned long long. The size_t data type is never negative.
Therefore many C library functions like malloc, memcpy and strlen declare their arguments and return type as size_t. For instance, 
 

// Declaration of various standard library functions.

  

// Here argument of 'n' refers to maximum blocks that can be

// allocated which is guaranteed to be non-negative.

void *malloc(size_t n);

  

// While copying 'n' bytes from 's2' to 's1'

// n must be non-negative integer.

void *memcpy(void *s1, void const *s2, size_t n);

  

// strlen() uses size_t because the length of any string

// will always be at least 0.

size_t strlen(char const *s);

size_t or any unsigned type might be seen used as loop variable as loop variables are typically greater than or equal to 0.
Note: When we use a size_t object, we have to make sure that in all the contexts it is used, including arithmetic, we want only non-negative values. For instance, the following program would definitely give the unexpected result: 
 

// C program to demonstrate that size_t or

// any unsigned int type should be used 

// carefully when used in a loop.

#include<stdio.h>

  

#define N 10

  

int main()

{

    int a[N];

  

    // This is fine.

    for (size_t n = 0; n < N; ++n) {

        a[n] = n;

    }

          

    // But reverse cycles are tricky for unsigned 

    // types as they can lead to infinite loops.

    for (size_t n = N-1; n >= 0; --n)

        printf("%d ", a[n]);

}

Output

Infinite loop and then segmentation fault

 

标签:used,int,void,unsigned,type,size
From: https://www.cnblogs.com/xiangsplayground/p/16918502.html

相关文章

  • sizeof运算符介绍以及常见的坑
    文章目录​​一、基本概念​​​​二、注意事项​​​​1、sizeof(结构体)​​​​2、不要对void使用sizeof​​​​3、不要在子函数中对字符指针用sizeof​​​​4、不要在子......
  • springboot maxheadersize 配置不当 oom
    在SpringBoot项目中,我们可以通过如下配置来设置header的大小:server.max-http-header-size=102400但如果此参数设置不好,便会引来OOM等相关问题,特别是并发的时候。m......
  • C# get form to invalidate when form is resized?
    我刚刚接受了一个挑战,创建一个64个相同矩形或正方形的颜色交替的棋盘,从技术上来说,我已经完成了这个挑战;但是出于好奇,我注意到当用户调整窗体的大小并拖动它时,窗体会一直重......
  • 一次启动失败引发的思考:-server -XX:PermSize=2048M -XX:MaxPermSize=4096m
    Tomcat启动参数启动项目时,由于项目比较大,无法正常启动,报异常:java.lang.OutOfMemoryError:PermGenspace,在idea中设置VMoptions为:-server-XX:PermSize=2048M-XX:MaxPer......
  • java -Xms -Xmx -XX:PermSize -XX:MaxPermSize-详解
         在做java开发时尤其是大型软件开发时经常会遇到内存溢出的问题,比如说OutOfMemoryError等。这是个让开发人员很痛苦、也很纠结的问题,因为我们有时不知道什么样的......
  • sizeof的迷惑行为
    问题:判断下面代码的运行结果?A.>  B.<   C.不输出  D.程序有问题分析:i为全局变量,未设初始值时,编译器默认初始化为0。所有i--为-1,sizeof(i)求i类型大小为4,家人......
  • C++ Tips:static const size_t nops、string substr、upper_bound、find()
    维基百科连接......
  • Unity ContentSizeFitter组件
    ContentSizeFitter组件,它可以动态改变物体的宽高,但它有一个非常需要注意的点就是,它不是即时刷新,是帧末刷新,这个特性如果没注意会出现一个问题就是你拿到加了这个组件的......
  • ORA-13541: system moving window baseline size (691200) greater than retention (6
    ORA-13541:systemmovingwindowbaselinesize(691200)greaterthanretention(604800)Error:SQL>execdbms_workload_repository.modify_snapshot_settings(inter......
  • Size of的用法
    1#include<cstdlib>2#include<iostream>3#include<iterator>4#include"string.h"5usingnamespacestd;6struct{7shorta1;8shorta2......