Exercise - Written Test Questions for Dynamic Memory Allocation

Today we share a few classic written test questions, which will become
insert image description here
the first question
~~ ---------------------------- -------------------------------------------------- --------------------~~

void GetMemory(char* p)
{
    
    
	p = (char*)malloc(100);
}
void Test(void)
{
    
    
	char* str = NULL;
	GetMemory(str);
	strcpy(str, "hello world");
	printf(str);
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    
    
	Test();
	return 0;
}

Enter the Test function in the main function to assign str to a null pointer, and str passes parameters to GetMemory. When passing parameters, it is called by value, so the formal parameter is a temporary copy of the actual parameter. When GetMemory performs dynamic memory development, it will be The space is placed in the null pointer, which will not affect the actual parameter str, so when the GetMemory function returns, str is still a null pointer. If we strcpy copy a string to the null pointer, the copy will fail. When the program ends, we have no Release the dynamic memory opened by malloc, so it will cause memory leaks, so our program will eventually get stuck
--------------------------- -------------------------------------------------- --------------------------
Code modification
method one

void GetMemory(char** p)
{
    
    
	p = (char*)malloc(100);
}
void Test(void)
{
    
    
	char* str = NULL;
	GetMemory(&str);
	strcpy(str, "hello world");
	printf(str);
	free(str);
	str = NULL;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    
    
	Test();
	return 0;
}

law two

void GetMemory(char* p)
{
    
    
	return p = (char*)malloc(100);
}
void Test(void)
{
    
    
	char* str = NULL;
	GetMemory(str);
	strcpy(str, "hello world");
	printf(str);
	free(str);
	str = NULL;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    
    
	Test();
	return 0;
}

The second method returns the space opened by malloc
insert image description here
----------------------------------------- -------------------------------------------------- ---------------

After meeting the first one, we will watch our exercise 2, and we will be able to solve it with ease

char* GetMemory(void)
{
    
    
	char p[] = "hello world";
	return p;
}
void Test(void)
{
    
    
	char* str = NULL;
	str = GetMemory();
	printf(str);
}

#include<stdio.h>
int main()
{
    
    
	Test();
	return 0;
}

Solution: This question is not dynamic memory development. In order to prevent everyone from going crazy, we put an extra one. This question is opened on the stack in the GetMemory() function, and the destruction space is given to the operating system, so when calling In the GetMemory() function, although we created a string in the GetMemory() function, when the function ends, it will be popped and destroyed, and the returned address is also an invalid address, and there is no content I need in it
--- -------------------------------------------------- -------------------------------------------------- ----

Exercise three


void GetMemory(char** p, int num)
{
    
    
	*p = (char*)malloc(num);
}
void Test(void)
{
    
    
	char* str = NULL;
	GetMemory(&str, 100);
	strcpy(str, "hello");
	printf(str);
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    
    
	Test();
	return 0;
}

Solution: The solution to this problem is very simple, just add a free to solve the problem

code modification


void GetMemory(char** p, int num)
{
    
    
	*p = (char*)malloc(num);
}
void Test(void)
{
    
    
	char* str = NULL;
	GetMemory(&str, 100);
	strcpy(str, "hello");
	printf(str);
	free(str);
	str = NULL;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    
    
	Test();
	return 0;
}

-------------------------------------------------- -------------------------------------------------- ------
The fourth question

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void Test(void)
{
    
    
	char* str = (char*)malloc(100);
	strcpy(str, "hello");
	free(str);
	if (str != NULL)
	{
    
    
		strcpy(str, "world");
		printf(str);
	}
}
int main()
{
    
    
	Test();
	return 0;
}

The reason for this question is that there is no free
correction


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void Test(void)
{
    
    
	char* str = (char*)malloc(100);
	strcpy(str, "hello");
	free(str);
	if (str != NULL)
	{
    
    
		strcpy(str, "world");
		printf(str);
	}
	free(str);
	str = NULL;
}
int main()
{
    
    
	Test();
	return 0;
}

-----------------------------------------------------------------------------------------------------------

That's all for today's sharing, the next article to update the file, the editor will continue to study hard

Guess you like

Origin blog.csdn.net/2301_76895050/article/details/131877291
Recommended