<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" 
  xmlns:content="http://purl.org/rss/1.0/modules/content/" 
  xmlns:dc="http://purl.org/dc/elements/1.1/" 
  xmlns:atom="http://www.w3.org/2005/Atom" 
  xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" 
  xmlns:media="http://search.yahoo.com/mrss/">
  <channel>
    <title>cpp on Ben&#39;s ideas and projects</title>
    <link>https://ben.the-collective.net/tags/cpp/</link>
    <description>Recent content in cpp on Ben&#39;s ideas and projects</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <managingEditor>locutus@the-collective.net (Ben Mason)</managingEditor>
    <webMaster>locutus@the-collective.net (Ben Mason)</webMaster>
    <copyright>©2022, All Rights Reserved</copyright>
    <lastBuildDate>Sun, 08 Jul 2018 12:17:05 -0400</lastBuildDate>
    <sy:updatePeriod>daily</sy:updatePeriod>
    
        <atom:link href="https://ben.the-collective.net/tags/cpp/index.xml" rel="self" type="application/rss+xml" />
    

      
      <item>
        <title>Strings in C</title>
        <link>https://ben.the-collective.net/my-notes/strings-in-c/</link>
        <pubDate>Sun, 08 Jul 2018 12:17:05 -0400</pubDate>
        <author>locutus@the-collective.net (Ben Mason)</author>
        <atom:modified>Sun, 08 Jul 2018 12:17:05 -0400</atom:modified>
        <guid>https://ben.the-collective.net/my-notes/strings-in-c/</guid>
        <description>Strings (c and c++) Strings are an array of characters * they are accessed using the address of the first character (item) in the array A String is an array of characters ending in a null character \0 char color[] = “blue”; // 5 Char array continaing &amp;#39;b&amp;#39;, &amp;#39;l&amp;#39;, &amp;#39;u&amp;#39;, &amp;#39;e&amp;#39;, &amp;#39;\0&amp;#39; char *colorPtr = “blue”; // pointer to memory location constinaing &amp;#39;blue&amp;#39; don’t forget to size arrays to hold the null character if explicitly defining array length C++ String Read functions Collect unlimited Characters until space, tab or EOL cin &amp;gt;&amp;gt; word;</description>
        <content:encoded>&lt;h1 id=&#34;strings-c-and-c&#34;&gt;Strings (c and c++)&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;Strings are an array of characters&lt;/li&gt;
