为什么分配的内存是固定的。 [英] Why is the memory allocated is fixed .

查看:45
本文介绍了为什么分配的内存是固定的。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Subham和Binary Strings |位操作的基础知识&基本编程实践问题| HackerEarth [ ^ ]



我尝试了什么:



Subham and Binary Strings | Basics of Bit Manipulation & Basic Programming Practice Problems | HackerEarth[^]

What I have tried:

#include <stdio.h>
 
int main()
{
    int t,n,i;
    scanf("%d",&t);
    
    while(t--){
        scanf("%d",&n);
        char arr[100000];
        int count=0;
        
        scanf("%s",&arr);
        for(i=0;i<strlen(arr);i++){
            if(arr[i]=='0')
            count++;
        }
        
        printf("%d\n",count);
    }
    return 0;
}





但如果我使用arr [n]来指定数组的大小。

为什么我们必须修改数组的大小。

我的代码只适用于第一个测试用例。

plzz解释我发生了这个问题。



but if i using arr[n] to specify the size of the array.
why we have to fix size of the array.
my code is working for only 1st test case.
plzz explain me this problem occured.

推荐答案

当你声明一个数组时,你必须告诉系统你需要多少元素(以及什么类型) - 否则它不能给你正确的数字,如果它给你太少,你将超过分配的内存,并写入其他数据。这将导致您的应用程序出现故障,或导致其终止以尝试访问它没有的内存。



这就像去咖啡店:你必须告诉他们你想喝多大的饮料,因为如果他们给你最大的饮料,你就不会为此付出代价,如果他们给你一杯浓咖啡,你可能会非常失望。



另一个解决方案是读取n,并使用 malloc 来分配你需要的空间 - n *元素的大小以字节为单位完成后不要忘记释放内存!
When you declare an array, you have to tell the system how many elements (and of what type) you need - otherwise it can't give you the right number, and if it gives you too few, you will exceed the allocated memory, and write over other data. This will either cause a fault in your app, or cause it to be terminated for trying to access memory it doesn't have.

It's like going to a coffee shop: you have to tell them what size drink you want, because if they give you the biggest they can, you won't want to pay for it, and if they give you an espresso cup you might be very disappointed.

The other solution is to read n, and use malloc to allocate the space you need - n * the size of the element in bytes. Don't forget to free the memory when you are finished with it!


#include <stdio.h>
 
int main()
{
    int t,n,i;
    scanf("%d",&t);
    
    while(t--){
        scanf("%d",&n);
        char arr[n];
        int count=0;
        
        scanf("%s",&arr);
        for(i=0;i<strlen(arr);i++){
            if(arr[i]=='0')
            count++;
        }
        
        printf("%d\n",count);
    }
    return 0;





它适用于第一个测试用例。

如果我使用char [100000],即如果我将修复数组的大小,它适用于所有测试用例。

plzzz在hackerearth.com查看问题一次。



it is working for 1st test case .
and if i use char[100000] i.e. if i will fix the size of array it is working for all the test cases.
plzzz check the ques in hackerearth.com once.


Quote:

scanf(%d,& n);

char arr [n];

int count = 0;

scanf(%s,arr);

scanf("%d",&n);
char arr[n];
int count=0;
scanf("%s",arr);



C99 允许在堆栈上进行动态内存分配,因此


C99 allows dynamic memory allocation on the stack, hence

char arr[n];

是一个有效的声明。

但是,你的代码存在缺陷,因为 scanf(%s,arr); 可能会超出 arr 缓冲区(例如假设用户设置 n = 5 然后输入字符串foobar)。

is a valid statement.
However, your code is flawed, because scanf("%s",arr); could overrun the arr buffer (e.g. suppose the user sets n=5 and then enters the string "foobar").


这篇关于为什么分配的内存是固定的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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