Questions tagged [allocation]

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

1869 questions
18
votes
2 answers

Should I check if malloc() was successful?

Should one check after each malloc() if it was successful? Is it at all possible that a malloc() fails? What happens then? At school we were told that we should check, i.e.: arr = (int) malloc(sizeof(int)*x*y); if(arr==NULL){ printf("Error.…
gen
  • 9,528
  • 14
  • 35
  • 64
18
votes
1 answer

If a function returns an UnsafeMutablePointer is it our responsibility to destroy and dealloc?

For example if I were to write this code: var t = time_t() time(&t) let x = localtime(&t) // returns UnsafeMutablePointer println("\(x.memory.tm_hour): \(x.memory.tm_min): \(x.memory.tm_sec)") ...would it also be necessary to also do…
sketchyTech
  • 5,746
  • 1
  • 33
  • 56
18
votes
8 answers

Allocate memory 2d array in function C

How to allocate dynamic memory for 2d array in function ? I tried this way: int main() { int m=4,n=3; int** arr; allocate_mem(&arr,n,m); } void allocate_mem(int*** arr,int n, int m) { *arr=(int**)malloc(n*sizeof(int*)); for(int…
serhii
  • 1,135
  • 2
  • 12
  • 29
17
votes
3 answers

Why is there a stack and a heap?

Why do assembly languages use both a stack and a heap? They seem redundant.
mcandre
  • 22,868
  • 20
  • 88
  • 147
17
votes
7 answers

dynamic allocating array of arrays in C

I don't truly understand some basic things in C like dynamically allocating array of arrays. I know you can do: int **m; in order to declare a 2 dimensional array (which subsequently would be allocated using some *alloc function). Also it can be…
hyperboreean
  • 8,273
  • 12
  • 61
  • 97
17
votes
1 answer

How should C++ libraries allow custom allocators?

In C, it's simple for a library to allow the user to customize memory allocation by using global function pointers to a function that should behave similarly to malloc() and to a function that should behave similarly to free(). SQLite, for example,…
Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
17
votes
6 answers

How to avoid long chain of free's (or deletes) after every error check in C?

Suppose I write my code very defensively and always check the return types from all the functions that I call. So I go like: char* function() { char* mem = get_memory(100); // first allocation if (!mem) return NULL; struct binder* b =…
bodacydo
  • 75,521
  • 93
  • 229
  • 319
17
votes
2 answers

Why is there no "recalloc" in the C standard?

Everyone knows that: realloc resizes an existing block of memory or copies it to a larger block. calloc ensures the memory is zeroed out and guards against arithmetic overflows and is generally geared toward large arrays. Why doesn't the C…
Matt
  • 21,026
  • 18
  • 63
  • 115
17
votes
10 answers

How to allocate a 2D array of pointers in C++

I'm trying to make a pointer point to a 2D array of pointers. What is the syntax and how would I access elements?
TheFuzz
  • 2,607
  • 6
  • 29
  • 48
17
votes
4 answers

Linux Allocator Does Not Release Small Chunks of Memory

The Linux glibc allocator seems to be behaving weirdly. Hopefully, someone can shed some light on this. Here is the source file that I have: first.cpp: #include #include #include #include int main() { …
user1418199
  • 257
  • 1
  • 3
  • 7
16
votes
6 answers

Bad allocation exceptions in C++

In a school project of mine, I was requested to create a program without using STL In the program, I use a lot of Pointer* = new Something; if (Pointer == NULL) throw AllocationError(); My questions are about allocation error: is there an…
CaptainNemo
  • 1,532
  • 2
  • 22
  • 45
16
votes
5 answers

Does an unused STL container allocate memory?

Given the code: class Foo { std::vector items; std::map dictionary; }; If nothing is ever added to the above vector or map, will either still allocate a block of buffer memory? (In other words, does buffer allocation always…
silentorb
  • 2,432
  • 2
  • 18
  • 20
16
votes
4 answers

Proof that Fowler's money allocation algorithm is correct

Martin Fowler has a Money class that has a money allocation routine. This routine allocates money according to a given list of ratios without losing any value through rounding. It spreads any remainder value over the results. For example, $100…
dan-gph
  • 16,301
  • 12
  • 61
  • 79
15
votes
2 answers

Should I pass allocator as a function parameter? (my misunderstanding about allocator)

After I am studying about allocator for a few days by reading some articles (cppreference and Are we out of memory) , I am confused about how to control a data-structure to allocate memory in a certain way. I am quite sure I misunderstand…
javaLover
  • 6,347
  • 2
  • 22
  • 67
14
votes
2 answers

Android: Track number of objects created

I'm porting a game to Android (there's a lot of code and very little of it is mine), and DalvikVM is telling me (through LogCat) all about the garbage collection. At some point in the code, I get a stream of "GC freed x objects / x ms" messages,…
mcccclean
  • 7,721
  • 10
  • 32
  • 36