Tags down


When is memory allocated and de-allocated static and dynamic memory in C?

By : Test Seventyseven
Date : September 14 2020, 09:00 AM
hop of those help? There are 3 different kinds of storage duration in C language:
static: the lifetime of the variable is the lifetime of the program. It is allocated at load time (only defined at compile time) and only freed when the operation system unloads the program. Static variables are variables declared outside any functions, and local variables (declared in a function or block) having the static modifier automatic: automatic variables are declared inside a block (or function), with no storage modifier. Their lifetime starts at the beginning of the bloc and ends at the end of the bloc. They are generally allocated at the beginning of the bloc and deallocated at its end, but because of the as if rule, optimizing compilers could allocate them sooner and free them later, for example if the bloc in located inside a loop. dynamic: they are allocated manually through malloc, and will only be deallocated by free
code :

Share : facebook icon twitter icon

stack allocated memory versus dynamically allocated memory

By : Kelson
Date : March 29 2020, 07:55 AM
seems to work fine I wouldn't say that any one way of allocating a structure has "more chance of corruption". Whatever is causing the corruption could happen just as easily however it's allocated.
I'd say you'd be better off fixing the source of the corruption: You can use gdb to put a breakpoint on write to the corrupted variable with watch . Alternatively, you can also put a breakpoint when you detect corruption, and then use reverse debugging to find where the corruption occurred.
code :
void foo() {
    int a[10]; // stack
    static int b[10]; // static

    int *c = malloc(sizeof(int) * 10); // dynamic on the heap
void foo() {
    static int a = 0;
static void foo() { ... }
static int x;

free allocated memory in 2D dynamic memory allocation array in C++

By : mbox
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further You have to go through your matrix and delete each array. At the end of doing that you can delete the matrix itself
code :
// free dynamically allocated memory
for( int i = 0 ; i < *row ; i++ )
    delete[] matrix[i]; // delete array within matrix
// delete actual matrix
delete[] matrix;

dynamic memory allocation in c , free some part of memory that is allocated before using malloc()

By : ycam
Date : March 29 2020, 07:55 AM
will be helpful for those in need You should use the standard library function realloc. As the name suggests, it reallocates a block of memory. Its prototype is (contained in the header stdlib.h)
code :
 void *realloc(void *ptr, size_t size);
// allocate memory for 10 integers
int *arr = malloc(10 * sizeof *arr);
// check arr for NULL in case malloc fails

// save the value of arr in temp in case
// realloc fails
int *temp = arr;  

// realloc may keep the same block of memory
// and free the memory for the extra 5 elements
// or may allocate a new block for 5 elements,
// copy the first five elements from the older block to the
// newer block and then free the older block
arr = realloc(arr, 5 * sizeof *arr);
if(arr == NULL) {
    // realloc failed
    arr = temp;

How to add contiguous blocks of memory (dynamically) to a previosly allocated dynamic memory

By : Qzak
Date : March 29 2020, 07:55 AM
I wish this helpful for you Now that i am aware of std::vector i think i can use it for this problem. I haven't posted the entire code because I think it will add to too much of details that are not relevant.
code :
vector<vector<int> > grid;
dist_y = get_y();        //returns the number of blocks in Y direction
if(dist_y > threshold){
    forward();          //moves a step forward.
    dist_x = get_x();   //returns blocks available in X direction.
    vector<int> col;
    for(int i = 0; i < dist_x; i++) {
        col.push_back(1);   //1 indicates there is free space there.

C free() function called on static memory (on not dynamically allocated memory)

By : Noviana Arifin
Date : March 29 2020, 07:55 AM
will help you It's against the rules, so just about anything might happen. free() might print an error message. The program might crash with a segmentation violation or other memory access error. The error might be silently ignored. Or the error might lead to corruption of the malloc/free heap such that an error or crash might happen during some later call to malloc or free.
Related Posts Related Posts :
  • Efficient Assembly multiplication
  • while (getchar != '\n'); alternative to clear the buffer
  • How to fill a number with a variable amount of 0s?
  • Why C programming gives different output?
  • How to get float value from uint32_t?
  • What is the output of expression c=a+++b? If a=2,b=5 in c language...?
  • Why does it print 1 at the end?
  • Caesar cipher for numbers in C
  • Could someone explain the method of making rules for connect 4 game in C?
  • Is it a memory leak in C when code fails to free memory, but the OS will anyway?
  • Why can't I initialize a loop with i = -1?
  • How does the fork() function behave in if statements?
  • Why strcpy_s is safer than strcpy?
  • gcc flags to disable arithmetic optimisations
  • Recursion in c to print characters
  • Character array initialization using for loop
  • C Programming - Cannot Call Function More Than Once
  • Structures and pointer to pointer element
  • Error when initializing struct members in struct definition
  • Is that fixed-size array? a[]
  • It is not outputting what was expected and am unsure as to the issue
  • i want use C shell code to make buffer overflow and execute bin/sh
  • what the main function in this program returns to os?
  • Suffixes for integer types
  • I don't understand why a equals 1 and b equals 0 in this program
  • C prints other integer number than what it shows on Watches. Why is that?
  • Output of the for loop?
  • How to find 64-bit divisions in existing C code?
  • Finding a numbers with the largest/smallest number of bits set
  • how does " for (int j = 0; j < n || !putchar('\n'); j++) " work
  • handle ctrl+c in client-server socket in c programing
  • Restrict a process to read "/etc/resolv.conf" file in linux
  • Restrict scope of global variable inside one file in C
  • Arduino AES128 encryption - decryption problem
  • Why is not ampersand needed in the following code?
  • Is it possible to define a function name with #define?
  • Why is the size of the data type different when the value is directly passed to the sizeof operator?
  • Segmentation fault / Incorrect checksum triggered ocasionally when reading lines from a file
  • Adding bits at specific indexes for a uint8_t block
  • how can I use "shadow call stack" feature of clang-8 in x86_64?
  • malloc Assertion Failed
  • Array of structs inside of another struct in C
  • Why does this print the wrong part of the string?
  • Moving the cursor and printing characters in console doesn't work with sleep
  • Confusion in adding an element to a sorted list of integers
  • How I can separate integer number in 3 "houses"? Hundred, Ten and Unity
  • User-implemented memory management
  • What it entails to declare and initialize a pointer in this way: int (*p)[n]
  • Function to look though any struct in C
  • Microcontroller not recognized at USB port when connected via pins
  • Unexpected bitshift behavior in C
  • Model 3D spring from points
  • C control reaches end of non void function
  • Tax outputting 0
  • Gtk 3.0 Text View Resizable
  • Statically linking libc into my binary causes it to segfault
  • Child Process Executing Print Statement But Nothing Afterwards
  • Why is the function to find factorial wrong?
  • I am making a prime number checker: when I try to run it, it stopped working
  • I dont understand how this code can display a string by using just getchar
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk