allocation

Passing around fixed-size arrays in C++?

Basically I'd like to do something like that: int[3] array_func() { return {1,1,1}; } int main(int argc,char * argv[]) { int[3] point=array_func(); } But that doesn't seem legal in C++. I know I can use vectors, but since I know the size of the array is a constant, it seems like a loss of performance is likely to occur. I'd a...

how to allocate a 2D array of pointers in c++

I'm trying to make a pointer point to a 2D array of pointers. what is the syntax and how would i access elements. ...

JVM and CLR allocation optimization

Do the JVM and .NET VM allocate objects on the stack when it is obvious to the runtime that an objects lifetime is limited to a certain scope? ...

C++ dynamic allocated array

Hi, I'm doing some assignment and got stuck at one point here. I am trying to write an list_add() function. The first functionality of it is to add values to the array. The second functionality for it is to increase the size of the array. So it works much like a vector. I dunno if I get it right though. What I tried is to create a new dy...

How is memory allocated for a static multi-dimensional array?

All, This has been bugging me for a while now. In C\C++( i guess java and .NET as well) we do not have to specify the row index in a multi-dimensional array. So, for example i can declare an array of ints as such: int Array[][100]; I think static arrays in general are represented as contiguous memory on the stack. So, taking a column...

CUDA allocating array of arrays

Hi, I have some trouble with allocate array of arrays in CUDA. void ** data; cudaMalloc(&data, sizeof(void**)*N); // allocates without problems for(int i = 0; i < N; i++) { cudaMalloc(data + i, getSize(i) * sizeof(void*)); // seg fault is thrown } What did I wrong? ...

alloc/init and the setting of instance variables

It is my understanding that, in addition to allocating memory, alloc sets all instance variables (with the exception of the isa variable) to zero or to the equivalent type for zero, such as nil, NULL, and 0.0. But I recently read the following about init: Initialization sets the instance variables of an object to reasonable and ...

How do I over-allocate memory using new to allocate variables within a struct?

So I have a couple of structs... struct myBaseStruct { }; struct myDerivedStruct : public myBaseStruct { int a, b, c, d; unsigned char* ident; }; myDerivedStruct* pNewStruct; ...and I want to dynamically allocate enough space so that I can 'memcpy' in some data, including a zero-terminated string. The size of the base struct...

Struct instantiation from void pointer buffer

Here's some C++ code that just looks funny to me, but I know it works. There is a struct defined, and in the program we allocate memory using a void pointer. Then the struct is created using the allocated buffer. Here's some code typedef struct{ char buffer[1024]; } MyStruct int main() { MyStruct* mystruct_ptr = 0; void* ...

array of pointers allocation

Hi all, typedef struct { struct table **symbols; // array of the stack int top; //index of the top element int size; //maximum size of the stack }stack; void *createStack(int size) { stack *stck; stck = (stack *) malloc(sizeof(stack)); stck->symbols = .... stck->size = size; stck->top = -1; printf("stack is created...

Dynamically allocate C struct?

Hi, I want to dynamically allocate a C struct: typedef struct { short *offset; char *values; } swc; Both 'offset' and 'values' are supposed to be arrays, but their size is unknown until runtime. How can I dynamically allocate memory for my struct and the struct's arrays? ...

How to write a thread-safe and efficient, lock-free memory allocator in C?

How to write a thread-safe and efficient, lock-free memory allocator in C? By efficient I mean: Fast allocation & deallocation Optimal memory usage (minimal wastage and no external fragmentation) Minimal meta-data overhead ...

Looking for resource management/allocation system

What I need is a system I can define simple objects on (say, a "Server" than can have an "Operating System" and "Version" fields, alongside other metadata (IP, MAC address, etc)). I'd like to be able to request objects from the system in a safe way, such that if I define a "Server", for example, can be used by 3 clients concurrently, the...

a library forces global overloads of new/delete on me!

I'm maintaining a plugin (implemented as a dll) for a big closed source application. This has been working fine for years. However, with the latest update to it's SDK the vendor overloaded global operators new and delete. This causes lots of trouble for me. What happens is that my plugin allocates a string. I pass this string into a stat...

Dynamic Allocation of buttons in C Builder 6?

Hi, I want to make a game of TicTacToe. WhenI resize the window I want to appear more buttons on the interface. From a matrix of 3x3 to 4x4 etc up to 9x9, depending on how much I resize the window. How do I do this? I will make a free web site design to whoever provides me with a working answer (and something extra for the full program...

Java object creation and memory size

Hi All, I am trying understand about the size that a Java object will be allocated with when created using a new operator. Consider that i am creating a class `public class NewClass { NewClass() { } }` when i create an instance of NewClass using NewClass nc = new NewClass();. what is the size of the NewClass that gets created in the...

Finding memory allocation error

I'm getting memory allocation errors (and a subsequent crash) on the following simplified code: std::wstring myKey = L"str_not_actually_constant"; MyType obj; Read( obj ); std::map<std::wstring, MyType> myMap; myMap[myKey] = obj; // Sometimes allocation error (1) ... Read( MyType& obj ) { obj.member1 = ReadFromFuncThatMayBeProblem()...

Java unrequested memory allocation

The following piece of code observed under JConsole shows a constant increase of the heap size. The heap reach a maximum of 25mb and then the GC runs and decease the heap size to almost 3MB. Is this the expected behavior ? I'm very surprised! public class Dummy { public static void main(String[] args) { System.out.println("...

Do I have to allocate everything on the heap (that gets stored within) heap containers?

Hi, I'm overriding the new operator to manually allocate heap space when using the new keyword. Usually using a stack-allocated heap container with pointers to its items- CArray<CObject*> objects; -is fine. but I'm picky and I want to make buffers- CArray<CObject> objects; -that let me modify POD types. So, I allocate them on the ...

Objective-C memory management (alloc and autorelease)

When you allocate and initialize and object, and then want to return that object, how are you supposed to return it? I have the following code: NSXMLDocument* fmdoc = [[NSXMLDocument alloc] initWithContentsOfURL:trackInfoUrl options:NSXMLDocumentTidyXML error:&err]; return [fmdoc autorelease]; Is this correct? ...