Questions tagged [dynamic-allocation]

Use for questions related to *dynamic allocation of memory*, such as `malloc()` in C, `new` in C++, etc. . Please notice that the tag is not language specific.

From Wikipedia: "C dynamic memory allocation refers to performing manual memory management for dynamic memory allocation in the C programming language via a group of functions in the C standard library, namely malloc, realloc, calloc and free."

Please notice that the tag is not language specific.

506 questions
2
votes
1 answer

How to dynamically load and unload an F# dll into an C# project?

I understand how to load an F# dll into a project. But I would like to be able to load specific parts of them dynamically and without a big performance hit from the dll being purely functional.
Jesse Glover
  • 325
  • 3
  • 14
2
votes
1 answer

Fortran pointer to derived types and default initialisation

Fortran 2003 derived types have a nice feature of default initialization. type TTest integer :: a integer :: b = 1 integer, pointer :: p1, p2 => null() end type TTest then any declared variable of type (TTest) will have initialized …
Roux
  • 435
  • 3
  • 12
2
votes
4 answers

Freed pointer reallocaing 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; …
2
votes
1 answer

C++ - overloading operator >> for my string class

I realized string class MyString. Here is code: #include #include using std::cout; using std::endl; class MyString{ private: char * content; int length; void copy(const MyString & source); public: …
etf
  • 265
  • 4
  • 12
2
votes
1 answer

How to properly use memory dynamically allocated in C++ dll within Matlab

I wrote a C++ interface dll to call some key image processing statistical measuring methods from Intel's Integrated Performance Primitives (IPP) library so we can exploit SIMD capabilities. I was able to pass two 2D Matlab arrays over into the…
jxramos
  • 7,356
  • 6
  • 57
  • 105
2
votes
1 answer

How to implement dynamic arrays in C?

I am writing this code using dynamic memory allocation, for student records as indicated, this code suppose to be simple , I am obviously allocating elements in their correct places the right way, but when it comes to printing them, it gives me a…
aero
  • 372
  • 1
  • 4
  • 18
2
votes
1 answer

Allocate memory dynamically of a vector of struct

I can't access my pointer by index notation in main function. The way I'm passing the pointer as paramter to the functions, is that right ? I tried without the & and It didnt work neither. Here is my code: //My Struct typedef struct{ int a; …
PlayHardGoPro
  • 2,791
  • 10
  • 51
  • 90
2
votes
4 answers

Allocate and Delete memory for pointers in a struct C++

Given the following struct declaration: struct Student { char * name; int * test; float gpa; }; int main() { Student *newObject = new Student; newObject->name = new char[10]; newObject->test = new int; return 0; } My…
ReeSSult
  • 486
  • 3
  • 7
  • 21
2
votes
2 answers

Why do Rust's sort methods allocate memory?

Methods like sort_by on std::slice::MutableSliceAllocating or sort_by on collections::vec::Vec are documented to "allocate approximately 2 * n, where n is the length". I don't think that good C++ std::sort implementations allocate (on the heap) and…
kmky
  • 783
  • 6
  • 17
2
votes
1 answer

Allocating 2D array with pointer to fixed-size array

Is it valid in C to dynamically allocate 2d arrays this way? //fixed size int (*arr2d)[9][9]=malloc(sizeof(int[9][9])); //variable size int x=5,y=3; int (*arr2d)[x][y]=malloc(sizeof(int[x][y])); //assignment (*arr2d)[0][1]=2; EDIT: By "valid" I…
humodz
  • 600
  • 3
  • 8
2
votes
3 answers

Trying to create 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 Vec { public: //TYPEDEFS typedef T* iterator; typedef const T* const_iterator; typedef…
jld
  • 83
  • 1
  • 6
2
votes
2 answers

Trying to figure out the mistake I made with pointers (C++)

I'm a newbie to C++, currently working on a project on dynamic allocation in my C++ class. I can't for the life of me figure out where I went wrong with pointers in this program. I have three different .cpp files that I'm working with, we're…
user1888527
  • 543
  • 1
  • 7
  • 12
2
votes
5 answers

Dynamic allocation and pointers

int *ptr; ptr=(int *)malloc(sizeof(int)*2); ptr=100; /*What will happen if I put an asterisk(*) indicating *ptr=100? */ ptr++; printf("ptr=%d",*ptr); free(ptr); So, I wanted the pointer to…
Sabbarish
  • 50
  • 8
2
votes
1 answer

C++ : operator new and default constructor

I'm having trouble understanding how to use dynamic allocation with constructors. I use in my code a class named graph (which is just a bool 2-d matrix representing the edges between the nodes) with the following constructor/destructor (there others…
Demod
  • 53
  • 1
  • 1
  • 8
2
votes
5 answers

Trying To Learn Dynamic Memory Allocation in C

I am trying to learn dynamic memory allocation and structures and I have some questions. First of all #include #include #include int main() { int *number; number = malloc(1*sizeof(int)); int a; …
ossobuko
  • 851
  • 8
  • 25