Questions tagged [memory-reallocation]

25 questions
6
votes
1 answer

Reallocate array with memcpy and memset

I've taken over some code, and came across a weird reallocation of an array. This is a function from within an Array class (used by the JsonValue) void reserve( uint32_t newCapacity ) { if ( newCapacity > length + additionalCapacity ) { …
ChrisMM
  • 8,448
  • 13
  • 29
  • 48
6
votes
1 answer

What's the reallocation equivalent of std::aligned_alloc()?

I've noticed std::aligned_alloc() coming into C++17, and I like it. But - what happens when I need to reallocate? I can do this manually (assuming the available space at the currently-allocated address is just the amount of space I asked for), but…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
2
votes
1 answer

Vulkan/VMA Change buffer size similar to `realloc`

Say I run into a situation where I would like to change a vulkan buffer's (VkBuffer) size. For example, if I wanted to add more vertices to an existing vertex buffer. How would I grow/shrink a VkBuffer? Would I be forced to just create a new buffer…
Rotartsi
  • 527
  • 5
  • 19
2
votes
2 answers

Designing a resize function for a queue

I'm currently trying to design a public API for a queue data structure and resize function to change its size. My first intention was to do it in the following way: typedef struct queue queue; /** * Resizes a given queue. * * Changes the size of…
St.Antario
  • 26,175
  • 41
  • 130
  • 318
2
votes
3 answers

Vulkan - 1 uniform buffer, N meshes - dynamic VkDeviceMemory

Suppose I'm rendering simple cubes at random positions. Having 3 of them as the starting number of cubes, the application acquires a VkBuffer handle and binds it to a VkDeviceMemory in order to store the model matrices of all cubes consecutively in…
Carlos Romero
  • 698
  • 8
  • 18
1
vote
2 answers

Explanation of "realloc doesn't require that ptr point to a memory but in practice does."

I am reading about the realloc function, i.e. void *realloc(void *ptr, size_t size); and the author of the textbook I'm reading (K.N. King "C Programming a Modern Approach", 2nd edition, p.421, last paragraph), at some point writes Although…
utobi
  • 279
  • 8
  • 16
1
vote
0 answers

Novice question about using realloc properly in practice

I'm trying to understand how to think about memory allocation from a practical perspective. I think I understand how to implement malloc and realloc in a basic sense, but I don't understand how to do it efficiently. The examples I have in my books…
Gary
  • 2,393
  • 12
  • 31
1
vote
1 answer

Segmentation Fault accessing array positions after realloc

Let's suppose I have this function: void arrayExtendDouble(int **ptArr, int *size) { *ptArr = realloc(*ptArr, (*size * 2) * sizeof(int)); for(int i = (*size * 2) - 1; i >= *size; i--) ptArr[i] = fib(i); //this will throw SEG…
1
vote
1 answer

How memory works if vectors inside class array are dynamically push_backed?

Assume that we have following class: class Test { public: Test() {} std::vector& getIntList() { return intList; } private: std::vector intList; }; Also, we have following codes inside main function to declare class…
sungjun cho
  • 809
  • 7
  • 18
1
vote
5 answers

Is there a way to change an array size using malloc and free functions only?

As homework, I have to write a function that changes an array's size using malloc and free functions only. I know how to do it with realloc, but I don't know how to do it with malloc. typedef struct{ int *tab; int size; }arr; and I need to…
John
  • 21
  • 4
1
vote
1 answer

Problems using realloc for decreasing

I am trying to delete with realloc but it isn't working the way I expected... void deallocates(int** v, int size, int original_size) { int i; *v = (int*)realloc(*v, sizeof(int) * size); printf("\nAFTER REALLOC FOR SIZE = %d\n",…
0
votes
0 answers

Issue with Queue Reallocation

#include #include int sizec,sizep; struct queue { int *item; int f,r,cnt; }; typedef struct queue QUE; void insertpriority(QUE *q) { if(q->cnt==sizep) { printf("Queue FULL\nReallocating\n"); q->item=((int…
0
votes
1 answer

Memory pool: force allocating resources (map / vector) to reallocate for continuity

I have a memory pool of fixed size. A custom allocator is used to put std::vector and std::unordered_map elements into that pool. Over time the memory pool gets fragmented and even though there might be enough memory available if you scrap all the…
glades
  • 3,778
  • 1
  • 12
  • 34
0
votes
1 answer

what's going wrong here with realloc()?

I am writing a code which uses realloc(). The following is a simplified version of the problem. Though the code looks obvious yet it doesn't seem to work. // Program for implementing variable length integer…
0
votes
2 answers

Why is realloc giving me inconsistent behaviour?

I am currently taking a procedural programming course at my school. We are using C with C99 standard. I discussed this with my instructor and I cannot understand why realloc() is working for his machine, but it is not working for mine. The goal of…
Ryan Leung
  • 11
  • 3
1
2