有问题关于使用exitC++中.答案讨论主要是因为RAII不是好主意,例如,如果exit在代码中某处调用,则不会调用对象的析构函数,因此,如果例如析构函数是为了将数据写入文件,则不会发生这种情况,因为没有调用析构函数.
我感兴趣的是C中的这种情况.类似的问题是否也适用于C?我想在C中我们不使用构造函数/析构函数,C中的情况可能会有所不同.所以exit在C中使用是否可以?
我已经看到了下面的函数,我觉得在某些情况下可以使用它,但是如果我们在C中使用类似的问题感兴趣exit,如上面用C++所述?(这将使使用下面的功能不是一个好主意.).
void die(const char *message)
{
if(errno) {
perror(message);
} else {
printf("ERROR: %s\n", message);
}
exit(1);
}
Run Code Online (Sandbox Code Playgroud) 假设我们有组件:
let Component = (props)=><div>Hi</div>;
Run Code Online (Sandbox Code Playgroud)
我有时会遇到有人在渲染中将反应组件调用为函数的代码:
const App = () => (
<div> {Component()} </div>
)
Run Code Online (Sandbox Code Playgroud)
VS将其渲染为元素
const App = () => (
<div> <Component/> </div>
)
Run Code Online (Sandbox Code Playgroud)
在 React Hooks 中,如果我将组件作为函数调用会出现什么问题?
有类似的问题,但它并不是专门针对钩子 - 而且这个问题更多的是关于性能。
考虑这个例子:
let memoizedCb = React.useCallback(
memoize((param) => () => someFunction(param)),
[]
);
Run Code Online (Sandbox Code Playgroud)
哪里memoize来自外部库,例如“fast-memoize”。上面的代码给出了警告:
React Hook useCallback 收到一个依赖项未知的函数。改为传递内联函数
我发现这个线程,如果我们适应我的用例,会建议将此作为解决方案(如果我没有错的话):
const memoizedCb = React.useMemo(
() => memoize((param) => () => someFunction(param)),
[]
);
Run Code Online (Sandbox Code Playgroud)
警告是关于什么的?为什么要useMemo解决这个问题?
注意:someFunction在函数组件之外定义,因此不需要将其作为依赖项。
想象一下这个(简化的)某个父组件的函数成员:
handleInputChange(e) {
// let val = e.target.value; - if I uncomment this, it works.
// Update text box value
this.setState(function (prevState, props) {
return {
searchValue: e.target.value,
}
})
}
Run Code Online (Sandbox Code Playgroud)
和textbox(子组件的成员)如下:
<input type="text" onChange={that.props.handleInputChange} value={that.props.searchValue} />
Run Code Online (Sandbox Code Playgroud)
当我在文本字段中输入内容时,我得到错误handleInputChange.
如果我取消注释props函数内的第一行,我将文本框值存储在Cannot read property 'value' of null变量中,它运行良好.想法为什么?
PS.如上所述,该handleInputChange函数在父组件中声明,而实际输入控件在子组件中定义.您可以看到我如何使用click处理程序和值作为子组件中的props.
我尝试编写带有内存泄漏的javascript代码,以便与Chrome中的探查器一起使用.然而,似乎剖析器没有显示它应该是什么.
这是我的代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="start_button">Start</button>
<button id="destroy_button">Destroy</button>
<script type="text/javascript" charset="utf-8">
var Leaker = function(){};
Leaker.prototype = {
init:function(){
this._interval = null;
this.start();
},
start: function(){
var self = this;
this._interval = setInterval(function(){ self.onInterval(); }, 100);
},
onInterval: function(){ console.log("Interval"); }
};
$("#start_button").click(function(){
if(leak !== null && leak !== undefined){
return;
}
leak = new Leaker();
leak.init();
});
$("#destroy_button").click(function(){
leak = null;
});
var leak;
</script>
Run Code Online (Sandbox Code Playgroud)
你可以看到我点击开始按钮时创建了一个新的对象Leaker.当我点击destroy时,该对象被设置为null(注意:但它不会被垃圾收集,因为setInterval仍然可以工作).
问题:然而,当我使用Google Chrome Profiler时,我点击destroy后没有向我显示对Leaker实例的任何引用(但它应该向我显示这样的引用,因为如上所述,setInterval的闭包仍然是抓住它).
点击破坏按钮后的Profiler(你可以看到我再也找不到leaker实例,而它应该在那里).
在这里控制台,setInterval仍然在运行,尽管探查器告诉我们没有更多的Leaker实例.
我是否在垃圾收集工作方式或Chrome分析器上遗漏了什么?
这个C程序给出了一个奇怪的结果:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char str1[5] = "abcde";
char str2[5] = " haha";
printf("%s\n", str1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我得到:
abcde haha
Run Code Online (Sandbox Code Playgroud)
我只想打印第一个字符串,从代码中可以看出.
为什么要打印它们?
我在我的代码中使用BinaryWriter,这是我的代码:
static void Main(string[] args)
{
FileInfo file = new FileInfo(@"F:\testfile");
if (file.Exists) file.Delete();
using (BinaryWriter bw = new BinaryWriter(file.Create()))
{
ushort a = 1024;
ushort b = 2048;
bw.Write(a);
bw.Write(b);
bw.Write(a);
bw.Write(b);
}
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
但输出文件的十六进制是:
是不是0x0004 = 4?为什么?
我想这是因为JS的工作原理,但是我想的类不会遇到这个问题。在此代码中:
let [open, setOpen] = React.useState(false);
let [counter, setCounter] = React.useState(0);
function handleClick() {
setOpen(true);
setInterval(() => {
console.log(counter);
setCounter(counter + 1);
}, 2000);
}
Run Code Online (Sandbox Code Playgroud)
如果我一次调用handleClick (例如,单击按钮),则控制台上登录的值始终为0(尽管每次都会更新状态)。
这可能是由于关闭。但是,如果我想查看counter此类设置的最新值怎么办?使用类,您可以完成任务this.state.counter,并且它将读取最新值。挂钩是否有解决方法?
演示。
我发现这个问题基本相同。不知何故,我在最初的搜索中没有遇到它。
春天有点新鲜.当我通过接口实例化bean时,它似乎不会获取事件,但是,如果我使用实际的类实现接口,则接收事件.为什么是这样?代码如下.
package javabeans.di;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;
public class HelloWorldImpl implements HelloWorld, ApplicationListener<ContextStartedEvent> {
private String msg;
public HelloWorldImpl(String s){
msg = s;
}
@Override
public void printHelloWorld() {
System.out.println("Hello : " + msg);
}
public void onApplicationEvent(ContextStartedEvent event) {
System.out.println("ContextStartedEvent Received");
}
}
Run Code Online (Sandbox Code Playgroud)
这是调用代码:
public static void main(String[] args) {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfig.class);
// Let us raise a start event.
ctx.start();
HelloWorld obj = (HelloWorld) ctx.getBean("helloWorld");
obj.printHelloWorld();
ctx.stop();
}
Run Code Online (Sandbox Code Playgroud)
配置类:
@Configuration
public class HelloWorldConfig {
@Bean
@Scope("prototype") …Run Code Online (Sandbox Code Playgroud) 经过一番尝试,我发现严格模式下会出现以下问题。如果有人能解释原因,我会很感兴趣。
以这个简单的例子为例,在渲染内部我只是安排一个更新状态的超时:
例子:
let firstRender = true; // Normally I would use ref but I was playing with example
export default function App() {
let [data, setData] = React.useState({ name: 'Nick' });
// Schedule a timeout on first render
if (firstRender) {
setTimeout(() => {
console.log('Running');
setData((ps) => ({
...ps,
name: 'Paul',
}));
}, 1000);
}
console.log('Running render');
firstRender = false;
return (
<div>
<h1>{data.name}</h1>
<p>Start editing to see some magic happen :)</p>
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
如果您 …
javascript ×6
reactjs ×5
react-hooks ×3
c ×2
binarywriter ×1
c# ×1
endianness ×1
java ×1
memory-leaks ×1
printf ×1
profiler ×1
spring ×1
string ×1
usecallback ×1