Home > Blockchain >  How the memory is organized when I declare this array of chars pointer? "char* op[30]"
How the memory is organized when I declare this array of chars pointer? "char* op[30]"

Time:03-05

How that code (of the language C) can works? I don't understand how much memory that chunk of code, "char* op[30]", allocate. And I don't know too how the memory is organized in that situation. I got the ideia how allocate memory with "malloc" works, but the case below is obscure to me.

int main(void) {
    int n;
    scanf("%i", &n);
    
    char* op[30];
    
    op[0] = "Hello, World"
    op[1] = "Hello, World World World"
    op[2] = "Hello, World World World World World World"
    op[3] = "Hello, World World World World World World World World World..."

    printf("%s\n", op[0]);
    printf("%s\n", op[1]);
    printf("%s\n", op[2]);
    printf("%s\n", op[3]);
  
    return 0;
}

CodePudding user response:

You will have a contiguous array on the stack of length 30. Each element of that array is a char* pointer. The first 4 pointers each point to a literal character string. The other entries are uninitialized

CodePudding user response:

  1. char* op[30]; allocates on a heap. In this case you just say to compiler that you are going to store 30 pointers to char arrays;
  2. On the other hand strings are statically defined and compiler already knows how to place them in memory. char *x;
  3. Think about memory as a linked list;
  4. Summarizing 1-3 string places one by one and when you assign new value to the array's element memory management system searches for the end of last string and places new string next to it;
  5. Your code is unsafe. Array of strings defined that way don't have limits and data might be corrupted;
  6. When you use malloc you tells memory management system to find on a heap a solid block of memory with desired size. Memory manager, if possible reserves block, add it to end of list (#2) and returns you a pointer to the start address of that block.

Code to play with:

#include <stdio.h>
#include <string.h>

int main(void)
{
    
    char* op[30];
    
    op[0] = "Hello, World";
    op[1] = "Hello, World World World";
    op[2] = "Hello, World World World World World World";
    op[3] = "Hello, World World World World World World World World World...";

    printf("mem:0xX >> %u - %u >> %s\n", op[0], 0, strlen(op[0]), op[0]);
    printf("mem:0xX >> %u - %u >> %s\n", op[1], (op[1]-op[0]), strlen(op[1]), op[1]);
    printf("mem:0xX >> %u - %u >> %s\n", op[2], (op[2]-op[1]), strlen(op[2]), op[2]);
    printf("mem:0xX >> %u - %u >> %s\n\n", op[3], (op[3]-op[2]), strlen(op[3]), op[3]);

  /* save pointer to op[1] */
  char *tmp_string = op[1];

  /* overrite op[1] */
  op[1] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ";
    
  printf("mem:0xX >> %u - %u >> %s\n", op[0], 0, strlen(op[0]), op[0]);
    printf("mem:0xX >> %u - %u >> %s\n", op[1], (op[1]-op[0]), strlen(op[1]), op[1]);
    printf("mem:0xX >> %u - %u >> %s\n", op[2], (op[2]-op[1]), strlen(op[2]), op[2]);
    printf("mem:0xX >> %u - %u >> %s\n", op[3], (op[3]-op[2]), strlen(op[3]), op[3]);

  printf("TMP String: mem:0xX >> %s\n", tmp_string, tmp_string);

  op[29] = "dolor sit amet, consectetur adipiscing elit";
    
  printf("mem:0xX >> %u - %u >> %s\n\n\n", op[29], (op[29]-op[3]), strlen(op[29]), op[29]);

  /* print mem layout */
  for(int i = 0; i < 30; i  ) {
    printf("mem:0xX >> op[%u]\n", op[i], i);
  }
    return 0;
}

  • Related