传递炭缓冲功能和获得的缓冲区的大小 [英] passing char buffer to functions and getting the size of the buffer

查看:115
本文介绍了传递炭缓冲功能和获得的缓冲区的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了缓冲区大小100。
我显示在所述缓冲器被声明的主要功能的缓冲器。
然而,当我通过缓冲功能,并获得sizeof的4,
我想它应该是100,因为这是缓冲区的大小,我
在主创建。
    输出:
    缓冲区大小:100
    的sizeof(缓冲区):4

I have set the buffer to size 100. I display the buffer in the main function where the buffer is declared. However, when I pass the buffer to the function and get the sizeof '4', I was thinking it should be 100, as that is the size of the buffer that I created in main. output: buffer size: 100 sizeof(buffer): 4

#include <string.h>
#include <stdio.h>

void load_buffer(char *buffer);

int main()
{
    char buffer[100];
    printf("buffer size: %d\n", sizeof(buffer));
    load_buffer(buffer);

    return 0;
}

void load_buffer(char *buffer)
{
    printf("sizeof(buffer): %d\n", sizeof(buffer));
}


推荐答案

您使用的指针的大小到缓冲区(4字节),而不是缓冲区的大小。

You are using the size of the pointer to the buffer (4 bytes), rather than the size of the buffer.

在C,你必须通过单独的缓冲,这也是原因缓冲区溢出轻松,频繁地发生,所以部分的大小。

In C, you have to pass the size of the buffer separately, which is part of the reason buffer overruns happen so easily and frequently.

void load_buffer(char * buffer, size_t bufSize)
{    
    ...
}

这篇关于传递炭缓冲功能和获得的缓冲区的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