2022-04-21 16:53:28 +02:00
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "utf-8" / >
< meta name = "generator" content = "pandoc" / >
< meta name = "viewport" content = "width=device-width, initial-scale=1" / >
2023-12-10 09:39:32 +01:00
< link href = "https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel = "stylesheet" integrity = "sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin = "anonymous" >
2022-11-21 14:48:20 +01:00
< title > GTK 4 tutorial< / title >
2022-04-21 16:53:28 +02:00
< style >
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
pre{overflow: visible;}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
2022-04-17 13:54:42 +02:00
}
2022-04-21 16:53:28 +02:00
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::after
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
div.sourceCode { margin: 10px; padding: 16px 10px 8px 10px; border: 2px solid silver; background-color: ghostwhite; overflow-x:scroll}
pre:not(.sourceCode) { margin: 10px; padding: 16px 10px 8px 10px; border: 2px solid silver; background-color: ghostwhite; overflow-x:scroll}
table {margin-left: auto; margin-right: auto; border-collapse: collapse; border: 1px solid;}
th {padding: 2px 6px; border: 1px solid; background-color: ghostwhite;}
td {padding: 2px 6px; border: 1px solid;}
img {display: block; margin-left: auto; margin-right: auto;}
figcaption {text-align: center;}
< / style >
< / head >
< body style = "padding-top: 70px;" >
< div class = "container" >
< nav class = "navbar fixed-top navbar-expand-lg navbar-dark bg-primary" >
< div class = "container-fluid" >
< span class = "navbar-brand" > Gtk4 tutorial< / span >
< button class = "navbar-toggler" type = "button" data-bs-toggle = "collapse" data-bs-target = "#navbarSupportedContent" aria-controls = "navbarSupportedContent" aria-expanded = "false" aria-label = "Toggle navigation" >
< span class = "navbar-toggler-icon" > < / span >
< / button >
< div class = "collapse navbar-collapse" id = "navbarSupportedContent" >
< ul class = "navbar-nav me-auto mb-2 mb-lg-0" >
< li class = "nav-item" >
< a class = "nav-link" href = "index.html" > Home< / a >
< / li >
< li class = "nav-item" >
< a class = "nav-link" href = "sec5.html" > Prev: section5< / a >
< / li >
< li class = "nav-item" >
< a class = "nav-link" href = "sec7.html" > Next: section7< / a >
< / li >
< / ul >
< / div >
< / div >
< / nav >
2023-12-10 09:39:32 +01:00
< div class = "row justify-content-center" >
< div class = "col-xl-10 col-xxl-9" >
2022-12-21 14:31:33 +01:00
< h1 id = "strings-and-memory-management" > Strings and memory
management< / h1 >
2023-07-15 10:27:53 +02:00
< p > GtkTextView and GtkTextBuffer have functions that have string
2022-11-21 14:48:20 +01:00
parameters or return a string. The knowledge of strings and memory
management is useful to understand how to use these functions.< / p >
2022-04-17 13:54:42 +02:00
< h2 id = "string-and-memory" > String and memory< / h2 >
2022-11-21 14:48:20 +01:00
< p > A String is an array of characters that is terminated with ‘ \0’ .
2022-12-21 14:31:33 +01:00
String is not a C type such as char, int, float or double, but a
character array. It behaves like a string in other languages. So, the
pointer is often called ‘ a string’ .< / p >
< p > The following is a sample program.< / p >
2022-11-21 14:48:20 +01:00
< div class = "sourceCode" id = "cb1" > < pre class = "sourceCode c" > < code class = "sourceCode c" > < span id = "cb1-1" > < a href = "#cb1-1" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "dt" > char< / span > a< span class = "op" > [< / span > < span class = "dv" > 10< / span > < span class = "op" > ],< / span > < span class = "op" > *< / span > b< span class = "op" > ;< / span > < / span >
< span id = "cb1-2" > < a href = "#cb1-2" aria-hidden = "true" tabindex = "-1" > < / a > < / span >
< span id = "cb1-3" > < a href = "#cb1-3" aria-hidden = "true" tabindex = "-1" > < / a > a< span class = "op" > [< / span > < span class = "dv" > 0< / span > < span class = "op" > ]< / span > < span class = "op" > =< / span > < span class = "ch" > ' H' < / span > < span class = "op" > ;< / span > < / span >
< span id = "cb1-4" > < a href = "#cb1-4" aria-hidden = "true" tabindex = "-1" > < / a > a< span class = "op" > [< / span > < span class = "dv" > 1< / span > < span class = "op" > ]< / span > < span class = "op" > =< / span > < span class = "ch" > ' e' < / span > < span class = "op" > ;< / span > < / span >
< span id = "cb1-5" > < a href = "#cb1-5" aria-hidden = "true" tabindex = "-1" > < / a > a< span class = "op" > [< / span > < span class = "dv" > 2< / span > < span class = "op" > ]< / span > < span class = "op" > =< / span > < span class = "ch" > ' l' < / span > < span class = "op" > ;< / span > < / span >
< span id = "cb1-6" > < a href = "#cb1-6" aria-hidden = "true" tabindex = "-1" > < / a > a< span class = "op" > [< / span > < span class = "dv" > 3< / span > < span class = "op" > ]< / span > < span class = "op" > =< / span > < span class = "ch" > ' l' < / span > < span class = "op" > ;< / span > < / span >
< span id = "cb1-7" > < a href = "#cb1-7" aria-hidden = "true" tabindex = "-1" > < / a > a< span class = "op" > [< / span > < span class = "dv" > 4< / span > < span class = "op" > ]< / span > < span class = "op" > =< / span > < span class = "ch" > ' o' < / span > < span class = "op" > ;< / span > < / span >
< span id = "cb1-8" > < a href = "#cb1-8" aria-hidden = "true" tabindex = "-1" > < / a > a< span class = "op" > [< / span > < span class = "dv" > 5< / span > < span class = "op" > ]< / span > < span class = "op" > =< / span > < span class = "ch" > ' \0' < / span > < span class = "op" > ;< / span > < / span >
< span id = "cb1-9" > < a href = "#cb1-9" aria-hidden = "true" tabindex = "-1" > < / a > < / span >
< span id = "cb1-10" > < a href = "#cb1-10" aria-hidden = "true" tabindex = "-1" > < / a > b < span class = "op" > =< / span > a< span class = "op" > ;< / span > < / span >
< span id = "cb1-11" > < a href = "#cb1-11" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "co" > /* *b is ' H' */< / span > < / span >
< span id = "cb1-12" > < a href = "#cb1-12" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "co" > /* *(++b) is ' e' */< / span > < / span > < / code > < / pre > < / div >
2022-12-21 14:31:33 +01:00
< p > An array < code > a< / code > is defined as a < code > char< / code > type array
and its size is ten. The first five elements are ‘ H’ , ‘ e’ , ‘ l’ , ‘ l’ ,
‘ o’ . They are character codes. For example, ‘ H’ is the same as 0x48 or
72. The sixth element is ‘ \0’ , which is the same as zero, and indicates
that the sequence of the data ends there. The array represents the
string “Hello”.< / p >
2023-07-15 10:27:53 +02:00
< p > The size of the array is 10, so four bytes aren’ t used. But it’ s OK.
They are just ignored. (If the variable < code > a< / code > is defined out of
functions or its class is static, the four bytes are assigned with zero.
Otherwise, that is to say, the class is auto or register, they are
undefined.)< / p >
< p > The variable < code > b< / code > is a pointer to a character. It is
assigned with < code > a< / code > , so < code > b< / code > points the first element
of < code > a< / code > (character ‘ H’ ). The array < code > a< / code > is
immutable. So < code > a=a+1< / code > causes syntax error.< / p >
< p > On the other hand, < code > b< / code > is a pointer type variable, which
is mutable. So, < code > ++b< / code > , which increases < code > b< / code > by one,
is allowed.< / p >
2022-12-21 14:31:33 +01:00
< p > If a pointer is NULL, it points nothing. So, the pointer is not a
string. It is different from empty string. Empty string is a pointer
2023-07-15 10:27:53 +02:00
points ‘ \0’ .< / p >
2022-12-21 14:31:33 +01:00
< p > There are four cases:< / p >
2022-04-17 13:54:42 +02:00
< ul >
2022-12-21 14:31:33 +01:00
< li > The string is read only< / li >
< li > The string is in static memory area< / li >
< li > The string is in stack< / li >
< li > The string is in memory allocated from the heap area< / li >
2022-04-17 13:54:42 +02:00
< / ul >
< h2 id = "read-only-string" > Read only string< / h2 >
2022-12-21 14:31:33 +01:00
< p > A string literal is surrounded by double quotes like this:< / p >
2022-11-21 14:48:20 +01:00
< div class = "sourceCode" id = "cb2" > < pre class = "sourceCode c" > < code class = "sourceCode c" > < span id = "cb2-1" > < a href = "#cb2-1" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "dt" > char< / span > < span class = "op" > *< / span > s< span class = "op" > ;< / span > < / span >
< span id = "cb2-2" > < a href = "#cb2-2" aria-hidden = "true" tabindex = "-1" > < / a > s < span class = "op" > =< / span > < span class = "st" > " Hello" < / span > < / span > < / code > < / pre > < / div >
2022-12-21 14:31:33 +01:00
< p > “Hello” is a string literal, and is read only. So, the following
program is illegal.< / p >
2022-11-21 14:48:20 +01:00
< div class = "sourceCode" id = "cb3" > < pre class = "sourceCode c" > < code class = "sourceCode c" > < span id = "cb3-1" > < a href = "#cb3-1" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "op" > *(< / span > s< span class = "op" > +< / span > < span class = "dv" > 1< / span > < span class = "op" > )< / span > < span class = "op" > =< / span > < span class = "ch" > ' a' < / span > < span class = "op" > ;< / span > < / span > < / code > < / pre > < / div >
< p > The result is undefined. Probably a bad thing will happen, for
example, a segmentation fault.< / p >
< p > NOTE: The memory of the literal string is allocated when the program
2022-12-21 14:31:33 +01:00
is compiled. It is possible to see the literal strings with
< code > strings< / code > command.< / p >
< pre > < code > $ strings src/tvf/a.out
/lib64/ld-linux-x86-64.so.2
cN< 5
... ... ...
... ... ...
Once upon a time, there was an old man who was called Taketori-no-Okina. It is a japanese word that means a man whose work is making bamboo baskets.
2023-07-15 10:27:53 +02:00
One day, he went into a hill and found a shining bamboo. " What a mysterious bamboo it is!," he said. He cut it, then there was a small cute baby girl in it. The girl was shining faintly. He thought this baby girl is a gift from Heaven and took her home.
2022-12-21 14:31:33 +01:00
His wife was surprized at his story. They were very happy because they had no children.
... ... ...
... ... ...< / code > < / pre >
< p > It tells us that literal strings are embedded in program binary
codes.< / p >
2022-04-17 13:54:42 +02:00
< h2 id = "strings-defined-as-arrays" > Strings defined as arrays< / h2 >
2022-12-21 14:31:33 +01:00
< p > If a string is defined as an array, it’ s stored in static memory area
or stack. It depends on the class of the array. If the array’ s class is
< code > static< / code > , then it’ s placed in static memory area. The
allocated memory lives for the life of the program. This area is
writable.< / p >
< p > If the array’ s class is < code > auto< / code > , it’ s placed in stack. If
the array is defined inside a function, its default class is
< code > auto< / code > . The stack area will disappear when the function
returns to the caller. Arrays defined on the stack are writable.< / p >
< div class = "sourceCode" id = "cb5" > < pre class = "sourceCode c" > < code class = "sourceCode c" > < span id = "cb5-1" > < a href = "#cb5-1" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "dt" > static< / span > < span class = "dt" > char< / span > a< span class = "op" > []< / span > < span class = "op" > =< / span > < span class = "op" > {< / span > < span class = "ch" > ' H' < / span > < span class = "op" > ,< / span > < span class = "ch" > ' e' < / span > < span class = "op" > ,< / span > < span class = "ch" > ' l' < / span > < span class = "op" > ,< / span > < span class = "ch" > ' l' < / span > < span class = "op" > ,< / span > < span class = "ch" > ' o' < / span > < span class = "op" > ,< / span > < span class = "ch" > ' \0' < / span > < span class = "op" > };< / span > < / span >
< span id = "cb5-2" > < a href = "#cb5-2" aria-hidden = "true" tabindex = "-1" > < / a > < / span >
< span id = "cb5-3" > < a href = "#cb5-3" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "dt" > void< / span > < / span >
< span id = "cb5-4" > < a href = "#cb5-4" aria-hidden = "true" tabindex = "-1" > < / a > print_strings < span class = "op" > (< / span > < span class = "dt" > void< / span > < span class = "op" > )< / span > < span class = "op" > {< / span > < / span >
< span id = "cb5-5" > < a href = "#cb5-5" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "dt" > char< / span > b< span class = "op" > []< / span > < span class = "op" > =< / span > < span class = "st" > " Hello" < / span > < span class = "op" > ;< / span > < / span >
< span id = "cb5-6" > < a href = "#cb5-6" aria-hidden = "true" tabindex = "-1" > < / a > < / span >
< span id = "cb5-7" > < a href = "#cb5-7" aria-hidden = "true" tabindex = "-1" > < / a > a< span class = "op" > [< / span > < span class = "dv" > 1< / span > < span class = "op" > ]< / span > < span class = "op" > =< / span > < span class = "ch" > ' a' < / span > < span class = "op" > ;< / span > < span class = "co" > /* Because the array is static, it' s writable. */< / span > < / span >
< span id = "cb5-8" > < a href = "#cb5-8" aria-hidden = "true" tabindex = "-1" > < / a > b< span class = "op" > [< / span > < span class = "dv" > 1< / span > < span class = "op" > ]< / span > < span class = "op" > =< / span > < span class = "ch" > ' a' < / span > < span class = "op" > ;< / span > < span class = "co" > /* Because the array is auto, it' s writable. */< / span > < / span >
< span id = "cb5-9" > < a href = "#cb5-9" aria-hidden = "true" tabindex = "-1" > < / a > < / span >
< span id = "cb5-10" > < a href = "#cb5-10" aria-hidden = "true" tabindex = "-1" > < / a > printf < span class = "op" > (< / span > < span class = "st" > " %s< / span > < span class = "sc" > \n< / span > < span class = "st" > " < / span > < span class = "op" > ,< / span > a< span class = "op" > );< / span > < span class = "co" > /* Hallo */< / span > < / span >
< span id = "cb5-11" > < a href = "#cb5-11" aria-hidden = "true" tabindex = "-1" > < / a > printf < span class = "op" > (< / span > < span class = "st" > " %s< / span > < span class = "sc" > \n< / span > < span class = "st" > " < / span > < span class = "op" > ,< / span > b< span class = "op" > );< / span > < span class = "co" > /* Hallo */< / span > < / span >
< span id = "cb5-12" > < a href = "#cb5-12" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "op" > }< / span > < / span > < / code > < / pre > < / div >
< p > The array < code > a< / code > is defined out of functions. It is placed in
the static memory area even if the < code > static< / code > class is left
out. The compiler calculates the number of the elements (six) and
allocates six bytes in the static memory area. Then, it copies “Hello”
literal string data to the memory.< / p >
< p > The array < code > b< / code > is defined inside the function, so its class
2022-11-21 14:48:20 +01:00
is < code > auto< / code > . The compiler calculates the number of the elements
2022-12-21 14:31:33 +01:00
in the string literal. It is six because it has ‘ \0’ terminator. The
compiler allocates six bytes in the stack and copies “Hello” litaral
string to the stack memory.< / p >
2022-04-17 13:54:42 +02:00
< p > Both < code > a< / code > and < code > b< / code > are writable.< / p >
2022-12-21 14:31:33 +01:00
< p > The memory is allocated and freed by the program automatically so you
don’ t need to allocate or free. The array < code > a< / code > is alive during
the program’ s life time. The array < code > b< / code > is alive when the
function is called until the function returns to the caller.< / p >
2022-04-17 13:54:42 +02:00
< h2 id = "strings-in-the-heap-area" > Strings in the heap area< / h2 >
2022-12-21 14:31:33 +01:00
< p > You can get, use and release memory from the heap area. The standard
C library provides < code > malloc< / code > to get memory and
2022-11-21 14:48:20 +01:00
< code > free< / code > to put back memory. GLib provides the functions
2022-12-21 14:31:33 +01:00
< code > g_new< / code > and < code > g_free< / code > . They are similar to
< code > malloc< / code > and < code > free< / code > .< / p >
< div class = "sourceCode" id = "cb6" > < pre class = "sourceCode c" > < code class = "sourceCode c" > < span id = "cb6-1" > < a href = "#cb6-1" aria-hidden = "true" tabindex = "-1" > < / a > g_new < span class = "op" > (< / span > struct_type< span class = "op" > ,< / span > n_struct< span class = "op" > )< / span > < / span > < / code > < / pre > < / div >
2022-04-17 13:54:42 +02:00
< p > < code > g_new< / code > is a macro to allocate memory for an array.< / p >
< ul >
2022-11-21 14:48:20 +01:00
< li > < code > struct_type< / code > is the type of the element of the
array.< / li >
2022-04-17 13:54:42 +02:00
< li > < code > n_struct< / code > is the size of the array.< / li >
2022-11-21 14:48:20 +01:00
< li > The return value is a pointer to the array. Its type is a pointer to
< code > struct_type< / code > .< / li >
2022-04-17 13:54:42 +02:00
< / ul >
< p > For example,< / p >
2022-12-21 14:31:33 +01:00
< div class = "sourceCode" id = "cb7" > < pre class = "sourceCode c" > < code class = "sourceCode c" > < span id = "cb7-1" > < a href = "#cb7-1" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "dt" > char< / span > < span class = "op" > *< / span > s< span class = "op" > ;< / span > < / span >
< span id = "cb7-2" > < a href = "#cb7-2" aria-hidden = "true" tabindex = "-1" > < / a > s < span class = "op" > =< / span > g_new < span class = "op" > (< / span > < span class = "dt" > char< / span > < span class = "op" > ,< / span > < span class = "dv" > 10< / span > < span class = "op" > );< / span > < / span >
< span id = "cb7-3" > < a href = "#cb7-3" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "co" > /* s points an array of char. The size of the array is 10. */< / span > < / span >
< span id = "cb7-4" > < a href = "#cb7-4" aria-hidden = "true" tabindex = "-1" > < / a > < / span >
< span id = "cb7-5" > < a href = "#cb7-5" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "kw" > struct< / span > tuple < span class = "op" > {< / span > < span class = "dt" > int< / span > x< span class = "op" > ,< / span > y< span class = "op" > ;}< / span > < span class = "op" > *< / span > t< span class = "op" > ;< / span > < / span >
< span id = "cb7-6" > < a href = "#cb7-6" aria-hidden = "true" tabindex = "-1" > < / a > t < span class = "op" > =< / span > g_new < span class = "op" > (< / span > < span class = "kw" > struct< / span > tuple< span class = "op" > ,< / span > < span class = "dv" > 5< / span > < span class = "op" > );< / span > < / span >
< span id = "cb7-7" > < a href = "#cb7-7" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "co" > /* t points an array of struct tuple. */< / span > < / span >
< span id = "cb7-8" > < a href = "#cb7-8" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "co" > /* The size of the array is 5. */< / span > < / span > < / code > < / pre > < / div >
2022-04-17 13:54:42 +02:00
< p > < code > g_free< / code > frees memory.< / p >
2022-12-21 14:31:33 +01:00
< div class = "sourceCode" id = "cb8" > < pre class = "sourceCode c" > < code class = "sourceCode c" > < span id = "cb8-1" > < a href = "#cb8-1" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "dt" > void< / span > < / span >
< span id = "cb8-2" > < a href = "#cb8-2" aria-hidden = "true" tabindex = "-1" > < / a > g_free < span class = "op" > (< / span > gpointer mem< span class = "op" > );< / span > < / span > < / code > < / pre > < / div >
2022-11-21 14:48:20 +01:00
< p > If < code > mem< / code > is NULL, < code > g_free< / code > does nothing.
< code > gpointer< / code > is a type of general pointer. It is the same as
< code > void *< / code > . This pointer can be casted to any pointer type.
Conversely, any pointer type can be casted to < code > gpointer< / code > .< / p >
2022-12-21 14:31:33 +01:00
< div class = "sourceCode" id = "cb9" > < pre class = "sourceCode c" > < code class = "sourceCode c" > < span id = "cb9-1" > < a href = "#cb9-1" aria-hidden = "true" tabindex = "-1" > < / a > g_free < span class = "op" > (< / span > s< span class = "op" > );< / span > < / span >
< span id = "cb9-2" > < a href = "#cb9-2" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "co" > /* Frees the memory allocated to s. */< / span > < / span >
< span id = "cb9-3" > < a href = "#cb9-3" aria-hidden = "true" tabindex = "-1" > < / a > < / span >
< span id = "cb9-4" > < a href = "#cb9-4" aria-hidden = "true" tabindex = "-1" > < / a > g_free < span class = "op" > (< / span > t< span class = "op" > );< / span > < / span >
< span id = "cb9-5" > < a href = "#cb9-5" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "co" > /* Frees the memory allocated to t. */< / span > < / span > < / code > < / pre > < / div >
2022-11-21 14:48:20 +01:00
< p > If the argument doesn’ t point allocated memory it will cause an
error, specifically, a segmentation fault.< / p >
< p > Some GLib functions allocate memory. For example,
< code > g_strdup< / code > allocates memory and copies a string given as an
argument.< / p >
2022-12-21 14:31:33 +01:00
< div class = "sourceCode" id = "cb10" > < pre class = "sourceCode c" > < code class = "sourceCode c" > < span id = "cb10-1" > < a href = "#cb10-1" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "dt" > char< / span > < span class = "op" > *< / span > s< span class = "op" > ;< / span > < / span >
< span id = "cb10-2" > < a href = "#cb10-2" aria-hidden = "true" tabindex = "-1" > < / a > s < span class = "op" > =< / span > g_strdup < span class = "op" > (< / span > < span class = "st" > " Hello" < / span > < span class = "op" > );< / span > < / span >
< span id = "cb10-3" > < a href = "#cb10-3" aria-hidden = "true" tabindex = "-1" > < / a > g_free < span class = "op" > (< / span > s< span class = "op" > );< / span > < / span > < / code > < / pre > < / div >
2022-11-21 14:48:20 +01:00
< p > The string literal “Hello” has 6 bytes because the string has ‘ \0’ at
2022-12-21 14:31:33 +01:00
the end. < code > g_strdup< / code > gets 6 bytes from the heap area and
copies the string to the memory. < code > s< / code > is assigned the start
2022-11-21 14:48:20 +01:00
address of the memory. < code > g_free< / code > returns the memory to the
heap area.< / p >
< p > < code > g_strdup< / code > is described in < a
href="https://docs.gtk.org/glib/func.strdup.html">GLib API
Reference< / a > . The following is extracted from the reference.< / p >
2022-04-17 13:54:42 +02:00
< blockquote >
2022-11-21 14:48:20 +01:00
< p > The returned string should be freed with < code > g_free()< / code > when
no longer needed.< / p >
2022-04-17 13:54:42 +02:00
< / blockquote >
2022-12-21 14:31:33 +01:00
< p > If you forget to free the allocated memory it will remain until the
program ends. Repeated allocation and no freeing cause memory leak. It
is a bug and may bring a serious problem.< / p >
< h2 id = "const-qualifier" > const qualifier< / h2 >
< p > A < code > const< / code > qualified variable can be assigned to initialize
it. Once it is initialized, it is never allowed to change or free.< / p >
2022-11-21 14:48:20 +01:00
< div class = "sourceCode" id = "cb11" > < pre class = "sourceCode c" > < code class = "sourceCode c" > < span id = "cb11-1" > < a href = "#cb11-1" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "dt" > const< / span > < span class = "dt" > int< / span > x < span class = "op" > =< / span > < span class = "dv" > 10< / span > < span class = "op" > ;< / span > < span class = "co" > /* initialization is OK. */< / span > < / span >
< span id = "cb11-2" > < a href = "#cb11-2" aria-hidden = "true" tabindex = "-1" > < / a > < / span >
< span id = "cb11-3" > < a href = "#cb11-3" aria-hidden = "true" tabindex = "-1" > < / a > x < span class = "op" > =< / span > < span class = "dv" > 20< / span > < span class = "op" > ;< / span > < span class = "co" > /* This is illegal because x is qualified with const */< / span > < / span > < / code > < / pre > < / div >
2022-12-21 14:31:33 +01:00
< p > If a function returns < code > const char*< / code > type, the string can’ t
be changed or freed. If a function has a < code > const char *< / code > type
parameter, it ensures that the parameter is not changed in the
function.< / p >
< div class = "sourceCode" id = "cb12" > < pre class = "sourceCode c" > < code class = "sourceCode c" > < span id = "cb12-1" > < a href = "#cb12-1" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "co" > // You never change or free the returned string.< / span > < / span >
< span id = "cb12-2" > < a href = "#cb12-2" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "dt" > const< / span > < span class = "dt" > char< / span > < span class = "op" > *< / span > < / span >
< span id = "cb12-3" > < a href = "#cb12-3" aria-hidden = "true" tabindex = "-1" > < / a > gtk_label_get_text < span class = "op" > (< / span > < / span >
< span id = "cb12-4" > < a href = "#cb12-4" aria-hidden = "true" tabindex = "-1" > < / a > GtkLabel< span class = "op" > *< / span > self< / span >
< span id = "cb12-5" > < a href = "#cb12-5" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "op" > )< / span > < / span >
< span id = "cb12-6" > < a href = "#cb12-6" aria-hidden = "true" tabindex = "-1" > < / a > < / span >
< span id = "cb12-7" > < a href = "#cb12-7" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "co" > // Str keeps itself during the function runs< / span > < / span >
< span id = "cb12-8" > < a href = "#cb12-8" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "dt" > void< / span > < / span >
< span id = "cb12-9" > < a href = "#cb12-9" aria-hidden = "true" tabindex = "-1" > < / a > gtk_label_set_text < span class = "op" > (< / span > < / span >
< span id = "cb12-10" > < a href = "#cb12-10" aria-hidden = "true" tabindex = "-1" > < / a > GtkLabel< span class = "op" > *< / span > self< span class = "op" > ,< / span > < / span >
< span id = "cb12-11" > < a href = "#cb12-11" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "dt" > const< / span > < span class = "dt" > char< / span > < span class = "op" > *< / span > str< / span >
< span id = "cb12-12" > < a href = "#cb12-12" aria-hidden = "true" tabindex = "-1" > < / a > < span class = "op" > )< / span > < / span > < / code > < / pre > < / div >
2023-12-10 09:39:32 +01:00
< / div >
< / div >
2022-04-21 16:53:28 +02:00
< / div >
< script src = "https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity = "sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin = "anonymous" > < / script >
< / body >
< / html >