realloc

Correct way to call “realloc” in Swift with a Float array?

元气小坏坏 提交于 2019-12-05 02:23:25
问题 I'm trying figure out what size to send "realloc" when I call it through Swift. It seems that I have to add an extra byte , but I don't understand why. typealias Floats = UnsafeMutablePointer<Float> let FLOAT_SIZE = sizeof( Float ) func floats_realloc( floats:Floats, qty_of_floats:Int ) -> Floats { let KLUDGE = 1 // Why? let qty_of_bytes = ( qty_of_floats * FLOAT_SIZE ) + KLUDGE let realloced_floats = Floats( realloc( floats, UInt( qty_of_bytes ) ) ) return realloced_floats } If I set KLUDGE

Replacing realloc (C --> C++)

空扰寡人 提交于 2019-12-05 02:09:33
In an earlier question, I asked about typecasting pointers, but was directed to the better solution of using the C++ allocation system instead of mallocs. (I am converting some C code to C++) However, I still have an issue with a similar function: I changed: tmp = malloc(sizeof(char*) * mtmp); --> tmp = new char*[mtmp]; and free(tmp) --> delete [] tmp; However, what do I do with realloc in the following function: char* space_getRndPlanet (void) { int i,j; char **tmp; int ntmp; int mtmp; char *res; ntmp = 0; mtmp = CHUNK_SIZE; //tmp = malloc(sizeof(char*) * mtmp); <-- replaced with line below

git out of memory on checkout

会有一股神秘感。 提交于 2019-12-05 01:28:54
I have cloned a large repo and got an error (after several attempts) Clone succeeded, but checkout failed When trying to fix this with git checkout -f HEAD an error comes back Fatal: Out of memory, realloc failed2 I've already set some memory limits higher because the cloning also ran into problems by setting git config pack.WindowMemory 256m && git config pack.packSizelimit 256m Based on advice below from Punit Vara (below) I've also edited the .git/config to: [core] packedGitLimit = 128m packedGitWindowSize = 128m [pack] deltaCacheSize = 128m packSizeLimit = 128m windowMemory = 128m And I've

realloc() invalid old size

强颜欢笑 提交于 2019-12-04 23:46:51
问题 I am doing an exercise for fun from KandR C programming book. The program is for finding the longest line from a set of lines entered by the user and then prints it. Here is what I have written (partially, some part is taken from the book directly):- #include <stdio.h> #include <stdlib.h> int MAXLINE = 10; int INCREMENT = 10; void copy(char longest[], char line[]){ int i=0; while((longest[i] = line[i]) != '\0'){ ++i; } } int _getline(char s[]){ int i,c; for(i=0; ((c=getchar())!=EOF && c!='\n'

Realloc() does not correctly free memory in Windows

岁酱吖の 提交于 2019-12-04 06:25:23
I am attempting to use realloc() in a Windows application. I am allocating a large block of memory, then using realloc() to shrink it down later once I know the correct size. I am finding that although realloc() appears to work correctly (memory in Task Manager reflects what you would expect) the application eventually runs out of memory. From what I can tell, it's as though relloc() frees the memory but does not free the virtual address space associated with the memory. As a result, malloc() will eventually fail. Here's a small console app that demonstrates the problem: int _tmain(int argc,

Why isn't realloc shrinking the array?

﹥>﹥吖頭↗ 提交于 2019-12-04 06:18:22
问题 I have trouble reducing the size of dynamically created array. Here's what my main function looks like: int main(void) { // Intialize big array int * a = (int *)malloc(10*sizeof(int)); assert(a); // Fill it with squares for (int i = 0; i < 10; ++i) a[i] = i*i; // Expected to print 64 printf("%d\n", a[8]); // Shrink the big array int * b = (int *)realloc(a, 5*sizeof(int)); assert(b); // Expected to cause SEGFAULT printf("%d\n", b[8]); return 0; } Everything works fine except for printf("%d\n",

Malloc and Realloc relation, How does it handle when the required space is not available in memory [duplicate]

拥有回忆 提交于 2019-12-04 05:49:07
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: realloc and malloc functions #include<stdio.h> #include<stdlib.h> void main() { int *p; p = malloc(6); p = realloc(p, 10); if (p == NULL) { printf("error"); // when does p point to null consider i have enough space in prgrm //memory area but not in memory where realloc is trying to search //for the memory, I dont know how to explain that try to undrstnd exit(1); } } Take this example for the code, suppose the

realloc but only first few bytes is meaningful

爷,独闯天下 提交于 2019-12-04 03:12:43
Assume I have used ptr = malloc(old_size); to allocate a memory block with old_size bytes. Only the first header_size bytes is meaningful. I'm going to increase the size to new_size . new_size is greater than old_size and old_size is greater than header_size . before: /- - - - - - - old_size - - - - - - - \ +===============+---------------------+ \-header_size-/ after: /- - - - - - - - - - - - - - - new_size - - - - - - - - - - - - - - - - - - -\ +===============+------------------------------------------------------------+ \- header_size-/ I don't care what is stored after ptr + header_size

What are the chances that realloc should fail?

走远了吗. 提交于 2019-12-04 03:02:01
Does it fail when it runs out of free memory similar to malloc or could there be other reasons? Any of the allocation functions ( malloc , realloc , calloc , and on POSIX, posix_memalign ) could fail for any of the following reasons, and possibly others: You've used up your entire virtual address space, or at least the usable portion of it. On a 32-bit machine, there are only 4GB worth of addresses, and possibly 1GB or so is reserved for use by the OS kernel. Even if your machine has 16GB of physical memory, a single process cannot use more than it has addresses for. You haven't used up your

How `realloc` work actually in the background?

余生颓废 提交于 2019-12-03 22:32:12
How realloc work actually in the background? If there is not enough memory available at old place does this one allocating two/many memory blocks and one pointer pointing to that and other are internally linked with each other or the old region copied into new place where enough memory is available and pointer is updating to new address and deleting the old memory? And is that realloc is Compiler/OS dependent or independent ? realloc attempts to extend your available memory range if sufficient memory is available behind it on the heap. If not then it is equivalent to malloc a block of the new