&lt;li&gt;* they are accessed using the address of the first character (item) in the array&lt;/li&gt;
&lt;li&gt;A String is an array of characters ending in a &lt;em&gt;null character&lt;/em&gt; &lt;code&gt;\0&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;char color[] = “blue”; 
// 5 Char array continaing &amp;#39;b&amp;#39;, &amp;#39;l&amp;#39;, &amp;#39;u&amp;#39;, &amp;#39;e&amp;#39;, &amp;#39;\0&amp;#39;
char *colorPtr = “blue”; 
// pointer to memory location constinaing &amp;#39;blue&amp;#39;
&lt;/code&gt;&lt;/pre&gt;&lt;ul&gt;
&lt;li&gt;don’t forget to size arrays to hold the null character if explicitly defining array length&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;c-string-read-functions&#34;&gt;C++ String Read functions&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Collect unlimited Characters until space, tab or EOL&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;cin &amp;gt;&amp;gt; word;&lt;/code&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Collect up to 19 characters or end on space, tab or EOL&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;cin &amp;gt;&amp;gt; setw(20) &amp;gt;&amp;gt; word;&lt;/code&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Collect full line of text, in the example up to 80 characters ending in a new line (&lt;code&gt;\n&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;char sentence[80];
cin.getline(sentence, 80, ‘\n’);
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;standard-c-libraries&#34;&gt;Standard C Libraries&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;stdio.h&lt;/code&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://www.tutorialspoint.com/c_standard_library/stdio_h.htm&#34;&gt;C Library &amp;lt;stdio.h&amp;gt;&amp;lt;/stdio.h&amp;gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;string.h&lt;/code&gt; – String Library
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://www.tutorialspoint.com/c_standard_library/string_h.htm&#34;&gt;C Library &amp;lt;string.h&amp;gt;&amp;lt;/string.h&amp;gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;stddef.h&lt;/code&gt; – contains standard type definitions&lt;/li&gt;
&lt;li&gt;&lt;code&gt;stdlib&lt;/code&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://www.tutorialspoint.com/c_standard_library/stdlib_h.htm&#34;&gt;C Library &amp;lt;stdlib.h&amp;gt;&amp;lt;/stdlib.h&amp;gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;printing&#34;&gt;Printing&lt;/h3&gt;
&lt;p&gt;&lt;a href=&#34;https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm&#34;&gt;C library function printf()&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;int printf(const char *format, ...)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;%[flags][width][.precision][length]specifier&lt;/code&gt;&lt;/p&gt;
&lt;h4 id=&#34;specifier&#34;&gt;specifier&lt;/h4&gt;
&lt;p&gt;&lt;code&gt;%s&lt;/code&gt; -string&lt;br /&gt;
&lt;code&gt;%d&lt;/code&gt; / &lt;code&gt;%i&lt;/code&gt; – Signed Decimal Integer&lt;br /&gt;
&lt;code&gt;%c&lt;/code&gt; – Character&lt;br /&gt;
&lt;code&gt;%f&lt;/code&gt; – Decimal floating point&lt;br /&gt;
&lt;code&gt;%o&lt;/code&gt; – Signed octal&lt;br /&gt;
&lt;code&gt;%u&lt;/code&gt; – Unsigned decimal integer&lt;br /&gt;
&lt;code&gt;%x&lt;/code&gt; – Unsigned hexadecimal&lt;br /&gt;
&lt;code&gt;%p&lt;/code&gt; – Pointer address&lt;/p&gt;
&lt;h4 id=&#34;flags&#34;&gt;flags&lt;/h4&gt;
&lt;p&gt;&lt;code&gt;-&lt;/code&gt; – Left Justify&lt;br /&gt;
&lt;code&gt;+&lt;/code&gt; – Forces to precede the result with a plus or minus sign (+ or -) even for positive numbers&lt;br /&gt;
&lt;code&gt; &lt;/code&gt; – (space) If no sign is going to be written, a blank space is inserted before the value&lt;br /&gt;
&lt;code&gt;#&lt;/code&gt; – Used with o, x or X specifiers the value is preceded with 0, 0x or 0X respectively for values different than zero. Used with e, E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written. Used with g or G the result is the same as with e or E but trailing zeros are not removed.&lt;br /&gt;
&lt;code&gt;0&lt;/code&gt; – Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see width sub-specifier).&lt;/p&gt;
&lt;h3 id=&#34;copy&#34;&gt;Copy&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;char *strcpy(char *s1, const char *s2);&lt;/code&gt; – Copies &lt;code&gt;s2&lt;/code&gt; into &lt;code&gt;s1&lt;/code&gt; and returns &lt;code&gt;s1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;char *strncpy(char *s1, const *s2, size_t n);&lt;/code&gt; – Copies at most &lt;code&gt;n&lt;/code&gt; characters from &lt;code&gt;s2&lt;/code&gt; into &lt;code&gt;s1&lt;/code&gt; and returns &lt;code&gt;s1&lt;/code&gt;
&lt;ul&gt;
&lt;li&gt;May not copy null character if &lt;code&gt;n&lt;/code&gt; is not long enough account for it.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;strlcpy( myString, &amp;quot;abcd&amp;quot;, 4);&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;concatenate&#34;&gt;Concatenate&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;char *strcat(char *s1, const char *s2);&lt;/code&gt; – appends &lt;code&gt;s2&lt;/code&gt; to &lt;code&gt;s1&lt;/code&gt; overwriting &lt;code&gt;s1&lt;/code&gt;’s null terminator returns new &lt;code&gt;s1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;char *strncat(char *s1, const char *s2, size_t n);&lt;/code&gt; – appends n characters of &lt;code&gt;s2&lt;/code&gt; to &lt;code&gt;s1&lt;/code&gt; overwriting &lt;code&gt;s1&lt;/code&gt;’s null terminator returns new &lt;code&gt;s1&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;compare&#34;&gt;Compare&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Compares the ASCII value of the characters to determine if it’s equal to, less than or greater&lt;/li&gt;
&lt;li&gt;&lt;code&gt;int strcmp(const char *s1, const, char *s2);&lt;/code&gt; – Compares &lt;code&gt;s1&lt;/code&gt; and &lt;code&gt;s2&lt;/code&gt; returns less then 0, equal to 0 or greater than 0, depending on if &lt;code&gt;s1&lt;/code&gt; is equal to, less than or greater then &lt;code&gt;s2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;int strncmp(const char *s1, const, char *s2, size_t n);&lt;/code&gt; – Compares n characters of &lt;code&gt;s1&lt;/code&gt; and &lt;code&gt;s2&lt;/code&gt; returns less then 0, equal to 0 or greater than 0, depending on if &lt;code&gt;s1&lt;/code&gt; is equal to, less than or greater then &lt;code&gt;s2&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;misc&#34;&gt;Misc&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;char strtok(char *s1, const char *s2)&lt;/code&gt; – splits &lt;code&gt;s1&lt;/code&gt; into “tokens” which are logical pieces such as words, should be run on &lt;code&gt;s1&lt;/code&gt; until all tokens have been created and it will return &lt;code&gt;NULL&lt;/code&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;s2&lt;/code&gt; is the delimiting character&lt;/li&gt;
&lt;li&gt;calls after the first call should contain &lt;code&gt;NULL&lt;/code&gt; as first argument to continue to tokenize initial argument.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;size_t strlen(const char *s)&lt;/code&gt; – determines length of string &lt;code&gt;s&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;reference&#34;&gt;Reference&lt;/h2&gt;
&lt;p&gt;&lt;a href=&#34;http://embedded.fm/blog/2016/4/25/ew-characters&#34;&gt;Embedded Wednesdays: Characters. — Embedded&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;hashtag&#34;&gt;#coding/c &lt;span class=&#34;hashtag&#34;&gt;#coding/cpp&lt;/p&gt;
</content:encoded>
        <dc:creator>Ben Mason</dc:creator>
        
        
        
        
          
            
              <category>c</category>
            
          
            
              <category>cpp</category>
            
          
            
              <category>notes</category>
            
          
        
        
        
      </item>
      
      <item>
        <title>Pointers in C / C&#43;&#43;</title>
        <link>https://ben.the-collective.net/my-notes/pointers-in-c-c/</link>
        <pubDate>Sun, 08 Jul 2018 12:16:12 -0400</pubDate>
        <author>locutus@the-collective.net (Ben Mason)</author>
        <atom:modified>Sun, 08 Jul 2018 12:16:12 -0400</atom:modified>
        <guid>https://ben.the-collective.net/my-notes/pointers-in-c-c/</guid>
        <description>Pointers (c and c++) The Basics and Pitfalls of Pointers in C | Hackaday
