dynamic-memory-allocation

When to use Malloc instead of New

Duplicate of: In what cases do I use malloc vs new? Just re-reading this question: http://stackoverflow.com/questions/807939/what-is-the-difference-between-new-and-malloc-and-calloc-in-c-closed I checked the answers but nobody answered the question: When would I use malloc instead of new? There are a couple of reasons (I can think ...

Simple C implementation to track memory malloc/free?

programming language: C platform: ARM Compiler: ADS 1.2 I need to keep track of simple melloc/free calls in my project. I just need to get very basic idea of how much heap memory is required when the program has allocated all its resources. Therefore, I have provided a wrapper for the malloc/free calls. In these wrappers I need to incre...

Resizing dynamic stack allocations in C++

Hi, I'm writing a small ray tracer using bounding volume hierarchies to accelerate ray tracing. Long story short, I have a binary tree and I might need to visit multiple leafs. Current I have a node with two children left and right, then during travel() if some condition, in this example intersect(), the children are visited: class Bo...

How well does Valgrind handle threads and machine-level synchronization instructions?

I have a highly parallel Windows program that uses lots of threads, hand-coded machine synchronization instructions, and home-rolled parallel-safe storage allocators. Alas, the storage management has a hole (not a synchonization hole in the allocators, I'm pretty sure) and I'd like to find it. Valgrind has been suggested as a good tool...

How much memory should you be able to allocate?

Background: I am writing a C++ program working with large amounts of geodata, and wish to load large chunks to process at a single go. I am constrained to working with an app compiled for 32 bit machines. The machine I am testing on is running a 64 bit OS (Windows 7) and has 6 gig of ram. Using MS VS 2008. I have the following code: ...

Segmentation fault when malloc/free appear in loop in C

Hi there, I have a program that basically looks like: typedef struct cpl_def { int A; int B; int OK; struct cpls *link; }cpls; int main(void) { int n1, n2; int num = 300; /* say */ int *a; ...

Can you declare a pointer on the heap?

This is the method for creating a variable on the heap in C++: T *ptr = new T; ptr refers to a pointer to the new T, obviously. My question is, can you do this: T *ptr = new T*; That seems like it could lead to some very, very dangerous code. Does anyone know if this is possible/how to use it properly? ...

Object Allocations going crazy.

I have CFArray and CFString allocations going all red while checking on the Instruments Object Alloc. Object seem to be alive but not being used, this because the used part of the histogram is 1/10th of the total histogram (which has turned red) in both cases. the app is a photo library application with 7 view controllers. Loading up ...

C++ dynamically allocated array of statically dimensioned arrays

I need to create a structure that holds a variable number of 'char[2]'s, i.e. static arrays of 2 chars. My question is, how do I allocate memory for x number of char[2]. I tried this (assuming int x is defined): char** m = NULL; m = new char[x][2]; ... delete [] m; (it didn't work) I realise I could use std::vector<char[2]> as a co...

How do dynamically allocated arrays get freed in C++?

I know that you have to do it like this: int * p; p = new int[10]; //use array delete [] p; Now my question is: Since it's not explicitly stated, how is it possible that the correct amount of memory is freed? Do the OS keep track of the allocated memory and its starting address? ...

Dynamic arrays size and dynamic arrays allocators in VC++

hello, guys I'm confused a little while writing own tiny discovering program to clear up how Visual C++ allocates the memory for dynamic arrays. I must note, I have never met technical documents that describe this question on new[]/delete[] operators for any C++ implementation. Initially I thought that new[] and delete[] are something ...

Storing heterogeneous objects in vector with stack-allocated objects

Storing objects in heterogeneous vector with stack-allocated objects Hello, Say I have an abstract class CA, derived into CA1, CA2, and maybe others. I want to put objects of these derived types into a vector, that I embbed into a class CB. To get polymorphism right, I need to store a vector of pointers: class CB { std::vector <C...

Storing data of unknown size in C++

Hi, I've been using PHP for about 4 years, however I've come across a problem that requires something with slightly (:P) better performance and so I've chosen C++. The program I'm writing is a Linux daemon that will scan a MySql database for URL's to load, load them using cURL, search for a specified string, and then update the databas...

Pointer won't return with assigned address

I'm using Qt Creator 4.5 with GCC 4.3 and I'm having the following problem that I am not sure is Qt or C++ related: I call a function with a char * as an input parameter. Inside that function I make a dynamic allocation and I assign the address to the char *. The problem is when the function returns it does not point to this address any...

dynamic allocation/deallocation of 2D & 3D arrays

I know about algorithms to allocate/deallocate a 2D array dynamically, however I'm not too sure about the same for 3D arrays. Using this knowledge and a bit of symmetry, I came up with the following code. (I had a hard time visualizing in 3D during coding). Please comment on the correctness and suggest any better alternative (efficien...

Allocating an array of Derived without new[]: Pointer to Base vtable is bad

Basically, I have a pure virtual class Base, and a concrete class Derived which inherits from Base. I then allocate a piece of memory and treat it as an array of Derived via a simple cast. Then, I populate the array using =. Finally, I loop through the array, trying to call the virtual method GetIndex that is declared in Base and defined...

Pass by reference 2D dynamically allocated double array in C

Hi, I'm trying to convert my string into a dynamic array of doubles. Each space of my string represents a column, each ";" represents a new row. When this code runs, it only works for when *F[0][col]. When it gets to *F[1][col] it gives me the error "Unhandled exception at 0x00e4483c in CCode.exe: 0xC0000005: Access violation reading l...

gdb gives an error, but program runs fine

I have a simple C program which has a pointer to a character array. To initiate it, I use malloc, and resize then set it x number of times later on in the program. When I resize it once with realloc, gdb doesn't show any errors, however, if I try calling the resize function again, gdb shows the following error: warning: Invalid Address...

dynamic memory in QList

I don't have much experience with QT and this problem came out today. QList<int> memList; const int large = 100000; getchar(); for (int i=0; i<large; i++) { memList.append(i); } cout << memList.size() << endl; getchar(); for (int i=0; i<large; i++) { memList.removeLast(); } cout << memList.size() << endl; getchar(); After ...

Is it safe to pass (synchronously) stack-allocated memory to other thread?

Recently I heard that memory in the stack is not shared with other thread and memory in the heap is shared with other threads. I normally do: HWND otherThreadHwnd; DWORD commandId; // initialize commandId and otherThreadHwnd struct MyData { int data1_; long data2_; void* chunk_; }; int abc() { MyData myData; // initialize ...