Comments (2)
我请教一个问题,这里应该是堆内存分配空间吧,作者这样为什么不用栈分配或内存池。性能会受损吧。
struct RawMD { RawMD(MsgType msgType, const void* data, std::uint32_t dataLen, bool isLast, std::uint64_t localTs = GetTotalUSSince1970()) { msgType_ = msgType; localTs_ = localTs; dataLen_ = dataLen; data_ = malloc(dataLen); memcpy(data_, data, dataLen_);
在这段代码中,使用堆内存分配空间是为了避免栈溢出的问题。栈内存有限,如果分配的空间过大,可能会导致栈溢出。而使用堆内存可以动态地申请所需的内存空间,不受栈大小的限制。
此外,使用堆内存还可以更好地管理内存资源。当不再需要这个内存时,可以通过调用delete释放该内存,避免内存泄漏。
对于性能方面的影响,堆内存分配可能会比栈内存分配稍慢一些,因为需要系统分配额外的内存空间,并且在释放内存时也需要额外的操作。但是,在实际应用中,这种差异通常可以忽略不计,并且使用动态内存分配可以提供更灵活的内存管理方式。
from betterquant2.
谢谢解答!
from betterquant2.
Related Issues (1)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from betterquant2.