single indirection Define a Pointer int *intPtr
* – indirection operator or de-referencing operator
&amp;amp;varable – Memory address to variable
*intPtr – De-reference pointer (indirection), pull literal value located at the memory address of the pointer
Pointer Example // Declare Variable int value1 = 5000; // Declare 3 pointers int *ptr1; int *ptr2; int *ptr3; // Assign address of variable to ptr1 ptr1 = &amp;amp;value1; // Assign address of ptr1 to ptr2 then ptr2 to ptr3 ptr2 = ptr1; ptr3 = ptr2; // Print literal value stored in value1 printf(&amp;#34;value1: %d\n\r&amp;#34;, value1); printf(&amp;#34;ptr1: %d\n\r&amp;#34;, *ptr1); printf(&amp;#34;ptr2: %d\n\r&amp;#34;, *ptr2); printf(&amp;#34;ptr3: %d\n\r&amp;#34;, *ptr3); /* Output value1: 5000 ptr1: 5000 ptr2: 5000 ptr3: 5000 */ // Set Literal value to the memory address in ptr3, setting the value of value1 to 4000 *ptr3 = 4000; // Print literal values printf(&amp;#34;\n\rAssign 4000 to *ptr3\n\r&amp;#34;); printf(&amp;#34;value1: %d\n\r&amp;#34;, value1); printf(&amp;#34;ptr1: %d\n\r&amp;#34;, *ptr1); printf(&amp;#34;ptr2: %d\n\r&amp;#34;, *ptr2); printf(&amp;#34;ptr3: %d\n\r&amp;#34;, *ptr3); /* Output: Assign 4000 to *ptr3 value1: 4000 ptr1: 4000 ptr2: 4000 ptr3: 4000 */ Pointer Math Adding 1 to will increment the pointer address the size of the value type (ie int, char etc)</description>
        <content:encoded>&lt;h1 id=&#34;pointers-c-and-c&#34;&gt;Pointers (c and c++)&lt;/h1&gt;
