alloca()
在堆栈上而不是在堆上分配内存,如同的情况一样malloc()
.所以,当我从例程返回时,内存被释放.所以,实际上这解决了我释放动态分配内存的问题.释放分配的内存malloc()
是一个令人头痛的问题,如果不知何故错过会导致各种内存问题.
alloca()
尽管有上述特征,为什么不鼓励使用?
在一个项目中,有人推动了这一行:
double (*e)[n+1] = malloc((n+1) * sizeof(*e));
Run Code Online (Sandbox Code Playgroud)
据推测,这会创建一个(n + 1)*(n + 1)双倍的二维数组.
据说,我说,因为到目前为止,我没有人问我能告诉我它的作用,确切地说,它起源于何处或为什么它应该起作用(据称,它确实如此,但我还没有购买它).
也许我错过了一些明显的东西,但如果有人能够向我解释上述内容,我会很感激.因为就个人而言,如果我们使用我们真正理解的东西,我会感觉好多了.
在Golang,var s []int
和之间有什么区别s := make([]int, 0)
?
我发现两者都有效,但哪一个更好?
C++中的静态数组和动态数组有什么区别?
我必须为我的班做一个任务,它说不要使用静态数组,只使用动态数组.我看过这本书和网上,但我似乎并不理解.
我认为静态是在编译时创建的,并且在运行时是动态的,但我可能会误以为内存分配.
你能解释一下C++中静态数组和动态数组之间的区别吗?
我有点困惑[Class new]
和 [[Class alloc] init]
.我content
用[Class new]
和 定义了一个对象[[Class alloc] init]
.
(1). NSMutableArray *content = [NSMutableArray new];
(2). NSMutableArray *content = [[NSMutableArray alloc] init];
Run Code Online (Sandbox Code Playgroud)
我的问题是关于之间的差异[Class new]
和[[Class alloc] init]
.对我来说,(1)和(2)是相似的.如果(1)和(2)相似,那么为什么我们[[Class alloc] init]
大部分时间都在使用[Class new]
?我认为必须有一些区别.
请解释两者的差异,利弊?
有没有办法在C中找出动态分配内存的大小?
例如,之后
char* p = malloc (100);
Run Code Online (Sandbox Code Playgroud)
有没有办法找出与之相关的内存大小p
?
在所有仪器跟踪模板中,我喜欢使用:
EXEC_BAD_ACCESS
错误.UITableView
滚动.我总是听到人们说要描述我的应用程序的内存使用情况和性能.
我使用了Allocations,看到我的iPhone应用程序的总分配内存为1 MB,正常使用后增长到5 MB.iPhone上的内存使用量是多少?iPad的?苹果电脑?
以下代码为我生成堆栈溢出错误
int main(int argc, char* argv[])
{
int sieve[2000000];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?我正在使用Turbo C++,但我想将我的代码保存在C中
编辑:
感谢您的建议.上面的代码只是例如,我实际上在函数中声明了数组而不是在sub main中.此外,我需要将数组初始化为零,所以当我使用Google搜索时,我发现calloc非常适合我的目的.
Malloc/calloc还具有优于堆栈分配的优势,允许我使用变量声明大小.
我正在制作一个简单的标记和紧凑的垃圾收集器.没有太多细节,它暴露的API是这样的:
/// Describes the internal structure of a managed object.
pub struct Tag { ... }
/// An unmanaged pointer to a managed object.
pub type Pointer = *mut usize;
/// Mapping from old to new locations.
pub type Adjust = BTreeMap<usize, Pointer>;
/// Mark this object and anything it points to as non-garbage.
pub unsafe fn survive(ptr: Pointer);
pub struct Heap { ... }
impl Heap {
pub fn new() -> Heap;
// Allocate an object with …
Run Code Online (Sandbox Code Playgroud) 我的印象是在C#中,struct元素在堆栈上分配,因此从创建它们的方法返回时会消失.但是如果我将struct-values放在一个列表中并返回它会发生什么?元素幸存下来.是否有时在堆上分配struct实例?
internal struct Stru
{
public int i;
}
internal class StruTry
{
public List<Stru> Get(int max)
{
var l = new List<Stru>();
for (int i = 0; i < max; i++)
l.Add(new Stru {i=i});
return l;
}
}
Run Code Online (Sandbox Code Playgroud)
代码打印0,1,2
[Test]
public void T()
{
var ll = new StruTry().Get(3);
foreach (var stru in ll)
Console.WriteLine("* "+ stru.i);
}
Run Code Online (Sandbox Code Playgroud) allocation ×10
c ×4
arrays ×3
dynamic ×2
malloc ×2
memory ×2
stack ×2
alloca ×1
c# ×1
c++ ×1
go ×1
init ×1
instruments ×1
ios ×1
lifetime ×1
new-operator ×1
objective-c ×1
performance ×1
profiling ×1
rust ×1
size ×1
slice ×1
static ×1
struct ×1
xcode ×1