free

Understanding of pointers with malloc and free

别说谁变了你拦得住时间么 提交于 2019-11-30 15:23:18
Pointers are a really tricky thing in C. For a lot of people is hard to understand it, so for a good understanding I wrote following code: #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]) { int *p; // pointer -> will be dynamic allocated int *a; // array -> will be dynamic allocated // print before allocate memory (1) printf("&p: %p\tp: %p\t*p: %d\n", &p, p, *p); printf("&a: %p\ta: %p\t*a: %d\n", &a, a, *a); printf("\n"); // allocate memory (2) p = (int *)malloc(sizeof(int)); a = (int *)malloc(sizeof(int) * 10); // print after allocate, but before give a value to

Free allocated memory before return a function

回眸只為那壹抹淺笑 提交于 2019-11-30 12:43:02
I am trying to return an array using malloc in a function: char* queueBulkDequeue(queueADT queue, unsigned int size) { unsigned int i; char* pElements=(char*)malloc(size * sizeof(char)); for (i=0; i<size; i++) { *(pElements+i) = queueDequeue(queue); } return pElements; } The problem is that I need to free it because my MCU's heap size is limited. But I want to return it so I cannot free it in the function, right?. Can I free the allocated memory outside the function (where I call the function). Is there any best practices for this? Thank you in advance! As the memory allocated by malloc() is

Reusing freed pointers in C

邮差的信 提交于 2019-11-30 08:11:08
问题 There are many questions on this website regarding freeing pointers after use and, further, setting them to NULL. Arguments are fierce and the topic is seemingly divided equally. For example: This question. I am confused about freeing pointers in general. Imagine you have a pointer to some memory space. After using the space, you free the pointer but do not set it to NULL. Later, you have another pointer that calls malloc() , or some analog, and it is allocated memory including the memory

When a program terminates what happens to the memory allocated using malloc that is not free'ed?

╄→гoц情女王★ 提交于 2019-11-30 07:56:41
问题 Say I have the following program #include <stdio.h> #include <stdlib.h> int main(void) { int * i; if ((i = malloc(sizeof(int) * 100)) == NULL) { printf("EROOR: unable to allocate memory \n"); return -1; } /* memory is allocated successfully */ /* memory is not free'ed but program terminates */ // free(i); return 0; } The above program calls malloc to allocate some memory and does not call free to de-allocate it. And the program terminates without de-allocating the memory. Valgrind clearly

Can I force a numpy ndarray to take ownership of its memory?

纵然是瞬间 提交于 2019-11-30 06:57:09
I have a C function that mallocs() and populates a 2D array of floats. It "returns" that address and the size of the array. The signature is int get_array_c(float** addr, int* nrows, int* ncols); I want to call it from Python, so I use ctypes. import ctypes mylib = ctypes.cdll.LoadLibrary('mylib.so') get_array_c = mylib.get_array_c I never figured out how to specify argument types with ctypes. I tend to just write a python wrapper for each C function I'm using, and make sure I get the types right in the wrapper. The array of floats is a matrix in column-major order, and I'd like to get it as a

How to check if a pointer is freed already in C?

Deadly 提交于 2019-11-30 06:29:37
问题 I would like to check if a pointer is freed already or not. How do I do this using gnu compiler set? 回答1: You can't. The way to track this would be to assign the pointer to 0 or NULL after freeing it. However as Fred Larson mentioned, this does nothing to other pointers pointing to the same location. int* ptr = (int*)malloc(sizeof(int)); free(ptr); ptr = NULL; 回答2: You can't. Just assign NULL to it after you free it to make sure you don't free it twice (it's ok to free(NULL) ). Better yet, if

What is the best way to free memory after returning from an error?

倾然丶 夕夏残阳落幕 提交于 2019-11-30 06:29:31
问题 Suppose I have a function that allocates memory for the caller: int func(void **mem1, void **mem2) { *mem1 = malloc(SIZE); if (!*mem1) return 1; *mem2 = malloc(SIZE); if (!*mem2) { /* ... */ return 1; } return 0; } I'd like to hear your feedback on the best way to free() the allocated memory in case the second malloc() fails. You can imagine a more elaborate situation with more error exit points and more allocated memory. 回答1: I know people are loathe to use them, but this is the perfect

free() not deallocating memory?

夙愿已清 提交于 2019-11-30 06:05:05
问题 free(str); printf("%d\n", str->listeners); The call to printf succeeds (as do any other calls to str's members). How is this possible? 回答1: You're just (un)lucky. That code exhibits undefined behavior - anything can happen, including looking like the memory wasn't freed. The memory is freed, but there is no point in actively clearing it, so its original content is likely to still be there. But you can't rely on that. 回答2: Here's an analogy for you: imagine you're renting an apartment (that's

C - Design your own free( ) function

自作多情 提交于 2019-11-30 04:56:14
Today, I appeared for an interview and the interviewer asked me this, Tell me the steps how will you design your own free( ) function for deallocate the allocated memory. How can it be more efficient than C's default free() function ? What can you conclude ? I was confused, couldn't think of the way to design. What do you think guys ? EDIT : Since we need to know about how malloc() works, can you tell me the steps to write our own malloc() function That's actually a pretty vague question, and that's probably why you got confused. Does he mean, given an existing malloc implementation, how would

What is the difference between freeing the pointer and assigning it to NULL?

筅森魡賤 提交于 2019-11-30 04:47:26
Could somebody tell me the difference between: int *p; p=(int*)malloc(10*sizeof(int)); free(p); or int *p; p=(int*)malloc(10*sizeof(int)); p=NULL; free will deallocate the memory that p points to - simply assigning it to NULL will not (and thus you will have a memory leak). It is worth mentioning that it is good practice to assign your pointer to NULL AFTER the call to free , as this will prevent you from accidentally trying to access the freed memory (which is still possible, but absolutely should not be done). There is no garbage collection in C, so if you don't explicitly free a chunk of