Last active
May 1, 2020 05:53
-
-
Save nicknapoli82/bce19668d0c834b19b29ad0f228a9145 to your computer and use it in GitHub Desktop.
A simple look at what can happen when you don't use pointers correct
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
typedef struct | |
{ | |
char rgbtBlue; | |
char rgbtGreen; | |
char rgbtRed; | |
char int_padding; | |
} __attribute__((__packed__)) | |
RGBTRIPLE; | |
int main(void) { | |
// lets create an RGBTRIPLE with a little data | |
// this doesn't mean anything, but we will use | |
// this in order to understand how pointers | |
// look at the data in the struct | |
RGBTRIPLE test_subject = {1, 2, 3, 0}; | |
// Notice using these statements here the addresses line up | |
// sequentially in memory. This is how a struct is created | |
// in computer memory. | |
// Notice how the numbers are what we would expect them to be | |
printf("ts value b = %i | at addr %p\n", test_subject.rgbtBlue, &test_subject.rgbtBlue); | |
printf("ts value g = %i | at addr %p\n", test_subject.rgbtGreen, &test_subject.rgbtGreen); | |
printf("ts value r = %i | at addr %p\n", test_subject.rgbtRed, &test_subject.rgbtRed); | |
printf("\n"); | |
// Above in the RGBTRIPLE struct, notice I added | |
// an extra value in there. This is due to the size | |
// of an int, and how a pointer to that struct would | |
// look at the memory. So lets go ahead and make a | |
// mistake | |
int* mistake = (int*)&test_subject.rgbtBlue; | |
*mistake = 12345; // This is the mistake here | |
// So what happened to our test subject? | |
// I took the address of test_subject as a pointer of size int | |
// Then just wrote over all them memory that exists there | |
// Lets see what would print out now having done so... | |
printf("ts value b = %i | at addr %p\n", test_subject.rgbtBlue, &test_subject.rgbtBlue); | |
printf("ts value g = %i | at addr %p\n", test_subject.rgbtGreen, &test_subject.rgbtGreen); | |
printf("ts value r = %i | at addr %p\n", test_subject.rgbtRed, &test_subject.rgbtRed); | |
printf("\n"); | |
// Notice now nothing makes sense. Everything has become | |
// garbage because of my mistake. We need to make sure | |
// and be careful that we know what we are looking at | |
// using pointers. | |
// How do we solve this problem? | |
// Remember the size of the memory address that we | |
// are pointing to is important. So lets try again. | |
test_subject = (RGBTRIPLE){1, 2, 3, 0}; | |
char* not_mistake = &test_subject.rgbtBlue; | |
*not_mistake = 127; | |
not_mistake++; | |
*not_mistake = 126; | |
not_mistake++; | |
*not_mistake = 125; | |
// Here I just made sure the size of the point is equivalent | |
// to the size of the data in the struct and changed it | |
// Does it make sense now? | |
printf("ts value b = %i | at addr %p\n", test_subject.rgbtBlue, &test_subject.rgbtBlue); | |
printf("ts value g = %i | at addr %p\n", test_subject.rgbtGreen, &test_subject.rgbtGreen); | |
printf("ts value r = %i | at addr %p\n", test_subject.rgbtRed, &test_subject.rgbtRed); | |
printf("\n"); | |
// Looks correct to me. | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment