free

How to free an array of char-pointer?

谁说胖子不能爱 提交于 2019-12-23 19:19:47
问题 I use this Method to convert values from a list into an array for use in an execvp()-Systemcall: char **list2argarray(struct shellvalue *values, int count) { char **array = (char **)malloc((count + 1) * sizeof(char *)); int i = 0; while (values) { char *word = values->word; array[i] = (char *)malloc(sizeof(word) + 1); strcpy(array[i], word); values = values->next; i++; } array[i] = NULL; return array; } What is a proper way to free such Arrays? I tried it with things like void freeargpointer

Error while freeing memory in C

喜夏-厌秋 提交于 2019-12-23 04:43:21
问题 I have written a problem to practice pointers and allocating memory. However, I am getting a stack dump when I free the memory. Am I freeing in the correct place? Is there anything else wrong with my program that could make it unsafe? void display_names(char **names_to_display, char **output); int main(void) { char *names[] = {"Luke", "John", "Peter", 0}; char **my_names = names; char *new_output[1024] = {0}; size_t i = 0; // Print the ordinal names while(*my_names) { printf("Name: %s\n", *my

C Programming- Malloc/Free

白昼怎懂夜的黑 提交于 2019-12-22 18:04:24
问题 So I have a code and when I run it, it hangs when I enter a size greater than 3. When it's exactly 3 it runs smoothly. I narrowed down the problem to malloc and free and I don't know what the problem is. I'm new at this so any help is appreciated. do //repeatedly ask the user to put a number between 3-9 { printf("Enter the size of the game board between 3-9: "); scanf("%d", &size); }while(size<3 || size>9); if((board = (char***)malloc(sizeof(char**)*size))==NULL) printf("Memory Allocation

unexpected output of size allocated by malloc in C [closed]

断了今生、忘了曾经 提交于 2019-12-22 12:53:34
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 7 years ago . I have read that malloc actually allocates (required_size + 1) blocks of memory and it stores the size in the first block and the pointer to the second

freeing memory of a binary tree C

£可爱£侵袭症+ 提交于 2019-12-22 08:59:34
问题 I would like to free memory from my allocated binary tree what traversal is the best for doing so? typedef struct Node{ struct Node * right; struct Node * left; void * data; }Node; typedef int (*cmp) (void*,void *); Node* init(void * element){ Node * newNode=(Node*)malloc(sizeof(Node)); newNode->data=element; newNode->left=NULL; newNode->right=NULL; return newNode; } void insert(void * element, Node** root,cmp compareTo){ if(*root==NULL){ *root=init(element); return; } if(compareTo(element,(

Understanding glibc malloc trimming

[亡魂溺海] 提交于 2019-12-22 06:24:36
问题 Some program that I am currently working on consumes much more memory than I think it should. So I am trying to understand how glibc malloc trimming works. I wrote the following test: #include <malloc.h> #include <unistd.h> #define NUM_CHUNKS 1000000 #define CHUNCK_SIZE 100 int main() { // disable fast bins mallopt(M_MXFAST, 0); void** array = (void**)malloc(sizeof(void*) * NUM_CHUNKS); // allocating memory for(unsigned int i = 0; i < NUM_CHUNKS; i++) { array[i] = malloc(CHUNCK_SIZE); } //

Understanding glibc malloc trimming

不想你离开。 提交于 2019-12-22 06:24:02
问题 Some program that I am currently working on consumes much more memory than I think it should. So I am trying to understand how glibc malloc trimming works. I wrote the following test: #include <malloc.h> #include <unistd.h> #define NUM_CHUNKS 1000000 #define CHUNCK_SIZE 100 int main() { // disable fast bins mallopt(M_MXFAST, 0); void** array = (void**)malloc(sizeof(void*) * NUM_CHUNKS); // allocating memory for(unsigned int i = 0; i < NUM_CHUNKS; i++) { array[i] = malloc(CHUNCK_SIZE); } //

C++ allocator<X>::deallocate(NULL,1) allowed?

孤街醉人 提交于 2019-12-22 04:45:12
问题 Both free(NULL) and ::operator delete(NULL) are allowed. Does the allocator concept (e.g. std::allocator also allow deallocate(NULL,1) , or is it required to put your own guard around it? 回答1: You'll need to add your own check. According to §20.4.​1.1/8, deallocate requires: p shall be a pointer value obtained from allocate(). n shall equal the value passed as the first argument to the invocation of allocate which returned p. allocate throws an exception when storage can't be given (§20.4.​1

Howto check if a char* points to a string literal in C

假装没事ソ 提交于 2019-12-22 01:27:00
问题 I have a struct struct request { int code; char *message; }; that I'd like to free properly. I have the following function to do that: void free_request(struct request *req) { if (req->message != NULL) { free(req->message); } free(req); req = NULL; } The problem is that I get an "free(): invalid pointer"/segfault error from the compiler when I try to free a request that has been created using a string literal: struct request *req; req = malloc(sizeof(struct request)); req->message = "TEST";

strtok and memory leaks

我怕爱的太早我们不能终老 提交于 2019-12-21 05:11:07
问题 I wrote a simple url parser using strtok(). here's the code #include <stdio.h> #include <stdlib.h> typedef struct { char *protocol; char *host; int port; char *path; } aUrl; void parse_url(char *url, aUrl *ret) { printf("Parsing %s\n", url); char *tmp = (char *)_strdup(url); //char *protocol, *host, *port, *path; int len = 0; // protocol agora eh por exemplo http: ou https: ret->protocol = (char *) strtok(tmp, "/"); len = strlen(ret->protocol) + 2; ret->host = (char *) strtok(NULL, "/"); len