A guide to C programming and kernel based on Linux operating system

Source: Internet
Author: User
Tags function prototype printf strlen

Use of dynamic memory

Although the method in the previous section avoids overflow problems, but results in loss of data, let's learn a better way to use dynamic memory. Because dynamic memory is used entirely by the user, you need to use some system calls, and we'll learn about them separately.

The first thing we need is the dynamic memory allocation system call CALLOC () function, whose function prototype is:

#include
void *malloc(size_t size);
void *calloc(size_t nmemb,size_t size);

Both function malloc and calloc are used to allocate dynamic memory space, where the size of the parameter in malloc represents the amount of memory space that the request is allocated, in bytes, and the Calloc parameter Nmemb represents the number of data items allocated to the memory space, and the size of the parameters represents the sizes of each data item , in bytes. Therefore, the CALLOC function allocates memory space of size nmemb*size size.

The biggest difference between calloc and malloc is that the Calloc function initializes the allocated memory space, placing all positions at 0.

When the call succeeds, their return value is a pointer to the allocated memory space, and the return value is NULL when the call fails.

When the use of a piece of dynamic memory ends, you need to manually release it. The system call used is the free () function, whose function prototype is:

#include
void free(void *ptr);

Parameter ptr is a pointer to the dynamic memory to be freed, to be aware of releasing it after the dynamic memory has been used, lest it cause a memory leak. Let's write a specific example of dynamic memory management.

Procedure 4.2 is as follows:

#include
#include
char *upcase(char *inputstring);
int main(void)
{
char *str1;
str1=upcase(“Everybody”); /*调用子函数upcase()*/
printf(“str1=%s\n”,str1);
free(str1);/*释放内存*/
return 0;
}
char *upcase(char *inputstring)
{
char *newstring;
int counter,N;
N=strlen(inputstring); /*N为字符串长度*/
/*申请N+1个字节的内存空间,若出错则报错并退出*/
if(!(newstring=malloc(N+1)))
{
printf(“ERROR ALLOCATING MEMORY!\n”);
exit(255);
}
/*将原字符串拷贝到新申请的内存块*/
strcpy(newstring,inputstring);
for(counter=0;counter
{
if(newstring[counter]>=97&&newstring[counter]<=122)
newstring[counter]-=32; /*将小写字母转换为大写字母*/
}
return newstring;
}

Results Analysis:

In this program, because you are using dynamic memory, the program can return a pointer to the memory space allocated in the child function to the main function. Also, because of the use of dynamic memory, so that the child functions can flexibly allocate the required memory space (note: The reason to request more than one byte of space, because strlen in the string length, does not include the end of the "" flag, but copy the string need to leave space for this string end flag). The results of this program run as follows:

EVERYBODY

Note: N=strlen (inputstring) can be used in the child functions of this program because InputString is a determined string at this time. The 4.1 program in the previous section, NewString, is an uninitialized array of characters, so you can't use strlen to find its length.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.