Questions tagged [allocation]

Memory allocation is an operation of giving a program a block of memory.

1869 questions
492
votes
24 answers

Why is the use of alloca() not considered good practice?

alloca() allocates memory on the stack rather than on the heap, as in the case of malloc(). So, when I return from the routine the memory is freed. So, actually this solves my problem of freeing up dynamically allocated memory. Freeing of memory…
Vaibhav
  • 5,749
  • 3
  • 22
  • 18
323
votes
0 answers

Managing the lifetimes of garbage-collected objects

I am making a simplistic mark-and-compact garbage collector. Without going too much into details, the API it exposes is like this: /// Describes the internal structure of a managed object. pub struct Tag { ... } /// An unmanaged pointer to a…
isekaijin
  • 19,076
  • 18
  • 85
  • 153
150
votes
4 answers

Declare slice or make slice?

In Go, what is the difference between var s []int and s := make([]int, 0)? I find that both works, but which one is better?
Wang Yi
  • 1,777
  • 3
  • 13
  • 6
120
votes
3 answers

Freaky way of allocating two-dimensional array?

In a project, somebody pushed this line: double (*e)[n+1] = malloc((n+1) * sizeof(*e)); Which supposedly creates a two-dimensional array of (n+1)*(n+1) doubles. Supposedly, I say, because so far, nobody I asked could tell me what this does,…
User1291
  • 7,664
  • 8
  • 51
  • 108
87
votes
15 answers

Determine size of dynamically allocated memory in C

Is there a way in C to find out the size of dynamically allocated memory? For example, after char* p = malloc (100); Is there a way to find out the size of memory associated with p?
s_itbhu
  • 1,177
  • 3
  • 10
  • 12
72
votes
3 answers

What is the difference between [Class new] and [[Class alloc] init] in iOS?

Possible Duplicate: alloc, init, and new in Objective-C I am a little confused about [Class new] and [[Class alloc] init]. I have defined an object content using [Class new] and [[Class alloc] init]. (1). NSMutableArray *content =…
Prasad G
  • 6,702
  • 7
  • 42
  • 65
58
votes
6 answers

Are Structs always stack allocated or sometimes heap allocated?

I was of the impression that in C#, struct elements are allocated on the stack and thus disappear when returning from a method in which they were created. But what happens if I place the struct-values in a list and return that? The elements…
Carlo V. Dango
  • 13,322
  • 16
  • 71
  • 114
52
votes
3 answers

How to profile memory usage & performance with Instruments?

Of all the Instruments Trace Templates, I love using: Zombies to detect where an object is getting over-released, great for debugging EXEC_BAD_ACCESS errors. Leaks to detect memory leaks. Core Animation w Color Blended Layers to detect frame rate &…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
51
votes
8 answers

Getting a stack overflow exception when declaring a large array

The following code is generating a stack overflow error for me int main(int argc, char* argv[]) { int sieve[2000000]; return 0; } How do I get around this? I am using Turbo C++ but would like to keep my code in C EDIT: Thanks for the…
Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120
41
votes
4 answers

Memory Allocation/Deallocation?

I have been looking at memory allocation lately and I am a bit confused about the basics. I haven't been able to wrap my head around the simple stuff. What does it mean to allocate memory? What happens? I would appreciated answers to any of these…
Isaac
  • 2,246
  • 5
  • 21
  • 34
38
votes
3 answers

Escape analysis in Java

As far as I know the JVM uses escape analysis for some performance optimisations like lock coarsening and lock elision. I'm interested if there is a possibility for the JVM to decide that any particular object can be allocated on stack using escape…
Denis Bazhenov
  • 9,680
  • 8
  • 43
  • 65
38
votes
7 answers

In what cases should I use memcpy over standard operators in C++?

When can I get better performance using memcpy or how do I benefit from using it? For example: float a[3]; float b[3]; is code: memcpy(a, b, 3*sizeof(float)); faster than this one? a[0] = b[0]; a[1] = b[1]; a[2] = b[2];
Pythagoras of Samos
  • 3,051
  • 5
  • 29
  • 51
38
votes
8 answers

Multithreaded Memory Allocators for C/C++

I currently have heavily multi-threaded server application, and I'm shopping around for a good multi-threaded memory allocator. So far I'm torn between: Sun's umem Google's tcmalloc Intel's threading building blocks allocator Emery Berger's…
Robert Gould
  • 68,773
  • 61
  • 187
  • 272
35
votes
2 answers

When is array allocated on stack in c#?

I've been trying to figure out when things get allocated on stack and I can't figure out how would you make array (or rather values in it) get allocated on stack; in this example: public void foo() { int bar[] = new int [10]; } 10 int structs…
morowinder
  • 483
  • 1
  • 4
  • 6
29
votes
2 answers

Is it possible to remove ExecutionContext and Thread allocations when using SocketAsyncEventArgs?

If you profile a simple client application that uses SocketAsyncEventArgs, you will notice Thread and ExecutionContext allocations. The source of the allocations is SocketAsyncEventArgs.StartOperationCommon that creates a copy of the…
cao
  • 997
  • 9
  • 17
1
2 3
99 100