free

Malloc, free and segmentation fault

落爺英雄遲暮 提交于 2019-11-28 12:22:24
I don't understand why, in this code, the call to "free" cause a segmentation fault: #include <stdio.h> #include <string.h> #include <stdlib.h> char *char_arr_allocator(int length); int main(int argc, char* argv[0]){ char* stringa = NULL; stringa = char_arr_allocator(100); printf("stringa address: %p\n", stringa); // same address as "arr" printf("stringa: %s\n",stringa); //free(stringa); return 0; } char *char_arr_allocator(int length) { char *arr; arr = malloc(length*sizeof(char)); arr = "xxxxxxx"; printf("arr address: %p\n", arr); // same address as "stringa" return arr; } Can someone

C++ pointer array is still accessible after delete[] is called [duplicate]

痴心易碎 提交于 2019-11-28 12:08:55
问题 This question already has answers here : c++ delete pointer issue, can still access data [closed] (6 answers) C++ delete - It deletes my objects but I can still access the data? (13 answers) Can a local variable's memory be accessed outside its scope? (20 answers) Closed 3 years ago . In the following code, delete[] is called once to free up the memory allocated by new . However, the array elements is still accessible after delete[] is called. I called delete[] twice to confirm that I am

Why doesn't free(p) set p to NULL?

廉价感情. 提交于 2019-11-28 10:50:55
Any reasons why this can not be standard behavior of free() ? multiple pointers pointing to the same object: #include <stdlib.h> #include <stdio.h> void safefree(void*& p) { free(p); p = NULL; } int main() { int *p = (int *)malloc(sizeof(int)); *p = 1234; int*& p2 = p; printf("p=%p p2=%p\n", p, p2); safefree((void*&)p2); printf("p=%p p2=%p\n", p, p2); safefree((void*&)p); // safe return 0; } assignment from malloc demands cast from void* vice versa: safefree() demands cast to void*& (reference) If it did, you would have to pass a pointer to a pointer to the function: int * p = malloc( sizeof(

Using realloc (X, 0) instead of free() and using malloc with length of a string +1

妖精的绣舞 提交于 2019-11-28 10:20:50
So I don't really know how to put the title this time. First of all I'd like to say that I've seen several comments on this page about warning if the question is related to "homework". Mine is, but it's also completed and I just want to further understand what is going on with the code. I have also read posts and books for some time, but I think I am still missing things. I have 2 lines of code I don't quite understand in the code I worked with. The work is about getting whatever file is used as argument (if it's 0 files, it read from stdin), and print it on the standard output backwards. All

C - Accessing data AFTER memory has been free()ed?

∥☆過路亽.° 提交于 2019-11-28 06:09:02
问题 I'm reading a lot about malloc() and free() in Standard C. As I understand it, you malloc() for some memory exactly once and then you free() that same memory exactly once. It may be bad practice, but I understand that after you malloc() memory, you can define multiple pointers to it. And once you free() any of those pointers, the allocated memory is de-allocated? Consider this toy example: #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(){ char* p = (char*)malloc(10 *

Overloading free() so my program use mine instead of the system's

半腔热情 提交于 2019-11-28 06:04:38
问题 I need to recode the free() func for educational purpose and it must be named free() also. When i rename my function myfree() it work flawlessly but when i name it free() the program don't know if he need to use mine or the system's so the program just Segmentation fault(core dumped) even if i don't call my free (just the declaration of another free() func seem to crash it) so how can i tell the compiler to use mine instead of the system's ? thanks you in advance. EDIT : Linux operating

What happens to the data in memory deallocated by free()?

别等时光非礼了梦想. 提交于 2019-11-28 06:01:51
问题 What happens to the data that is present in a memory location that has just been freed by a free() ? Is that data also deleted and the memory will now have a garbage value ? Or that data still persists there untill a new data is stored in that memory location (in future) ? I mean, for code below: int *ptr; ptr = malloc(sizeof(int)); *ptr = 1; // Suppose ptr = 2000 //Free now free(ptr); // My question is what is the value stored in memory address 2000 now ? // Is it still '1' or some garbage

Does the pointer passed to free() have to point to beginning of the memory block, or can it point to the interior?

假如想象 提交于 2019-11-28 04:00:15
问题 The question is in the title... I searched but couldn't find anything. Edit: I don't really see any need to explain this, but because people think that what I'm saying makes no sense (and that I'm asking the wrong questions), here's the problem: Since people seem to be very interested in the "root" cause of all the problem rather than the actual question asked (since that apparently helps things get solved better, let's see if it does), here's the problem: I'm trying to make a D runtime

Freeing in an atexit()

ぐ巨炮叔叔 提交于 2019-11-28 02:56:21
问题 Is there any point to freeing memory in an atexit() function? I have a global variable that gets malloc'ed after startup. I could write an atexit() function to free it, but isn't the system going to reclaim all that memory when the program exits anyway? Is there any benefit to being tidy and actively cleaning it up myself? 回答1: Not in C - it's like rearranging the deck chairs while the ship sinks around you. In C++ the answer is different, because objects can delete temporary files and so

Function free() in C isn't working for me

≯℡__Kan透↙ 提交于 2019-11-28 02:23:38
问题 I have been trying to free memory allocated via malloc() using free() . Some of the structs it does free but leaves some the way they were and they also remain linked to their children. It also never frees the root (gRootPtr) for a binary tree. I am using Xcode to find out if the memory used by the binary tree has been freed and also use the if statement. Code I am using to free the memory: void FreeMemory(InfoDefiner *InfoCarrier) { if ((*InfoCarrier) != NULL) { FreeMemory((&(*InfoCarrier)-