这段代码中字符串的大小会怎样? [英] What will be happen to the size of string in this code?

查看:45
本文介绍了这段代码中字符串的大小会怎样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码中有两个字符串,一个是数组,另一个是使用指针动态创建的。



1.如果我输入的字符串小于50对于string2,如果减少了大小,那么未填充的空间将被浪费掉,如果是这样的话。

2.如果是字符串2,则malloc size参数为10,fgets size参数为10如果我将大小增加到大于malloc大小的fgets(string2,50,stdin)会发生什么?

3.如何计算每种情况下输入字符串的最终大小?我用过sizeof运算符,但它给了string1和string2分别为50和10的硬编码大小

4.还有其他更好的方法来创建动态字符串吗?



我尝试过:



 #include< stdio.h> 
#include< stdlib.h>

int main()
{
int size = 10;
char string1 [50];
char * string2;
string2 =(char *)malloc(size * sizeof(char));

fgets(string1,10,stdin);
printf(%s,string1);

fgets(string2,10,stdin);
printf(%s,string2);

}

解决方案

1)是的,它会被浪费 - 尽管浪费几个字节是没有的今天计算机上的主要问题。



2)如果你在fgets调用中指定的长度比缓冲区中可用的长度长,则结果是未定义的。该操作将覆盖分配的缓冲区后面的一些内存,其结果是未定义的。在最坏的情况下,它可能导致程序崩溃。



3)要确定以null结尾的字符串的有效长度,请使用函数strlen或其表兄弟广泛的字符。



4)是的,有更好的方法,但它需要C ++。你有预先构建的类,比如std :: string或CString,它们可以为你完成缓冲区管理的大部分工作。


引用:

这段代码中字符串的大小会怎样?

没什么,你在变量中存储的东西不会改变它的大小。

1 )是的,它浪费空间,而不是一个大问题。

2)C / C ++未经检查,所以你有责任照顾这个。

这导致了问题称为缓冲区溢出 缓冲区溢出 - 维基百科,免费的百科全书 [ ^ ]

如果您使用托管语言,则可以避免此问题。


There are two strings in this code one is an array and another one is dynamically created using pointer.

1.If my input is less than 50 for string1 and less than 10 for string2 will the space that is not filled get wasted ,if so how to reduce the size.
2.In case of string 2 malloc size parameter is 10 and fgets size parameters is 10 what will happen if i increase the size to fgets(string2,50,stdin) which is greater than malloc size?
3.how to calculate the final size of input string in each case?I have used sizeof operator but it gave the hardcoded size that is 50 and 10 respectively for string1 and string2
4.Is there any other better approach to create a dynamic string?

What I have tried:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int size=10;
    char string1[50];       
    char *string2;
    string2=(char *)malloc(size*sizeof(char));

    fgets(string1,10,stdin);
    printf("%s",string1);

    fgets(string2,10,stdin);
    printf("%s",string2);

}

解决方案

1) yes, it will be wasted -- although wasting a few bytes is no major problem on today's computers.

2) If you specify a greater length in the fgets call than is available in your buffer the outcome is undefined. The operation will overwrite some memory behind your allocated buffer, the outcome of which is undefined. In the worst case it can result in a program crash.

3) To determine the effective length of a null-terminated string use the function strlen, or its cousins for wide-characters.

4) Yes, there is a better approach, but it requires C++. There you have pre-built classes like std::string or CString that do most of the hard work of buffer management for you.


Quote:

What will be happen to the size of string in this code?

Nothing, what you store in a variable do not change its size.
1) Yes, it waste space, not a big problem.
2) C/C++ are unchecked, so you are responsible of taking care of this.
This lead to a problem called buffer overflow Buffer overflow - Wikipedia, the free encyclopedia[^]
The problem is avoided if you use managed languages.


这篇关于这段代码中字符串的大小会怎样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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