此方法的功能是什么?问题是什么? [英] What is the functionality of this method?what are possibility problems,Bugs?

查看:75
本文介绍了此方法的功能是什么?问题是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void copy(int *s,int *d,int slength){
int i=0;
for(i=0;i<slength;i++){>
d[i]=s[i];
}

}

推荐答案

函数(显然不是方法)从源存储区(由slength int值>)到目标存储区(由d指向).
该函数(隐式)假定:
  • s是指向大小等于或大于slength的内存区域的有效指针.
  • 同样,d是指向内存的有效指针大小等于或大于slength的区域.
  • sd指向的内存区域不重叠.
The function (apparently it is not a method) copies slength int values from the source memory area (pointed by s) to the destination memory area ( pointed by d).
The function (implicitely) assumes that:
  • s is a valid pointer to a memory area of size equal to (or greater than) slength.
  • As well, d is a valid pointer to a memory area of size equal to (or greater than) slength.
  • The memory areas pointed by s and d don''t overlap.


忽略了CPallini的相当完整的答案,以及它是家庭作业还是求职面试的问题,您将如何解决该问题? ?除了在线询问之外.

假设您无法确定此代码的功能或问题.做大多数程序员无法做的事情,而这些程序员无法推理源代码会发生什么,并且:

-为该功能编写一组测试.系统化-尝试依次为每个参数添加零(可能是几个随机值).由于存在指针参数,请尝试指向各种大小的不同数组.尝试使用较小的slength值,尝试使用较大的值.

-运行测试,看看它们跌落了哪里.当您看到崩溃时,可以看到调试器下的代码发生了什么.

-看一下函数名,那里有一个指针(请原谅).在C或C ++运行时库中找到具有相似签名和功能的标准函数.在线查找它们,看看它们的极限是什么.

FFS仔细阅读,仔细考虑,尝试一下!

讲道结束,您可以停止阅读.

PS:这是母亲要孩子长大后成为Java程序员时会遇到的问题.
Ignoring CPallini''s rather complete answer and the fact it''s either homework or a job interview question how would YOU solve the problem? Apart from asking online about it that is.

Say you can''t work out what the functionality or problems with this code are. Do what most programmers would do who haven''t been able to reason about what happens with the source code and:

- write a set of tests for the function. Be systematic - try sticking zeroes in for -each parameter in turn, maybe a few random values. As there are pointer parameters try pointing at different arrays of various sizes. Try small values of slength, try massive values.

- run the tests and see where they fall over. When you see what crashes see what happens to the code under a debugger.

- look at the function name, bit of a pointer (pardon the pun) there. Find standard functions in the C or C++ runtime libraries with similar signatures and functionality. Look them up online and see what their limits are.

FFS read about it, think about it, try it!

End of sermon, you can stop reading now.

PS: This is the sort of problem that mothers give their children when they want them to grow up to become Java programmers.


当然,此功能没有问题.此函数的作用是将源数组的元素(此处为整数)复制到目标数组.
那么,告诉我们发生了什么事,我的意思是,如果您对此功能有任何疑问,那是什么?
我敢肯定错误只会由于参数传递给函数而发生,因为它们是指针.您需要仔细处理它们.如果指针未正确启动或遇到更多问题,您可能会遇到问题.
那么告诉我们您在使用此功能时遇到什么问题?
Certainly there is no problem with this function. What this function does is it copies elements(here integers)of a source array to a destination array.
So, tell us what happened, I mean if you have any problem with this function what is it?
I am sure the error would occur only because of parameters passed into the function, since they are pointers. You need to handle them carefully. You can face problems if your pointer was not initiated properly or and many more issues.
So tell us what problem have you encountered in using this function?


这篇关于此方法的功能是什么?问题是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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