malloc

mmap substitute for malloc

浪子不回头ぞ 提交于 2021-02-20 05:00:07
问题 I need to find a way to use mmap instead of malloc. How is this possible? (I am not using libc only syscalls) And yes brk() is possible. I used sbrk() but realized its not sys-call... (x86 inline assembly) I've been looking around and saw this: How to use mmap to allocate a memory in heap? But it didn't help for me, because I had a segfault. Basically, all I want to do a create 3 slabs of memory for storing characters. Say, char * x = malloc(1000); char * y = malloc(2000); char * z = malloc

malloc does not guarantee returning physically contiguous memory

﹥>﹥吖頭↗ 提交于 2021-02-19 04:25:07
问题 I'm reading about virtual memory and my conclusions are the following: malloc(size); malloc does not guarantee to return physically contiguous memory. It guarantees to return virtually contiguous memory. Especially it is true when size > 4KB because 4KB is the size of page. (On Linux systems). Am I right or am I wrong? Please explain. 回答1: malloc does not guarantee returning physically contiguous memory yes It guarantees returning virtually contiguous memory yes Especially it is true when

error when copying dynamically allocated data in device to host?

点点圈 提交于 2021-02-16 15:09:49
问题 I recently meet a problem when copying dynamically allocated data in device to host memory. The data is allocated with malloc, and I copy those data from device to host in host function. Here is the code: #include <cuda.h> #include <stdio.h> #define N 100 __device__ int* d_array; __global__ void allocDeviceMemory() { d_array = new int[N]; for(int i=0; i < N; i++) d_array[i] = 123; } int main() { allocDeviceMemory<<<1, 1>>>(); cudaDeviceSynchronize(); int* d_a = NULL; cudaMemcpyFromSymbol(

sbrk(0) value getting increased after calling printf

六月ゝ 毕业季﹏ 提交于 2021-02-11 07:43:47
问题 I am trying to write my own malloc implementation, following an online course. Issue I am facing is that i always get segmentation fault, and when i tried to debug it always point to printf statement, which is just a string. After further investigation i found the value for sbrk(0) always increase after printf gets called. I am not sure but i think my malloc implementation overwrites some memory space that printf might be using. But is it possible for printf to be there. The program works

sbrk(0) value getting increased after calling printf

青春壹個敷衍的年華 提交于 2021-02-11 07:43:36
问题 I am trying to write my own malloc implementation, following an online course. Issue I am facing is that i always get segmentation fault, and when i tried to debug it always point to printf statement, which is just a string. After further investigation i found the value for sbrk(0) always increase after printf gets called. I am not sure but i think my malloc implementation overwrites some memory space that printf might be using. But is it possible for printf to be there. The program works

Make calloc opportunistic

微笑、不失礼 提交于 2021-02-10 14:26:28
问题 On linux malloc behaves opportunistically, only backing virtual memory by real memory when it is first accessed. Would it be possible to modify calloc so that it also behaves this way (allocating and zeroing pages when they are first accessed)? 回答1: It is not a feature of malloc() that makes it "opportunistic". It's a feature of the kernel with which malloc() has nothing to do whatsoever. malloc() asks the kernel for a slap of memory everytime it needs more memory to fulfill a request, and it

Make calloc opportunistic

我只是一个虾纸丫 提交于 2021-02-10 14:25:42
问题 On linux malloc behaves opportunistically, only backing virtual memory by real memory when it is first accessed. Would it be possible to modify calloc so that it also behaves this way (allocating and zeroing pages when they are first accessed)? 回答1: It is not a feature of malloc() that makes it "opportunistic". It's a feature of the kernel with which malloc() has nothing to do whatsoever. malloc() asks the kernel for a slap of memory everytime it needs more memory to fulfill a request, and it

Unlogical C6001 warning: Using uninitialized memory warning in C with Visual Studio

白昼怎懂夜的黑 提交于 2021-02-09 06:56:47
问题 Given this code: #include <stdlib.h> typedef struct { int *p; } MyStruct; MyStruct Test() { MyStruct ms; ms.p = malloc(sizeof(int) * 5); if (!ms.p) exit(-1); return ms; } int main(void) { while (1) { MyStruct t = Test(); free(t.p); // C6001: Using uninitialized memory 't.p'. } } Visual Studio shows C6001 warning on the free call line. However, I see there is no way to achieve the free line with the memory t.p uninitialized. What am I missing ? 回答1: Some points: sometimes SAL warnings can be

Invalid write of size 8, C Valgrind, string arrays

房东的猫 提交于 2021-02-08 15:52:41
问题 I have been using both valgrind and gdb and I can not quite figure out what the problem is. It hops around too much for me to really trace it down in gdb, and in valgrind I don't have enough information. Here is my makeargv function, which is putting strings outputted from strtok() into arrays. makeargv is called from the below parse function. I'm not sure where I'm going wrong. I would really appreciate the help :D. Just an FYI I'm really new to all this malloc'ing and don't really

dynamic allocation of array of pointers

无人久伴 提交于 2021-02-08 15:44:08
问题 The following code gives a segmentation fault. I am not able to figure out as to why. Please see.. #include <stdio.h> #include <stdlib.h> int main() { int **ptr; int *val; int x = 7; val = &x; *ptr = (int *)malloc(10 * sizeof (*val)); *ptr[0] = *val; printf("%d\n", *ptr[0] ); return 0; } on debugging with gdb, it says: Program received signal SIGSEGV, Segmentation fault. 0x0804843f in main () at temp.c:10 *ptr = (int *)malloc(10 * sizeof (*val)); Any help regarding the matter is appreciated.