Reputation: 13
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;
}
Upvotes: 0
Views: 49
Reputation: 42
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; of course @pm100 is right. char *op[30] allocates on a stack and because it's an array it represented as a solid block of memorychar *x
; that's true. If we talk about firmware for MCU your strings will be written to flash. During initialization process to op[1] - op[3] assigns addresses of first element of a stringmalloc
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:0x%04X >> %u - %u >> %s\n", op[0], 0, strlen(op[0]), op[0]);
printf("mem:0x%04X >> %u - %u >> %s\n", op[1], (op[1]-op[0]), strlen(op[1]), op[1]);
printf("mem:0x%04X >> %u - %u >> %s\n", op[2], (op[2]-op[1]), strlen(op[2]), op[2]);
printf("mem:0x%04X >> %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:0x%04X >> %u - %u >> %s\n", op[0], 0, strlen(op[0]), op[0]);
printf("mem:0x%04X >> %u - %u >> %s\n", op[1], (op[1]-op[0]), strlen(op[1]), op[1]);
printf("mem:0x%04X >> %u - %u >> %s\n", op[2], (op[2]-op[1]), strlen(op[2]), op[2]);
printf("mem:0x%04X >> %u - %u >> %s\n", op[3], (op[3]-op[2]), strlen(op[3]), op[3]);
printf("TMP String: mem:0x%04X >> %s\n", tmp_string, tmp_string);
op[29] = "dolor sit amet, consectetur adipiscing elit";
printf("mem:0x%04X >> %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:0x%04X >> op[%u]\n", op[i], i);
}
return 0;
}
Upvotes: -2
Reputation: 50190
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
Upvotes: 3