#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
char *pszData;
char first[20], second[20];
gets_s(first, sizeof(first));
pszData = (char*)malloc(sizeof(first));
strcpy_s(pszData, sizeof(first), first);
puts(pszData);
gets_s(second, sizeof(second));
pszData = (char*)realloc(pszData, sizeof(first)+sizeof(second));
strcpy_s(pszData, sizeof(first), first);
strcat(pszData, second);
puts(pszData);
free(pszData);
return 0;
}
Hello! I'm studying dynamic allocation!
I'm asking you a question because I have a question while studying with a book and solving the questions in an example sentence!
[Question]
Create a program that stores the first string received from the user in dynamically assigned memory and outputs it to the screen, and adds the second string to the dynamically assigned memory first and outputs it. At this point, resize the memory so that it doesn't run out of memory and cause problems. You can increase the size of your existing allocated memory or reallocate it. Or it's also good to implement both cases.
This code is the answer that someone else posted.
Dynamic allocation doesn't know how much memory the variable will need, but it doesn't give you a huge amount of memory from the beginning, but it gives you as much as you need later? If you want to use this code for that purpose,
char first[20], second[20];
I think it's right to dynamically allocate this part later, not to reduce the large memory (20) from the beginning, but the codes I've referenced so far only give dynamic allocation to the pointer...
pszData = (char*)malloc(sizeof(first));
How do I assign an array dynamically? Do I have to do this? If I can implement what I want, please answer me how to do it!
c array allocation
int main(){
char *pt2;
char *pt;
char a[10];
char c[10];
pt = (char *)malloc(sizeof(char) * 0x20);
pt2 = (char *)malloc(sizeof(char) * 0x20);
printf("%p\n", c);
printf("%p\n", a);
printf("%p\n", &pt);
printf("%p\n", &pt2);
for (int i=0; i<20; i++)
{
printf("%p ", &pt[i]);
}
printf("\n");
for (int i=0; i<20; i++)
{
printf("%p ", &pt2[i]);
}
return 0;
}
//stack
//c: 0x7ffdb8eb4b84 10
//a: 0x7ffdb8eb4b8e 10
//pt: 0x7ffdb8eb4b70 8
//pt2 : 0x7ffdb8eb4b78 8
//heap
//a chunk
//0x16dec20 0x16dec21 0x16dec22 0x16dec23 0x16dec24 0x16dec25 0x16dec26 0x16dec27 0x16dec28 0x16dec29 0x16dec2a 0x16dec2b 0x16dec2c 0x16dec2d 0x16dec2e 0x16dec2f 0x16dec30 0x16dec31 0x16dec32 0x16dec33
//b chunk
//0x16dec50 0x16dec51 0x16dec52 0x16dec53 0x16dec54 0x16dec55 0x16dec56 0x16dec57 0x16dec58 0x16dec59 0x16dec5a 0x16dec5b 0x16dec5c 0x16dec5d 0x16dec5e 0x16dec5f 0x16dec60 0x16dec61 0x16dec62 0x16dec63
Let's say you made it 0x20 in size. If a chunk comes in 0x40 size, it will affect bchunk. (overflow)
If 0x10 size goes into 0x20 size and forcefully reduces the achunk, the information about the achunk in bchunk becomes wrong value. And if you put the wrong value on the Archunk side and put the malicious address value on the pointer side looking at the Archunk information on the bChunk, it becomes a vulnerable factor.
Therefore, it is not possible to automatically change the structurally allocated space, so reallocation is required....
As if the example answer reflects a reproduction (up to 20 characters) that is not provided in the question... Reproduction must be changed...
Since the return value of the malloc function is an address value, it is correct to specify it in the pointer variable (see malloc function).
There seems to be no exact basis for large memory. Dynamic allocation allows 20 size allocation to take up more memory than 20 space in static space...And there is no mention of the size of the initial input in the problem itself. In order to get the answer that the questioner wants, it seems necessary to copy "Ask how many letters you receive" in advance.
#include <stdio.h>
#include <stdlib.h>
int main() {
char *pt;
pt = (char *)malloc(sizeof(char) * 20);
pt[0] = 'h';
pt[1] = 'i';
int *pt2;
pt2 = (int *)malloc(sizeof(int) * 20);
pt2[1] = 12345678;
printf("%c\n", pt[1]); // 'i'
printf("%d\n", pt2[1]); // 12345678
return 0;
}
// The size of *pt and chara[20] are different.
// It doesn't have to do with the fastbin structure.
© 2024 OneMinuteCode. All rights reserved.