unordered_map: clear () does not release heap on clear ()

This question already has an answer here: How can I force the STL memory cache to clear? 2 answers I am using unordered_map using g++ 4.9.2 on Solaris 10, but surprisingly I found that clear() does not release heap. Here's the sample code: #include <

dynamic allocation of table function 2d

So i have a program in c structured in 3 files : main,alloc.h and alloc.c : In main i have the decclaration of a pointer to another pointer to which i intend to alloc a n*m array : #include <stdio.h> #include <stdlib.h> #include "alloc.h&

Get the size (in bytes) of an object on the heap

I'm aware you can use MemoryLayout<T>.size to get the size of a type T. For example: MemoryLayout<Int32>.size // 4 However, for class instances (objects), MemoryLayout<T>.size returns the size of the reference to the object (8 bytes on 6

What is the best way to destroy the C structure

In C++ we have structures which have a constructor and the destructor. It makes life much easier especially when it going to have the pointers, therefore dynamically allocated memory in the structure. You can even use std::shared_pointer library to d

Released from the pointer itself?

When I dynamically allocate a structure, and then try to free it, it seems to reallocate it. typedef struct OBJ_T { int param1, param2; } OBJ; OJB* Construct(int par1, int par2) { OBJ* x = malloc(sizeof(OBJ)); x->param1 = par1; x->param2 = par2; ret

change the table from fixed size to dynamic in C

I have an char array of fixed size in C application. I am passing that array to some function and from there I am passing it to multiple functions. So that the array gets filled in some of the functions based on some condition. Since I am sending a f

Trying to create a copy function in a vector class

I am working on implementing a vector class but cannot figure out how to write a function to copy one vector into another. template <class T> class Vec { public: //TYPEDEFS typedef T* iterator; typedef const T* const_iterator; typedef unsigned int s

What's wrong with using dynamically allocated tables in C ++?

This question already has an answer here: Why should C++ programmers minimize use of 'new'? 17 answers Understanding the meaning of the term and the concept - RAII (Resource Acquisition is Initialization) 11 answers Like the following code : int size

& ldquo; search & rdquo; linked list in c

I added a "search" function for my linked list menu and I don't know what is wrong in my code. When I enter a search key which is a name that is not in the list, instead of printing the "Search Key: %s Not Found!", the program will jus

Convert list to array using toArray

Please the read the question instead of assuming this is a duplicate. In the codebase I am working on I have a list of Foo objects: private List<Foo> fooList; Could someone please explain to me the difference(s) between this line of code: Foo foos[]

Assigning an array of dynamically allocated pointers

Having a lot of trouble with this after sifting through many posts on here. Everything compiles but I get a crash right here during this function which should be dynamically allocating the addresses of one array into this array of pointers. I see one

What is the use of creating objects on the free store?

void fun() { A *a = new A; //Here A is a class } //a should be deleted in fun()'s scope int main() { fun(); return 0; } The object created exists on the free store and cannot be used by the main() function. The why should the objects be created on th

Passing a dimension of a 2D dynamic array to a function

I have a 2D array that I'm dynamically allocating at runtime, like so accData = calloc(nbox, sizeof(double *)); for(bb = 0; bb < nbox; bb++) accData[bb] = calloc(usedTime * usedChan, sizeof(double *)); and I want to only pass the second dimension to

Core dumped error on Ubuntu with matrix functions in C ++

I'm trying to build a program in C++ that works a lot with matrices and functions of matrices. My code compiles normally, but when I try to execute it I get the message: Segmentation fault (core dumped) My code has a lot of function that look like th

Overloaded istream operator with dynamic memory allocation

Hello so I am confused with my istream& operator>>. I have to overload this operator to take input for a class that is using dynamic memory allocation for a C string. My Employee.h file is #include <iostream> using namespace std; const dou

Pass a & ldquo; normal & rdquo; 2d table to work as **

I want to do this but it doesn't work. Is it possible to do it or do I have to declare A as double pointer float**? Note that I need an universal function for various array dimensions so I can't change the function arguments. void func(float** arr, i

Globally dynamically allocated structure in C ++

I'm having problems trying to use an array of structs which doesn't have an initial size. How do I do this? This is my struct: struct carbon { double temp; double mass; rowvec::fixed<3> position; rowvec::fixed<3> velocity; rowvec::fixed<3&g

Returns an array of strings from a function

I need to return a char** but when I try to do this, the compiler tells me that I want to return the address of a local variable. How can I do that? I know that I should allocate space for this variable but how? Here is my code, but the second printf

Why should Objective-C objects be allocated dynamically?

Why do Objective-c objects have to be dynamically allocated? Why do I have to make it a pointer to an object, unlike in C++ I can create them on stack? Thanks.the primary reason: not knowing how much stack size to reserve. existing conventions and us