Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How C language dynamically allocates two-dimensional string array

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how C language dynamically allocates two-dimensional string arrays. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Dynamically allocate an array of two-dimensional strings

(1) allocate memory requests that may not be contiguous

Char**pps8Output = (char**) malloc (n * sizeof (char*))

For pps8Output, it gets a dynamically allocated contiguous piece of memory that can hold n char * pointers.

Here is the malloc for each line of memory so that the allocated memory is not necessarily contiguous.

For (int I = 0; I < n; iTunes +) {pps8Output [I] = (char *) malloc (lens * sizeof (char));} release

In line with the principle of first malloc memory, then free.

For (int I = 0; I < n; iTunes +) {free (pps8Output [I]);} free (pps8Output)

The method:

An array of strings with n lines, each dynamically allocated by malloc, so the memory of the entire two-dimensional string array may be discontiguous.

Complete demo:#include#includeint main () {int n = 3; int lens = 10; char**pps8Output = (char**) malloc (n * sizeof (char*)); / / < malloc for (int I = 0; I < n; iBo +) {pps8Output [I] = (char*) malloc (lens * sizeof (char)) Printf ("pps8Output [% d] ptr:%p\ r\ n", I, pps8Output [I]);} / / < free for (int I = 0; I < n; iOutput +) {free (pps8Output [I]);} free (pps8Output); return 0 } / / deubg://pps8Output [0] ptr:0x5625f38782c0 / / pps8Output [1] ptr:0x5625f38792f0 / / pps8Output [2] ptr:0x5625f3879310 (2) allocate continuous memory requests

Char * * pps8Output = (char * *) malloc (n * sizeof (char *))

For pps8Output, it gets a dynamically allocated contiguous piece of memory that can hold n char * pointers.

The following one-time malloc all the memory, and then calculate the starting address of each line, we use the subscript to access the pps8Output and assignment.

Pps8Output [0] = (char *) malloc (n * lens * sizeof (char)); for (int I = 1; I < n; iTunes +) {pps8Output [I] = pps8Output [0] + I * lens;} release

Since it is a malloc, it only needs to be free once.

Free (pps8Output)

The method: all the required memory for one-time malloc, and the allocation mode belongs to continuous memory.

Complete demo:#include#includeint main () {int n = 3; int lens = 10; char**pps8Output = (char**) malloc (n * sizeof (char*)); / / < malloc pps8Output [0] = (char*) malloc (n * lens * sizeof (char)); for (int I = 1; I < n; iBY +) {pps8Output [I] = pps8Output [0] + I * lens Printf ("pps8Output [% d] ptr:%p\ r\ n", I, pps8Output [I]);} / / < free free (pps8Output); return 0;} / / deubg://pps8Output [1] ptr:0x5591309b52ca//pps8Output [2] ptr:0x5591309b52d4 (3) treats a 2D string array as an one-dimensional string array application

Char * ps8Output = (char *) malloc (n * lens * sizeof (char))

Think of a two-dimensional string array as an one-dimensional string array. At the beginning, all the memory needed for an one-time malloc is allocated by manually calculating the index.

For (int I = 0; I < n; iTunes +) {for (int j = 0; j < lens; jacks +) {* (ps8Output + (i*lens) + j) = 'asides'; / / < demonstration of assignment with'a' as an example} release

Since it is a malloc, it only needs to be free once.

Free (ps8Output)

The method:

Think of a two-dimensional string array as an one-dimensional string array. Pps8Output is a string pointer, ps8Output+1 is the address of ps8Output + 1, each line has lens elements, then use lens*i to indicate the number of elements across.

Complete demo:#include#includeint main () {int n = 3; int lens = 10; char * ps8Output = (char *) malloc (n * lens * sizeof (char)); / / < malloc for (int I = 0; I < n; iLike +) {for (int j = 0; j < lens; jacks +) {* (ps8Output + (i*lens) + j) ='a' Printf ("% p\ r\ n", (ps8Output + (i*lens) + j));}} / < free free (ps8Output); return 0 } / / debug://0x5560bb0fb2a0 / / 0x5560bb0fb2a1 / / 0x5560bb0fb2a2 / / 0x5560bb0fb2a3 / 0x5560bb0fb2a4 / / 0x5560bb0fb2a5 / / 0x5560bb0fb2a6 / / 0x5560bb0fb2a7 / / 0x5560bb0fb2a8 / / 0x5560bb0fb2a9 / / 0x5560bb0fb2aa / / 0x5560bb0fb2ab / / 0x5560bb0fb2ac / / 0x5560bb0fb2ad / / 0x5560bb0fb2ae / 0x5560bb0fb2af / / 0x5560bb0fb2b0 / / 0x5560bb0fb2b1 / / 0x5560bb0fb2b2 / / 0x5560bb0fb2b3 / 0x5560bb0fb2b4 / / 0x5560bb0fb2b5 / 0x5560bb0fb2b6 / / 0x5560bb0fb2b7 / 0x5560bb0fb2b8 / / 0x5560bb0fb2b9 / / 0x5560bb0fb2ba / 0x5560bb0fb2bb / / 0x5560bb0fb2bc / / 0x5560bb0fb2bd Thank you for your reading! This is the end of the article on "how C language dynamically allocates two-dimensional string array". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report