malloc

Why are the contents pointed to by a pointer not changed when memory is deallocated using free()?

佐手、 提交于 2020-01-10 05:15:20
问题 I am a newbie when it comes to dynamic memory allocation. When we free the memory using void free(void *ptr) the memory is deallocated but the contents of the pointer are not deleted. Why is that? Is there any difference in more recent C compilers? 回答1: Computers don't "delete" memory as such, they just stop using all references to that memory cell and forget that anything of value is stored there. For example: int* func (void) { int x = 5; return &x; } printf("%d", *func()); // undefined

malloc behaviour on an embedded system

半腔热情 提交于 2020-01-09 19:39:43
问题 I'm currently working on an embedded project (STM32F103RB, CooCox CoIDE v.1.7.6 with arm-none-eabi-gcc 4.8 2013q4) and I'm trying to understand how malloc() behaves on plain C when the RAM is full. My STM32 has 20kB = 0x5000Bytes of RAM, 0x200 are used for the stack. #include <stdlib.h> #include "stm32f10x.h" struct list_el { char weight[1024]; }; typedef struct list_el item; int main(void) { item * curr; // allocate until RAM is full do { curr = (item *)malloc(sizeof(item)); } while (curr !=

Dynamic array in C — Is my understanding of malloc and realloc correct?

僤鯓⒐⒋嵵緔 提交于 2020-01-09 12:19:10
问题 I am learning how to create dynamic 1D arrays in C. The code below tries to do the following: Using malloc , create a dynamic array of length 10 , that holds values of type double . Set each entry of the array to j/100 for j = 0, 1,..., 9 . Then print it out. Add an additional empty entry to the end of the array using realloc . Set the new entry to j/100 and print out each entry again. Testing: double* data = (double*)malloc(10*sizeof(double)); for (j=0;j<10;j++) { data[j]= ((double)j)/100;

Malloc Memory Questions

霸气de小男生 提交于 2020-01-09 10:33:29
问题 First of all I noticed when I malloc memory vs. calloc the memory footprint is different. I am working with datasets of several GB. It is ok for this data to be random. I expected that I could just malloc a large amount of memory and read whatever random data was in it cast to a float. However, looking at the memory footprint in the process viewer the memory is obviously not being claimed (vs. calloc where I see a large foot print). I ran a loop to write data into the memory and then I saw

Malloc Memory Questions

和自甴很熟 提交于 2020-01-09 10:32:07
问题 First of all I noticed when I malloc memory vs. calloc the memory footprint is different. I am working with datasets of several GB. It is ok for this data to be random. I expected that I could just malloc a large amount of memory and read whatever random data was in it cast to a float. However, looking at the memory footprint in the process viewer the memory is obviously not being claimed (vs. calloc where I see a large foot print). I ran a loop to write data into the memory and then I saw

Why do I get different results when I dereference a pointer after freeing it?

杀马特。学长 韩版系。学妹 提交于 2020-01-09 03:57:12
问题 I've a question about the memory management in C (and GCC 4.3.3 under Debian GNU/Linux). According to the C Programming Language Book by K&R, (chap. 7.8.5), when I free a pointer and then dereference it, is an error. But I've some doubts since I've noted that sometimes, as in the source I've pasted below, the compiler (?) seems to work according a well-defined principle. I've a trivial program like this, that shows how to return an array dynamically allocated: #include <stdio.h> #include

GNU buffer overflow using malloc

这一生的挚爱 提交于 2020-01-07 09:54:17
问题 I am running in a loop the following function: int* rpermute(int n) { int* a = malloc(n * sizeof(int)); int k; for (k = 0; k < n; k++) { a[k] = k; } for (k = n - 1; k > 0; k--) { int j = rand() % (k + 1); int temp = a[j]; a[j] = a[k]; a[k] = temp; } return a; } If I set a new int variable in my code every variable is changing, I assume it is a buffer overflow problem. Running the valgrind i get the following: ==4459== 73,036 bytes in 19 blocks are definitely lost in loss record 1 of 1 ==4459=

Following pointer returned by malloc(0)

二次信任 提交于 2020-01-07 02:35:08
问题 I am trying to understand a portion of code. I am leaving out a lot of the code in order to make it simpler to explain, and to avoid unnecessary confusion. typedef void *UP_T; void FunctionC(void *pvD, int Offset) { unsigned long long int temp; void *pvFD = NULL; pvFD = pvD + Offset; temp = (unsigned long long int)*(int *)pvFD; } void FunctionB(UP_T s) { FunctionC(s, 8); } void FunctionA() { char *tempstorage=(char *)malloc(0); FunctionB(tempstorage); } int main () { FunctionA(); return 0; }

malloc and free with a dynamically changing structure

a 夏天 提交于 2020-01-07 02:25:28
问题 I am having trouble with moving my pointer in a dynamically changing structure. I have created my code where you can malloc more memory and this seems to be working. The problems that I am running into is how to add to the structure, how to free memory and how to move from structure to structure and print all items. I am trying to test add and print (the delete function that is there does not seem to work, segfaults) When I add to the struct and then print the struct I get a segfault from the

I've got a segmentation fault but I dont find oO?

妖精的绣舞 提交于 2020-01-06 18:08:24
问题 It should sort with merge. There are two functions the merge and the sort merge. Some not known functions (read array from file and print array) are totally functional in an input file. Valgrind show me that the failure is at the allocation from array2 and when it read and write at the 3rd while-loop in void merge. void merge(int* array, int start, int middle, int end) { int size = end - start + 1; int *array2 = malloc(size*sizeof(array2)); int k = start; int m = middle + 1; int i = 0; int j