RicardasSim

shrink the allocated memory

Apr 10th, 2019
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. //-std=c99 -Wall -Wextra -Wpedantic -Wshadow
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdint.h>
  7.  
  8. int main ()
  9. {
  10.  
  11.    int32_t blk_1[]={100,101,102,103,104,105,106,107,108};
  12.    int32_t blk_2[]={200,201};
  13.    int32_t blk_3[]={300,301,302,303,304,305,306,307,308,309,310,311,312,313,314};
  14.  
  15.    uint32_t blkNumE_1 = sizeof(blk_1)/sizeof(blk_1[0]);
  16.    uint32_t blkNumE_2 = sizeof(blk_2)/sizeof(blk_2[0]);
  17.  
  18.    printf("info: blkNumE_1: %d\n", blkNumE_1);
  19.    printf("info: blkNumE_2: %d\n", blkNumE_2);
  20.  
  21.    int32_t *p1;
  22.    int32_t *p2;
  23.    int32_t *p3;
  24.  
  25.    int32_t *buffer;
  26.    int32_t *bufferTmp;  
  27.    uint32_t buffSize;
  28.  
  29.    //in bytes
  30.    buffSize = sizeof(blk_1) + sizeof(blk_2);
  31.  
  32.    printf("info: buffer size: %d bytes\n", buffSize);
  33.  
  34.    buffer = malloc(buffSize);
  35.  
  36.    if(!buffer)
  37.    {
  38.       printf("error: memory allocation (1)\n");
  39.       return 1;
  40.    }
  41.  
  42.    memcpy(buffer, blk_1, sizeof(blk_1));
  43.  
  44.    /*
  45.       buffer + blkNumE_1; because buffer is int32_t*;
  46.       buffer + N; in bytes adds number * sizeof(int32_t) to the address of the buffer
  47.    */
  48.  
  49.    memcpy(buffer + blkNumE_1, blk_2, sizeof(blk_2));
  50.  
  51.    p1 = buffer;
  52.    p2 = buffer + blkNumE_1;
  53.  
  54.    printf("info: p1[0]: %d\n", p1[0]);
  55.    printf("info: p2[0]: %d\n", p2[0]);
  56.  
  57.    buffSize += sizeof(blk_3);
  58.  
  59.    printf("info: buffer size: %d bytes\n", buffSize);
  60.  
  61.    bufferTmp = realloc(buffer,buffSize);
  62.  
  63.    if(!bufferTmp)
  64.    {
  65.       printf("error: memory allocation (2)\n");
  66.       free(buffer);
  67.       return 1;
  68.    }
  69.  
  70.    buffer = bufferTmp;
  71.  
  72.    memcpy(buffer + blkNumE_1 + blkNumE_2, blk_3, sizeof(blk_3));
  73.  
  74.    p1 = buffer;
  75.    p2 = buffer + blkNumE_1;
  76.    p3 = buffer + blkNumE_1 + blkNumE_2;
  77.  
  78.    printf("info: p1[0]: %d\n", p1[0]);
  79.    printf("info: p2[0]: %d\n", p2[0]);
  80.    printf("info: p3[0]: %d\n", p3[0]);
  81.  
  82.  
  83.    //memmove(p2, p3, sizeof(blk_3));
  84.    memmove(buffer + blkNumE_1, buffer + blkNumE_1 + blkNumE_2, sizeof(blk_3));
  85.  
  86.    buffSize -= sizeof(blk_2);
  87.  
  88.    printf("info: buffer size: %d bytes\n", buffSize);
  89.  
  90.    //shrink the allocated memory
  91.    bufferTmp = realloc(buffer,buffSize);
  92.  
  93.    if(!bufferTmp)
  94.    {
  95.       printf("error: memory allocation (3)\n");
  96.       free(buffer);
  97.       return 1;
  98.    }
  99.  
  100.    buffer = bufferTmp;
  101.  
  102.    p1 = buffer;
  103.    p2 = buffer + blkNumE_1;
  104.  
  105.    printf("info: p1[0]: %d\n", p1[0]);
  106.    printf("info: p2[0]: %d\n", p2[0]);
  107.    printf("info: p2[2]: %d\n", p2[2]);
  108.  
  109.    free(buffer);
  110.  
  111.    return 0;
  112. }
  113.  
  114. /*
  115. output:
  116.  
  117. info: blkNumE_1: 9
  118. info: blkNumE_2: 2
  119. info: buffer size: 44 bytes
  120. info: p1[0]: 100
  121. info: p2[0]: 200
  122. info: buffer size: 104 bytes
  123. info: p1[0]: 100
  124. info: p2[0]: 200
  125. info: p3[0]: 300
  126. info: buffer size: 96 bytes
  127. info: p1[0]: 100
  128. info: p2[0]: 300
  129. info: p2[2]: 302
  130.  
  131.  
  132. valgrind:
  133. HEAP SUMMARY:
  134. in use at exit: 0 bytes in 0 blocks
  135. total heap usage: 4 allocs, 4 frees, 1,268 bytes allocated
  136. All heap blocks were freed -- no leaks are possible
  137. ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
  138.  
  139. */
Add Comment
Please, Sign In to add comment