Tags down


what will happen if we dont use free() for allocated memory

By : Vanathi V
Date : October 18 2020, 06:10 AM
hop of those help? You are using a very strange macro, the allocations in standard C would read:
code :
int * const a = malloc(sizeof *a);
if (a != NULL)
  *a = 100;

Share : facebook icon twitter icon

Is there an alternative way to free dynamically allocated memory in C - not using the free() function?

By : Joseph Hughes
Date : March 29 2020, 07:55 AM
seems to work fine Actually, the last of those is equivalent to a call to free(). Read the specification of realloc() very carefully, and you will find it can allocate data anew, or change the size of an allocation (which, especially if the new size is larger than the old, might move the data around), and it can release memory too. In fact, you don't need the other functions; they can all be written in terms of realloc(). Not that anyone in their right mind would do so...but it could be done.
See Steve Maguire's "Writing Solid Code" for a complete dissection of the perils of the malloc() family of functions. See the ACCU web site for a complete dissection of the perils of reading "Writing Solid Code". I'm not convinced it is as bad as the reviews make it out to be - though its complete lack of a treatment of const does date it (back to the early 90s, when C89 was still new and not widely implemented in full).
code :
#include <stdlib.h>
void *malloc(size_t size);
#include <stdlib.h>
void *realloc(void *ptr, size_t size);
OBJ * p; // pointer to a variable list of OBJs
    /* initial allocation */
p = (OBJ *) calloc(0, sizeof(OBJ));
     /* ... */
     /* reallocations until size settles */
 while(1) {
    p = (OBJ *) realloc((void *)p, c * sizeof(OBJ));
         /* change value of c or break out of loop */

Do I need to free allocated memory of sub-members or is it enough to free the parent?

By : Olga Velmakina
Date : March 29 2020, 07:55 AM
I hope this helps you . If you only frees memory of list element (when you have nodes), you cause a memory leak. In memory, the list looks like this:
code :
 [list caption]...other data...[node]...other data...[node]...[last node]
  ^(It is not always first!)

how to free allocated memory inside a function by passing correct pointer to free()

By : William
Date : March 29 2020, 07:55 AM
will be helpful for those in need In function "create_memory_pool" I can allocate pools of memory but I am not sure how can I free it from main program. , You claim you must adhere to the following prototype
code :
boolean create_memory_pool(char *name, int size);
 * Initialize a memory pool in {@code name}, which has size {@code size}.
boolean create_memory_pool(char *name, int size)
    // the initial bytes of {@code memory_pool} are the header
    struct memory_pool *pool = (struct memory_pool*) name;
    initialize stack pool->stack;
    divide the remainder of the memory block after the header into blocks of size BLOCK_SIZE and add to the stack pool->stack;

 * Allocate a block of size {@code BLOCK_SIZE} in the memory pool {@code memory_pool} or return null on failure.
void *alloc_block(char *poolopaque)
    struct memory_pool *pool = (struct memory_pool*) poolopaque;
    pop and return a block from pool->stack or return null;

 * Return a block of memory into the memory pool.
void free_block(char *poolopaque, void *block)
    struct memory_pool *pool = (struct memory_pool*) poolopaque;
    add block to pool->stack

free won't delete memory allocated to the pointer (int array), using free twice works, why?

By : Karol Nowak
Date : March 29 2020, 07:55 AM
will be helpful for those in need This is not odd, or wrong. When calling free on a pointer, you only tell the memory manager that it can re-use that portion of memory again for another malloc call. It won't erase the contents (this would take extra time with no function whatsoever). But, you can access it, because the pointer still points to a valid address. It leads to undefined behaviour though, because the memory manager could have given that memory to some other piece of code.
Calling free a second time can generate an error, because the memory manager doesn't know that pointer anymore as being valid (you just freed it!). In fact, this is undefined behaviour.

Do I need to free wchar memory allocated when using 'new' or does delete[] also free it?

By : LeonL
Date : October 09 2020, 12:00 PM
wish helps you Don't use new at all if you can help it. In modern C++ in most cases new is not needed. You can use std::vector, std::array, std::string (std::basic_string maybe), std::unique_ptr or std::shared_ptr.
If you feel like you really need to use new, make special class which will handle this. I.e. for example make new in constructor and delete in destructor.
code :
struct sStructUsers 
    const int MAX_NAME_SIZE = 128;
        sName = new TCHAR[MAX_NAME_SIZE];
        delete sName;
    TCHAR *sName;
struct sStructUsers 
    std::basic_string<TCHAR> sName;
struct sStructUsers 
    const int MAX_NAME_SIZE = 128;
    sStructUsers() : sName(new TCHAR[MAX_NAME_SIZE]) {}
    std::unique_ptr<TCHAR[]> sName;
Related Posts Related Posts :
  • Avoid Overflow when Calculating π by Evaluating a Series Using 16-bit Arithmetic?
  • How to handle data or char -1 when reading from file, since EOF is also -1
  • Compare every 1kb of contents of two files instead of character by character
  • Understanding functions in stm8s disassembly?
  • Recursive and no-recursive get different answer
  • Can anyone explain the following output of the code?
  • How to resync time from NTP server in esp-idf?
  • Output for the following C code is confusing me
  • Determine if window is visible with High CPU
  • Writing a file by taking input from user only saves value for "x" only
  • How should I fill an array of ints from a file using command line args? The size of the file and the no. of elements may
  • Segmentation Fault While Reading File in C
  • Fill an array at index n with m times data without bit-fields
  • Why don't the values I type in the terminal match the values I've already stored in the array?
  • MPLABX XC16 unable to resolve built-in identifier __builtin_dmaoffset
  • How call and compile function from elf to my binary?
  • Modifying non-const char array that is referred to by const char array
  • Does anyone see any mistakes here? I am trying to get a message via a pipe, from my parent to child
  • Memory Leak - Singly linked list in C
  • Trouble with translation of typedef between C to D
  • Why does SIGINT stop sleep more than one time?
  • How to pause a timer created with CreateTimerQueueTimer
  • How can I read data from file ? I can't fix it
  • Scanf in visual studio not accepting multiple cases of characters
  • How can I determine if a char* is a windows line ending?
  • I am trying to store value in long long int type but giving wrong return
  • How to manage different kinds of data in a linked list?
  • uint8_t not rollover to 0 after reaching 255 not working properly
  • Why do these two execvp produce different results?
  • Pass uintmax_t or size_t to custom printf conversion specifier
  • Why does free() leaves stuff in memory?
  • Why pointers can't be used to index arrays?
  • memory allocation eror in C
  • C custom datatypes mapped to C datatypes grouped under a single struct
  • pipe() data is not transferred to child process
  • Getting a core dump from a simple C program
  • Fatal error on makefile, need to understand the problem
  • How can I add a delay of 90 minutes when a port has gone from 0 to 1?
  • To use strcpy or not
  • the usage of strtok() in c shows warnings and returns segmentation fault(core dumped)
  • Trouble allocating array of structs
  • Only first char of user input used in array
  • Why does "int x = 5; printf("%d %d %d", x==5, x=10, x==5);" in C print "0 10 0"?
  • How to scan specific string format in C?
  • sscanf skipping the final value when reading multiple values from a line
  • How can I access full memory space in FreeDOS with C application
  • Semantics of sem_getvalue() in POSIX
  • What does the [x,y] symbol mean in a multidimensional array access?
  • compilation error: cast from pointer to integer of different size
  • why am i getting compilation error "error: conflicting types for ‘ptr’ " for the following code? static int va
  • Why does getw return -1 when trying to read a character?
  • Why C program in whch two functions call each other recursively gives segmentation fault on linux?
  • ssize_t is undefined
  • Passing a generic argument in a C function
  • Restoring stdout after redirecting it via freopen()
  • Tuples "cannot be marshalled in a foreign call" when trying to export a Haskell function to C
  • Dereferencing double pointer to pass to recursive function
  • Why is the output different in these two scenarios
  • counting words from user input
  • Why am I not getting the concatenated string?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk