memory-allocation

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 ...

How to implement a variable-length ‘string’-y in C

I’ve googled quite a bit, but I can’t find information on how variable-length strings are generally implemented in higher-level languages. I’m creating my own such language, and am not sure where to start with strings. I have a struct describing the string type, and then a create function that allocates such a ‘string’: /* A safer `str...

Best way to handle and report memory allocation errors due to integer overflow in Objective-C?

To begin with, let me say that I understand how and why the problem I'm describing can happen. I was a Computer Science major, and I understand overflow/underflow and signed/unsigned arithmetic. (For those unfamiliar with the topic, Apple's Secure Coding Guide discusses integer overflow briefly.) My question is about reporting and recov...

Delphi strings and reference counting

Delphi uses reference counting with strings. Do this mean that there is only one memory allocation for '1234567890' and all a,b,c,d, e and f.s reference to it? type TFoo = class s: string; end; const a = '1234567890'; b = a; c : string = a; var d: string; e: string; f: TFoo; function GetS...

Dynamic allocation of memory

Lets consider following two codes First: for (int i=0;i<10000000;i++) { char* tab = new char[500]; delete[] tab; } Second: for (int i=0;i<10000000;i++) { char tab[500]; } The peak memory usage is almost the same, but the second code runs about 20 times faster than the first one. Question Is it because in first code ar...

Memory allocation for singleton pattern

if we use singleton pattern in our web application, when free the specified memory that allocated to our class? ...

Finding leaks under GeneralBlock-16?

If ObjectAlloc cannot deduce type information for the block, it uses 'GeneralBlock'. Any strategies to get leaks from this block that may eliminate the need of my 'trial and error' methods that I use? The Extended Detail thing doesn't really do it for me as I just keep guessing. ...

can we use a pointer freed earlier?

Hi, I have a question regarding free() in C. Suppose I have a pointer to some struct (say node *ptr).. after freeing it can i Initialize it to NULL and make it point to some new location using malloc() or realloc()? For Example: node *ptr=NULL; ptr=realloc(ptr,sizeof(node)); //works exactly like malloc /* Do some operations on ptr */...

How to create a C-style array without calling default constructors?

I am writing a memory-managing template class in which I want to create a C-style array of fixed size, to serve as a heap. I keep the objects stored in an array like this: T v[SIZE]; As this only serves the role as a heap that can hold T objects, I don't want the T default constructor to get automatically called for every object in th...

Classes, Static Methods, or Instance Methods - Memory Consumption and Executable Size in Compiled Languages?

I keep wondering about this to try to improve performance and size of my Flex swfs, how do classes vs. static methods vs. instance methods impact performance and the final compiled "executable's" size? Thinking how it might be possible to apply something like HAML and Sass to Flex... Say I am building a very large admin interface with...

Dynamically allocating an array of size 0

Possible Duplicate: C++ new int[0] will it allocate memory? What is supposed to happen when i do this: int * MakeArray( std::size_t Size ) { return new int[ Size ]; } MakeArray( 0 ); We had this is out code (using VC++ 2005), and the debug-heap complained about a memory-block of size 0 that was not deallocated. Is it ...

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...

Dereferencing issue in C

Hi, when I try to compile my code below I get the following errors: error C2440: '>=' : cannot convert from 'double *' to 'double' error C2440: '>=' : cannot convert from 'double *' to 'double' I believe I'm dereferencing everything correctly #define TRUE 1 #define FALSE 0; #include <stdio.h> typedef struct Con{ double samTime[2]...

What is the fastest way to initialize all elements in an array to NaN?

In C# .NET, what is the fastest way to initialize an array of doubles to NaN? Here is how I am presently initializing an array with many elements. int length = array.Length; for(int i = 0; i < length; i++) { array[i] = double.NaN; } Is there a faster way? ...

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...

Java interfaces... Dynamic or static memory?

Where do java interfaces reside in memory? Do they reside in the heap or the stack, or maybe global memory? I am thinking the are in the stack since they are created at compile time. Am I correct? ...

Why does this program crash: passing of std::string between DLLs

Hello together. I have some trouble figuring out why the following crashes (MSVC9): //// the following compiles to A.dll with release runtime linked dynamically //A.h class A { __declspec(dllexport) std::string getString(); }; //A.cpp #include "A.h" std::string A::getString() { return "I am a string."; } //// the following compil...

Why can I write and read memory when I haven't allocated space?

Hi, I'm trying to build my own Hash Table in C from scratch as an exercise and I'm doing one little step at a time. But I'm having a little issue... I'm declaring the Hash Table structure as pointer so I can initialize it with the size I want and increase it's size whenever the load factor is high. The problem is that I'm creating a t...

Allocating memory for a char array to concatenate a known piece of text and an integer

I want to concatenate a piece of text, for example "The answer is " with a signed integer, to give the output "The number is 42". I know how long the piece of text is (14 characters) but I don't know how many characters the string representation of the number will be. I assume the worst case scenario, the largest signed 16-bit integer ...

static array allocation issue!

I want to statically allocate the array. Look at the following code, this code is not correct but it will give you an idea what I want to do class array { const int arraysize; int array[arraysize];//i want to statically allocate the array as we can do it by non type parameters of templates public: array(); }; array::array():array...