3,841 questions
1173
votes
21
answers
310k
views
Can a local variable's memory be accessed outside its scope?
I have the following code.
#include <iostream>
int * foo()
{
int a = 5;
return &a;
}
int main()
{
int* p = foo();
std::cout << *p;
*p = 8;
std::cout << *...
9565
votes
30
answers
2.0m
views
What and where are the stack and heap?
What are the stack and heap?
Where are they located physically in a computer's memory?
To what extent are they controlled by the OS or language run-time?
What is their scope?
What determines their ...
1002
votes
19
answers
188k
views
Why should C++ programmers minimize use of 'new'?
I stumbled upon Stack Overflow question Memory leak with std::string when using std::list<std::string>, and one of the comments says this:
Stop using new so much. I can't see any reason you ...
155
votes
8
answers
42k
views
When and why will a compiler initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?
I know that the compiler will sometimes initialize memory with certain patterns such as 0xCD and 0xDD. What I want to know is when and why this happens.
When
Is this specific to the compiler used?
...
527
votes
26
answers
258k
views
What uses are there for "placement new"?
Has anyone here ever used C++'s "placement new"? If so, what for? It looks to me like it would only be useful on memory-mapped hardware.
197
votes
9
answers
694k
views
R memory management / cannot allocate vector of size n Mb
I am running into issues trying to use large objects in R. For example:
> memory.limit(4000)
> a = matrix(NA, 1500000, 60)
> a = matrix(NA, 2500000, 60)
> a = matrix(NA, 3500000, 60)
...
1514
votes
5
answers
3.1m
views
What are the -Xms and -Xmx parameters when starting JVM?
What are the Xms and Xmx parameters in JVMs - and what are their default values?
1093
votes
17
answers
1.2m
views
How do I determine the size of an object in Python?
How do I get the size occupied in memory by an object in Python?
113
votes
11
answers
26k
views
When to use -retainCount?
I would like to know in what situation did you use -retainCount so far, and eventually the problems that can happen using it.
Thanks.
840
votes
10
answers
356k
views
How do I discover memory usage of my application in Android?
How can I find the memory used on my Android application, programmatically?
I hope there is a way to do it. Plus, how do I get the free memory of the phone too?
640
votes
10
answers
920k
views
How can I explicitly free memory in Python?
I wrote a Python program that acts on a large input file to create a few million objects representing triangles. The algorithm is:
read an input file
process the file and create a list of triangles, ...
626
votes
20
answers
395k
views
In what cases do I use malloc and/or new?
I see in C++ there are multiple ways to allocate and free data and I understand that when you call malloc you should call free and when you use the new operator you should pair with delete and it is a ...
777
votes
8
answers
388k
views
Which Python memory profiler is recommended? [closed]
I want to know the memory usage of my Python application and specifically want to know what code blocks/portions or objects are consuming most memory.
Google search shows a commercial one is Python ...
336
votes
13
answers
224k
views
How do malloc() and free() work?
I want to know how malloc and free work.
int main() {
unsigned char *p = (unsigned char*)malloc(4*sizeof(unsigned char));
memset(p,0,4);
strcpy((char*)p,"abcdabcd"); // **deliberately ...
362
votes
24
answers
898k
views
Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php [duplicate]
This error message is being presented, any suggestions?
Allowed memory size of 33554432 bytes exhausted (tried to allocate
43148176 bytes) in php
308
votes
15
answers
223k
views
What is the difference between new/delete and malloc/free?
What is the difference between new/delete and malloc/free?
Related (duplicate?): In what cases do I use malloc vs new?
550
votes
28
answers
144k
views
Tricks to manage the available memory in an R session
What tricks do people use to manage the available memory of an interactive R session? I use the functions below [based on postings by Petr Pikal and David Hinds to the r-help list in 2004] to list (...
293
votes
10
answers
144k
views
Is "delete this" allowed in C++?
Is it allowed to delete this; if the delete-statement is the last statement that will be executed on that instance of the class? Of course I'm sure that the object represented by the this-pointer is ...
233
votes
7
answers
204k
views
What is the difference between delete and delete[]?
What is the difference between delete and delete[] operators in C++?
46
votes
6
answers
13k
views
Is delete[] equal to delete? [duplicate]
IP_ADAPTER_INFO *ptr=new IP_ADAPTER_INFO[100];
if I free using
delete ptr;
will it lead to memory leak, if not then why ?
This is disassembly code generated by VS2005
; delete ptr;
0041351D mov ...
473
votes
12
answers
501k
views
Total memory used by Python process?
Is there a way for a Python program to determine how much memory it's currently using? I've seen discussions about memory usage for a single object, but what I need is total memory usage for the ...
397
votes
14
answers
476k
views
Deleting Objects in JavaScript
I'm a bit confused with JavaScript's delete operator. Take the following piece of code:
var obj = {
helloText: "Hello World!"
};
var foo = obj;
delete obj;
After this piece of code has been ...
181
votes
4
answers
230k
views
Releasing memory in Python
I have a few related questions regarding memory usage in the following example.
If I run in the interpreter,
foo = ['bar' for _ in xrange(10000000)]
the real memory used on my machine goes up to 80....
225
votes
5
answers
233k
views
Choice between vector::resize() and vector::reserve()
I am pre-allocating some memory to my a vector data member. Example:
class A {
vector<string> t_Names;
public:
A () : t_Names(1000) {}
};
At some point in time, if the t_Names.size()...
76
votes
6
answers
46k
views
Does GC release back memory to OS?
When the garbage collector runs and releases memory does this memory go back to the OS or is it being kept as part of the process. I was under the strong impression that the memory is never actually ...
170
votes
7
answers
346k
views
Increasing (or decreasing) the memory available to R processes
I would like to increase (or decrease) the amount of memory available to R. What are the methods for achieving this?
73
votes
2
answers
28k
views
How to debug memory leaks when Leaks instrument does not show them?
I have an iOS app written in Swift that is leaking memory - in certain situation some objects should be released but they are not. I have learnt about the issue by simply adding deinit debug messages ...
365
votes
8
answers
226k
views
Is it safe to delete a NULL pointer?
Is it safe to delete a NULL pointer?
And is it a good coding style?
282
votes
6
answers
84k
views
C++ new int[0] -- will it allocate memory?
A simple test app:
cout << new int[0] << endl;
outputs:
0x876c0b8
So it looks like it works. What does the standard say about this? Is it always legal to "allocate" empty block of memory?...
594
votes
4
answers
401k
views
What is private bytes, virtual bytes, working set?
I am trying to use the perfmon windows utility to debug memory leaks in a process.
This is how perfmon explains the terms:
Working Set is the current size, in bytes, of the Working Set of this ...
66
votes
7
answers
18k
views
Defining methods via prototype vs using this in the constructor - really a performance difference?
In JavaScript, we have two ways of making a "class" and giving it public functions.
Method 1:
function MyClass() {
var privateInstanceVariable = 'foo';
this.myFunc = function() { alert(...
116
votes
3
answers
62k
views
Is it necessary to use autoreleasepool in a Swift program?
On page 17 of this WWDC14 presentation, it says
Working with Objective-C? Still have to manage autorelease pools
autoreleasepool { /* code */ }
What does that mean? Does it mean that if my code ...
72
votes
16
answers
50k
views
How can I get the size of an array from a pointer in C?
I've allocated an "array" of mystruct of size n like this:
if (NULL == (p = calloc(sizeof(struct mystruct) * n,1))) {
/* handle error */
}
Later on, I only have access to p, and no longer have n. Is ...
119
votes
7
answers
330k
views
Difference between static memory allocation and dynamic memory allocation
I would like to know what is the difference between static memory allocation and dynamic memory allocation?
Could you explain this with any example?
225
votes
9
answers
336k
views
Where in memory are my variables stored in C?
By considering that the memory is divided into four segments: data, heap, stack, and code, where do global variables, static variables, constant data types, local variables (defined and declared in ...
470
votes
17
answers
233k
views
How to allocate aligned memory only using the standard library?
I just finished a test as part of a job interview, and one question stumped me, even using Google for reference. I'd like to see what the StackOverflow crew can do with it:
The memset_16aligned ...
72
votes
7
answers
167k
views
Dynamically allocating an array of objects
I have a class that contains a dynamically allocated array, say
class A
{
int* myArray;
A()
{
myArray = 0;
}
A(int size)
{
myArray = new int[size];
}
~...
71
votes
7
answers
9k
views
Array placement-new requires unspecified overhead in the buffer?
5.3.4 [expr.new] of the C++11 Feb draft gives the example:
new(2,f) T[5] results in a call of operator new[](sizeof(T)*5+y,2,f).
Here, x and y are non-negative unspecified values representing array ...
197
votes
16
answers
120k
views
Setting Objects to Null/Nothing after use in .NET
Should you set all the objects to null (Nothing in VB.NET) once you have finished with them?
I understand that in .NET it is essential to dispose of any instances of objects that implement the ...
115
votes
9
answers
48k
views
When you exit a C application, is the malloc-ed memory automatically freed?
Let's say I have the following C code:
int main () {
int *p = malloc(10 * sizeof *p);
*p = 42;
return 0; //Exiting without freeing the allocated memory
}
When I compile and execute that C ...
2
votes
1
answer
2k
views
bds 2006 C hidden memory manager conflicts (class new / delete[] vs. AnsiString)
I am using BDS 2006 Turbo C++ for a long time now and some of my bigger projects (CAD/CAM,3D gfx engines and Astronomic computations) occasionally throw an exception (for example once in 3-12 months ...
-2
votes
2
answers
1k
views
Dynamic allocation of an unknown matrix in C
I need to take a file that is inputted by the user and multiply it by another file. That much I know how to do.
The problem is one file is an array and the other is a matrix.
I need to scan in ...
104
votes
4
answers
118k
views
Operator new initializes memory to zero
There is such code:
#include <iostream>
int main(){
unsigned int* wsk2 = new unsigned int(5);
std::cout << "wsk2: " << wsk2 << " " << *wsk2 << std::endl;
...
17
votes
3
answers
6k
views
Why is malloc not "using up" the memory on my computer?
So I have this program that allocates 256 MB of memory, and after the user presses ENTER it frees the memory and terminates.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
...
137
votes
12
answers
251k
views
How to get current memory usage in android?
I have used /proc/meminfo and parsed command response.however it result shows that :
MemTotal: 94348 kB
MemFree: 5784 kB
means. it shows there is only 5MB free memory. Is it ...
180
votes
9
answers
234k
views
Memory address of variables in Java
Please take a look at the picture below.
When we create an object in java with the new keyword, we are getting a memory address from the OS.
When we write out.println(objName) we can see a "special" ...
48
votes
5
answers
12k
views
when does Python allocate new memory for identical strings?
Two Python strings with the same characters, a == b,
may share memory, id(a) == id(b),
or may be in memory twice, id(a) != id(b).
Try
ab = "ab"
print id( ab ), id( "a"+"b" )
Here Python recognizes ...
805
votes
2
answers
101k
views
Why is my program slow when looping over exactly 8192 elements?
Here is the extract from the program in question. The matrix img[][] has the size SIZE×SIZE, and is initialized at:
img[j][i] = 2 * j + i
Then, you make a matrix res[][], and each field in here is ...
211
votes
8
answers
75k
views
Why is @autoreleasepool still needed with ARC?
For the most part with ARC (Automatic Reference Counting), we don't need to think about memory management at all with Objective-C objects. It is not permitted to create NSAutoreleasePools anymore, ...
117
votes
20
answers
77k
views
What's so wrong about using GC.Collect()?
Although I do understand the serious implications of playing with this function (or at least that's what I think), I fail to see why it's becoming one of these things that respectable programmers ...