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
3
votes
8 answers

Difference between char[] and new char[] when using constant lengths

So this may seem like a widely-answered question, but I'm interested more in the internals of what exactly happens differently between the two. Other than the fact that the second example creates not only the memory, but a pointer to the memory,…
3
votes
2 answers

Resizing a dynamic string causes a memory leak

I start with a very simple program: #include int main(int argv, char** argc) { tb::String test(""); test = "Hello World!"; return 0; } tb::String is my own string class, which was designed to handle both char strings and…
knight666
  • 1,599
  • 3
  • 22
  • 38
2
votes
3 answers

how to use a dynamically created one-dimensional array by a two-dimension array reference only with standard library?

how to use a dynamically created one-dimensional array by a two-dimension array reference only with standard library? could it be done by construct overloading of operator [] ? example: i want to form a matrix by creating a one-dimension array and…
dayu321
  • 35
  • 5
2
votes
5 answers

How to make it so the user can't delete a dynamic array?

In writing a response, I wrote some code that challenged my assumptions on how const pointers work. I had assumed const pointers could not be deleted by the delete function, but as you'll see from the code below, that isn't the case: #include…
SE Does Not Like Dissent
  • 1,767
  • 3
  • 16
  • 36
2
votes
3 answers

Optimizing unnecessary string copying in vector

Presenting the minimal code to describe the problem: struct A { vector v; // ... other data and methods }; A obj; ifstream file("some_file.txt"); char buffer[BIG_SIZE]; while( ) { file.getline(buffer, BIG_SIZE-1); //…
iammilind
  • 68,093
  • 33
  • 169
  • 336
2
votes
2 answers

weird SIGABORT on malloc/calloc call

running my program with gdb I get this: fem.o: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long)…
the_candyman
  • 1,563
  • 4
  • 22
  • 36
2
votes
2 answers

Allocation of variables inside dynamically allocated structs

Suppose to have a struct that contains a pointer to an array and its size, like this one: typedef struct { int * array; int arr_size; }IntArray; and want to have this inside another struct, it can be done in two ways: typedef struct{ …
riciloma
  • 1,456
  • 2
  • 16
  • 31
2
votes
4 answers

How to properly clean up elements from vectors of object pointers

I have been studying dynamic allocation and came across this question on StackOverflow: Deallocating objects stored in a vector? One of the upvoted answers explain how to manually manage memory when using "Vectors of Pointers to Objects": iterating…
2
votes
1 answer

Write an image row by row with libpng using C

I'm trying to create a png image with the libpng library using C (gcc on linux). This should expose my problem: void createPng(char *filename, int width, int height) { FILE *fp = fopen(filename, "wb");; png_bytep row = NULL; png_structp…
Andrea Ciceri
  • 436
  • 6
  • 16
2
votes
4 answers

Remove element from dynamic array of structure

I'm working in C I have a struct called Entity and I create a dynamic array of that struct. Then I try to remove one element from the array but I don't get the behaviour I want. Here is the code I'm using: #include #include…
Drakalex
  • 1,488
  • 3
  • 19
  • 39
2
votes
1 answer

C: Multiplication of 2D arrays of sizes greater than 1000 results in a segmentation fault

I am multiplying 2-D arrays and am using dynamic allocation when initializing the arrays (but I don't know if the implementation is right). I am compiling my code with CodeBlock16.0. The program runs well for matrix sizes below 200, but I want this…
afni
  • 75
  • 2
  • 12
2
votes
1 answer

Efficiently generating byte buffer without breaking strict aliasing

This is such a simple pattern, there has to be a "nice" way of sorting it out. I have a function that needs to generate a dynamically sized byte array containing arithmetic data. // Given that I have a function that kinda looks like this: void…
user4442671
2
votes
3 answers

Dynamically Allocated input, and output 2-D Arrays in C++

My goal is dynamically allocate 2 dimensional array such that it prompts the user to input the size of the row and column of the matrix array they want to create. After dynamically allocating the size of the rows and columns, the user will input the…
2
votes
1 answer

Finding max element in an array

I have an array and I have to find the largest element in it, tell how many times it is in and in what position. I have to use pointers and dynamic memory allocation. It doesn't work when the array is filled randomly, but works fine when filled…
accr0
  • 23
  • 5
2
votes
2 answers

Valgrind shows more memory allocated than actually is

I was writting some simple code in C to test some memory allocation and pointers: #include #include int *randomAlloc(int n) { int *address = NULL, i = 0; address = malloc (n * sizeof(int)); for (i = 0; i < n ;…
gmelodie
  • 411
  • 4
  • 18