dynamic-memory-allocation

QList memory deallocation

I'm trying to free memory after using QList, but it doesn't seem to work properly. Here's my code: QList<double> * myList; myList = new QList<double>; double myNumber; cout << "CP1" << endl; getchar(); // checkpoint 1 for (int i=0; i<1000000; i++) { myNumber = i; myList->append(myNumber); cout << myList->size() << endl; ...

Dynamic memory and inherited structs in C++

Say I have some structs like this: struct A{ int someInt; } struct B : public A{ int someInt; int someOtherInt; } And a class: class C{ A *someAs; void myFunc(A *someMoreAs){ delete [] someMoreAs; } } would this cause a problem: B *b=new B[10]; C c; c.myFunc(b); Because it's deleting b, thinking that it's of type A, whi...

memory allocation and inherited classes in C++

Say I have these structs: struct Base{ ... } struct Derived:public Base{ //everything Base contains and some more } I have a function in which I want to duplicate an array of these and then alter it. void doStuff(Base *data, unsigned int numItems){ Base *newdata = new Base[numItems]; memcpy(newdata, data, numItems*sizeof(Base));...

Dynamic memory allocation question

when you allocate dynamic memory on the heap using a pointer, char *buffer_heap = new char[15]; it would be represented in memory as: ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍýýýý««««««««þþþ why doesn't there be a NULL terminating character at the end instead of ýýýý««««««««þþþ? ...

ALLOCATABLE arrays or POINTER arrays?

Hello, I am writing a new code in Fortran and hesitating between using allocatable arrays or pointer arrays. I read somewhere that allocatable arrays have significant advantages over pointer arrays: 1) More efficient because they are always contiguous in memory 2) No memory leaks are possible Can someone confirm this? Which one wou...

C function: is this dynamic allocation? initializating an array with a changing length

Suppose I have a C function: void myFunction(..., int nObs){ int myVec[nObs] ; ... } Is myVec being dynamically allocated? nObs is not constant whenever myFunction is called. I ask because I am currently programming with this habit, and a friend was having errors with his program where the culprit is he didn't dynamically al...

posix_memalign for std::vector

Is there a way to posix_memalign a std::vector without creating a local instance of the vector first? The problem I'm encountering is that I need to tell posix_memalign how much space to allocate and I don't know how to say sizeof(std::vector<type>(n)) without actually creating a new vector. Thanks ...

What does this dynamic allocation do?

Today, I found out that you can write such code in C++ and compile it: int* ptr = new int(5, 6); What is the purpose of this? I know of course the dynamic new int(5) thing, but here i'm lost. Any clues? ...

How to avoid reallocation using the STL (C++)

This question is derived of the topic: http://stackoverflow.com/questions/2280655/vector-reserve-c I am using a datastructur of the type vector<vector<vector<double> > >. It is not possible to know the size of each of these vector (except the outer one) before items (doubles) are added. I can get an approximate size (upper bound) on th...

GLib atoms and memory chunks

The following code snippet is from The Official GNOME 2 Developer's Guide: GMemChunk my_chunk; my_chunk = g_mem_chunk_new("My Chunk", 42, 42*16, G_ALLOC_AND_FREE); gchar *data[50000]; gint i; /* allocate 40,000 atoms */ for(i = 0; i < 40000; i++) { data...

Delete memory error

Hi, I have the following scenario. class foo { ... private: char *_test; }; void foo::setTest() { if( 0 != _test ) delete [] _test; } The function setTest throws an error when first run as it trying to delete _test when it has not been assigned yet. This is happening because _test is not set to 0X0. Can anyone help me to ...

local and dynamic allocating

I have a tree and I want to release the allocated memory, but I face a problem that a pointer may refers to a variable that isn't dynamically allocated,so how to know wether this pointer refers to dynamic a variable or not ...

Linked List exercise, what am I doing wrong?

Hey all. I'm doing a linked list exercise that involves dynamic memory allocation, pointers, classes, and exceptions. Would someone be willing to critique it and tell me what I did wrong and what I should have done better both with regards to style and to those subjects I listed above? /* Linked List exercise */ #include <iostream>...

Where does memory dynamically allocated reside?

Hello everyone: We know that malloc() and new operation allocate memory from heap dynamically, but where does heap reside? Does each process have its own private heap in the namespace for dynamic allocation or the OS have a global one shared by all the processes. What's more, I read from a textbook that once memory leak occurs, the miss...

C++ new memory allocation fragmentation

I was trying to look at the behavior of the new allocator and why it doesn't place data contiguously. My code: struct ci { char c; int i; } template <typename T> void memTest() { T * pLast = new T(); for(int i = 0; i < 20; ++i) { T * pNew = new T(); cout << (pNew - pLast) << " "; pLast = pNe...

Char * reallocation in C++

Hi, I need to store a certain amount of data in the "char *" in C++, because I want to avoid std::string to run out of memory, when exceeding max_size(). But the data comes in data blocks from the network so I need to use reallocation every time I get the data block. Is there any elegant solution for char * reallocation and concatenatio...

How to Dynamically Allocate Memory Using Assembly and System Calls Under Linux

I'm looking for some good code examples of dynamic memory allocation using an assembly language under Linux and using system calls, not malloc and friends. What are some of the simplest but effective ways to do this? On Intel 386+ computers. ...

Simple dynamic memory allocation bug.

I'm sure you (pros) can identify the bug's' in my code, I also would appreciate any other comments on my code. BTW, the code crashes after I run it. #include <stdlib.h> #include <stdio.h> #include <stdbool.h> typedef struct { int x; int y; } Location; typedef struct { bool walkable; unsigned char walked; // number of...

Constructors for C++ objects

I have class Person as following : class Person { char* name; int age; }; Now I need to add two contructors. One taking no arguments, that inserts field values to dynamically allocated resources. Second taking (char*, int) arguments initialized by initialization list. Last part is to define a destructor showing information ...

Another dynamic memory allocation bug.

I'm trying to allocate memory for a multidimensional array (8 rows, 3 columns). Here's the code for the allocation (I'm sure the error is clear for you) char **ptr = (char **) malloc( sizeof(char) * 8); for (i = 0; i < 3; i++) ptr[i] = (char *) malloc( sizeof(char) * 3); The crash happens when I reference this: ptr[3][0]; ...