&lt;p&gt;&lt;a href=&#34;https://hackaday.com/2018/04/04/the-basics-and-pitfalls-of-pointers-in-c/&#34;&gt;The Basics and Pitfalls of Pointers in C | Hackaday&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;single-indirection&#34;&gt;single indirection&lt;/h2&gt;
&lt;p&gt;Define a Pointer &lt;code&gt;int *intPtr&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;*&lt;/code&gt; – indirection operator or de-referencing operator&lt;br /&gt;
&lt;code&gt;&amp;amp;varable&lt;/code&gt; – Memory address to variable&lt;br /&gt;
&lt;code&gt;*intPtr&lt;/code&gt; – De-reference pointer (indirection), pull literal value located at the memory address of the pointer&lt;/p&gt;
&lt;h3 id=&#34;pointer-example&#34;&gt;Pointer Example&lt;/h3&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;// Declare Variable
int value1 = 5000;
// Declare 3 pointers
int *ptr1;
int *ptr2;
int *ptr3;

// Assign address of variable to ptr1
ptr1 = &amp;amp;value1;
// Assign address of ptr1 to ptr2 then ptr2 to ptr3
ptr2 = ptr1;
ptr3 = ptr2;

// Print literal value stored in value1
printf(&amp;#34;value1: %d\n\r&amp;#34;, value1);
printf(&amp;#34;ptr1:   %d\n\r&amp;#34;, *ptr1);
printf(&amp;#34;ptr2:   %d\n\r&amp;#34;, *ptr2);
printf(&amp;#34;ptr3:   %d\n\r&amp;#34;, *ptr3);

/*
Output
value1: 5000
ptr1:   5000
ptr2:   5000
ptr3:   5000
*/

// Set Literal value to the memory address in ptr3, setting the value of value1 to 4000
*ptr3 = 4000;

// Print literal values
printf(&amp;#34;\n\rAssign 4000 to *ptr3\n\r&amp;#34;);
printf(&amp;#34;value1: %d\n\r&amp;#34;, value1);
printf(&amp;#34;ptr1:   %d\n\r&amp;#34;, *ptr1);
printf(&amp;#34;ptr2:   %d\n\r&amp;#34;, *ptr2);
printf(&amp;#34;ptr3:   %d\n\r&amp;#34;, *ptr3);

/*
Output:
Assign 4000 to *ptr3
value1: 4000
ptr1:   4000
ptr2:   4000
ptr3:   4000
*/
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;pointer-math&#34;&gt;Pointer Math&lt;/h2&gt;
&lt;p&gt;Adding 1 to will increment the pointer address the size of the value type (ie int, char etc)&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;int c[100];
int *pc;

pc = c;

pc = pc + 1 // move form c[0] to c[1] or add 4 bytes
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;multiple-indirection&#34;&gt;Multiple indirection&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;int **ptrptr;&lt;/code&gt; – pointer to a pointer (multiple indirection) to an int, two asterisks`&lt;/p&gt;
&lt;p&gt;&lt;code&gt;**&lt;/code&gt; – Can be used to dereference a pointer to a pointer printing the value in the original pointer&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;int orig;
int *ptr;
int **ptrptr;

ptr = &amp;amp;orig;
ptrptr = &amp;amp;ptr;

// print address of ptr stored in ptrptr
printf(&amp;#34;%p&amp;#34;, ptrptr);
// print value of ptr which is referced by ptrptr
printf(&amp;#34;%d&amp;#34;, **ptrptr);
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;void-pointer&#34;&gt;void pointer&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;void *ptr; // ptr is a void pointer&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;void pointer, also known as the generic pointer, is a special type of pointer that can be pointed at objects of any data type! A void pointer is declared like a normal pointer&lt;/p&gt;
&lt;p&gt;void pointer does not know what type of object it is pointing to, it cannot be dereferenced directly! Rather, the void pointer must first be explicitly cast to another pointer type before it is dereferenced.&lt;/p&gt;
&lt;h3 id=&#34;c&#34;&gt;C++&lt;/h3&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;int *intPtr = static_cast&amp;lt;int*&amp;gt;(voidPtr); // however, if we cast our void pointer to an int pointer... 
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&#34;c-1&#34;&gt;C&lt;/h3&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;int *intPtr = (int *)(voidPtr); // however, if we cast our void pointer to an int pointer...
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;-gt-parenthesis-and-structs&#34;&gt;-&amp;gt;, parenthesis, and structs&lt;/h2&gt;
&lt;p&gt;Also see structs in &lt;a href=&#34;https://ben.the-collective.net/my-notes/c-notes/&#34;&gt;C Notes&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;(From Hack-A_Day Article)&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;// regular struct member assignment
s.some_member = value;
// dereferencing void pointer, will result in compiler error
foo-&amp;gt;some_member = value;
// cast before dereferencing void pointer, this is okay
((struct something *) foo)-&amp;gt;some_member = value;
// dereferencing explicit pointer type, no problems here
bar-&amp;gt;some_member = value;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Note the arrow operator &lt;code&gt;-&amp;gt;&lt;/code&gt; when dereferencing a struct (or union) to access its members. This is a shortcut C offers and is identical to &lt;code&gt;(*variable).member&lt;/code&gt;. Beware though that &lt;code&gt;(*variable).member&lt;/code&gt; is not the same as &lt;code&gt;*variable.member&lt;/code&gt;. The first, enforced by the parentheses, dereference the pointer *before* accessing member, while the second dereferences a pointer-type member inside the struct. This is naturally an easy source for errors, which the arrow operator helps us to prevent.&lt;/p&gt;
&lt;p&gt;Structure pointer members can also be accessed using an operator.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;(*personPtr).age is same as personPtr-&amp;gt;age
(*personPtr).weight is same as personPtr-&amp;gt;weight
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;memory-management&#34;&gt;memory management&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;stack is cleared when function exists (local variables)&lt;/li&gt;
&lt;li&gt;heap remains after the function exits (global memory)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;sizeof()&lt;/code&gt; – returns size of the data in bytes&lt;/p&gt;
&lt;h3 id=&#34;structs&#34;&gt;structs&lt;/h3&gt;
&lt;p&gt;Struct may allocated more then the needed memory for the structure. This will depend on the order of the variables. It will pad out smaller variables to align to the larger variables&lt;/p&gt;
&lt;p&gt;efficient order:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;typedef struct {
	int a;				// 4 bytes
	int c;				// 4 bytes
	double b;			// 8 bytes
	long long int d;	// 8 bytes
} MYSTRUCT;			// 24 bytes allocated
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;inefficient order:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;typedef struct {
	int a;				// 4 bytes + 4 empty padding bytes
	double b;			// 8 bytes
	int c;				// 4 bytes + 4 empty padding bytes
	long long int d;	// 8 bytes
} MYSTRUCT;			// 32 bytes allocated
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;use &lt;code&gt;sizeof()&lt;/code&gt; on types when using &lt;code&gt;malloc()&lt;/code&gt; or &lt;code&gt;calloc()&lt;/code&gt; or any static memory allocations.&lt;/p&gt;
&lt;h3 id=&#34;memory-allocation&#34;&gt;Memory Allocation&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;malloc(size)&lt;/code&gt; – allocated memory on &lt;strong&gt;global&lt;/strong&gt; the heap&lt;/p&gt;
&lt;p&gt;Example: &lt;code&gt;s = (char *)malloc(6)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;calloc(number of array elemant, sizeof each element)&lt;/code&gt; – clears memory ahead of allocation&lt;/p&gt;
&lt;p&gt;Example: &lt;code&gt;s = (char *)calloc(6, sizeof(char))&lt;/code&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Calloc/malloc return
&lt;ul&gt;
&lt;li&gt;Generic pointer (void) on success&lt;/li&gt;
&lt;li&gt;NULL pointer (0) on failure&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;misc&#34;&gt;Misc&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;free(*)&lt;/code&gt; – returns memory to the heap&lt;/p&gt;
&lt;p&gt;&lt;code&gt;realloc(*, size)&lt;/code&gt; – resize memory allocation&lt;/p&gt;
&lt;h2 id=&#34;linked-list&#34;&gt;linked list&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;used to allocate variable size sets of data&lt;/li&gt;
&lt;li&gt;can not be integrated through using pointer math, each element in the set contains the address of the next item in the set&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;single-linked-list&#34;&gt;Single linked list&lt;/h3&gt;
&lt;p&gt;This creates a sequential linked list of items, can only be read in order&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;typedef struct listitem {
	struct listitem *nextitem;
	int data;
} LISTITEM;

int main(void) {
	LISTITEM *listhead, *temp;

	listhead = NULL; // Set the end of the list
	
	for (int i = 0; i &amp;lt; 3; i++) {
		temp = malloc(sizeof(LISTITEM)); 	// allocate memory for list item
		temp-&amp;gt;data = i; 					// set value
		temp-&amp;gt;next = listhead; 				// set next to value of previous elemet
		listhead = temp;					// add element to the  start (head) of the list
	} 
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&#34;double-linked-list&#34;&gt;double linked list&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;In this code, the most recently added item is added to the beginning of list list&lt;/li&gt;
&lt;li&gt;list can be read in any direction&lt;/li&gt;
&lt;/ul&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;typedef struct listitem {
	struct listitem *nextitem; // item before current item
	struct listitem *previtem; // item after current item
	int data;
} LISTITEM;

int main(void) {
	LISTITEM *temp, *head;

	// define end of list
	head.nextitem = (LISTITEM*)&amp;amp;head; 		// Last added item
	head.previtem = (LISTITEM*)&amp;amp;head; 		// first added item
	head.data = -1;
	
	for (int i = 0; i &amp;lt; 3; i++) {
		temp = malloc(sizeof(LISTITEM)); 	// allocate item in list
		temp-&amp;gt;data = i; 					// set data in item
		temp-&amp;gt;nextitem = head.nextitem;		// set nextitem in new element to previous item in list
		head.nextitem = temp;				// set head.nextitem to address of new item on list	
		temp-&amp;gt;previtem = &amp;amp;head;				// set previtem of new item to address of head
		temp-&amp;gt;nextitem-&amp;gt;previtem = temp;	// set previtem of previous item to temp
	} 
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;img src=&#34;images/Screen%20Shot%202018-04-23%20at%2019.32.32.png&#34; alt=&#34;&#34; /&gt;&lt;/figure&gt;### Data structures&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;queue (FIFO structure)&lt;/li&gt;
&lt;li&gt;stack
&lt;ul&gt;
&lt;li&gt;Accessed in a LIFO order
&lt;ul&gt;
&lt;li&gt;usually sized size&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Items are pushed or pop’ed off a stack&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;function-pointers&#34;&gt;function pointers&lt;/h2&gt;
&lt;p&gt;Declared like the following, the types in the definition prototype should match the functions definition.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;int test(int blah) {
..
}

int main() {
	int (*funtionptr)(int);
	functionprt = test;
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It’s common to &lt;code&gt;typedef&lt;/code&gt; the pointer definition to clean the code.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;typedef int(*PFI)(int); 
...

PFI functionptr;
functionptr = test;
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&#34;call-by-reference&#34;&gt;Call by reference&lt;/h3&gt;
&lt;p&gt;Used when passing a pointer into a function allowing a function to access and/or manipulate the data referenced in the pointer. This allows for a function to not have to return data that is manipulated.&lt;/p&gt;
&lt;p&gt;When passing a single variable you must reference the address with a &lt;code&gt;&amp;amp;&lt;/code&gt;&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;
void inc_count(int *count_ptr) {
	(*count_ptr)++;
}

int main() {
	int count = 0;
	
	while (count &amp;lt; 10) {
		int_count(&amp;amp;count);
	}
return(0);

}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;When passing an array the array is already a reference to an address.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;...
// Define function with integer pointer and constant integer arguements
void bubbleSort(int *, const int);

int main() {
    const int arraySize = 10;
		// define and array of values
    int a[arraySize] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37};
  ...
	  // pass array and arraySize into the function
    bubbleSort(a, arraySize);
	...
}

/* bubbleSort takes used the reference to the array and does processing on it directly with out using return to send the value back */
void bubbleSort(int *array, const int size) {
	...
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can pass a pointer into a function allowing that function to change the value without passing the value of returning the variable&lt;/p&gt;
&lt;h3 id=&#34;array-of-functions&#34;&gt;Array of functions&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;You can define an array of functions, below is an example of the array declaration.&lt;/li&gt;
&lt;li&gt;An function reference is just a reference to the location in memory that function is located.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;// function proto types
void function1(int);
void function2(int);
void function3(int);

main() {
	// array declaration
	void (*functionarry[3]) (int) = {function1, function2, function3 };

	// Exectute function 3
	choice = 3;
	(*functionarry[choice])(choice);
}
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;copying&#34;&gt;Copying&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Deep copy – copy memory to new location (duplicate data)
&lt;ul&gt;
&lt;li&gt;More safe&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;shallow copy – two pointers pointer to same memory location (create new instance of data)
&lt;ul&gt;
&lt;li&gt;Less Safe, risk to two changes being made to same memory locations&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
</content:encoded>
        <dc:creator>Ben Mason</dc:creator>
        
        
        
        
          
            
              <category>c</category>
            
          
            
              <category>cpp</category>
            
          
            
              <category>notes</category>
            
          
        
        
        
      </item>
      

    
  </channel>
</rss>
