Last active
October 6, 2018 14:34
-
-
Save pfeilbr/760d2d24063a5a0075ee576626961d63 to your computer and use it in GitHub Desktop.
C scratch area
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
// C scratch area | |
#include <cstdio> | |
#include <memory> | |
#include <cstring> | |
#include <cstdlib> | |
#include <ctime> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <netdb.h> | |
#include <string.h> | |
typedef struct Person | |
{ | |
const char *name; | |
int age; | |
} Person; | |
Person *create_person(const char *name, int age) | |
{ | |
Person *p = (Person *)malloc(sizeof(Person)); | |
p->name = name; | |
p->age = age; | |
return p; | |
} | |
void delete_person(Person *p) | |
{ | |
free((void *)p->name); | |
free(p); | |
} | |
void print_person(Person *p) | |
{ | |
printf("name: %s, age: %d\n", p->name, p->age); | |
} | |
void memory_pressure() | |
{ | |
size_t number_of_elements = 1000000; | |
size_t element_size = 8; | |
int value = 7; | |
for (int i = 0; i < 200; i++) | |
{ | |
void *p = calloc(number_of_elements, element_size); | |
memset(p, value, number_of_elements * element_size); | |
printf("address of p: %p\n", (void *)p); | |
} | |
} | |
void read_file_into_memory_and_print(const char *path) | |
{ | |
FILE *f = fopen(path, "r"); | |
fseek(f, 0, SEEK_END); // position to end-of-file | |
long fsize = ftell(f); // get size | |
fseek(f, 0, SEEK_SET); // position to begining of file | |
char *contents = (char *)malloc(fsize + 1); // allocate memory | |
fread(contents, fsize, 1, f); | |
fclose(f); | |
contents[fsize] = 0; // null terminate string | |
printf("%s\n", contents); | |
} | |
int *create_random_numbers_in_range(int lower, int upper, int count) | |
{ | |
int *nums = (int *)malloc(sizeof(int) * count); | |
srand(time(NULL)); | |
for (int i = 0; i < count; i++) | |
{ | |
int num = (rand() % (upper - lower + 1)) + lower; | |
//printf("%d ", num); | |
//printf("%c ", num); | |
nums[i] = num; | |
} | |
return nums; | |
} | |
char *create_random_string(int len) | |
{ | |
// ASCII capital letters | |
int ascii_begin = 65; // A | |
int ascii_end = 90; // Z | |
char *s = (char *)malloc(len + 1); // caller is responsible to free | |
int count = 4; | |
int *nums = create_random_numbers_in_range(ascii_begin, ascii_end, len); | |
for (int i = 0; i < len; i++) | |
{ | |
s[i] = nums[i]; | |
} | |
s[len] = 0; | |
free(nums); | |
return s; | |
} | |
int int_sort(const void *a, const void *b) | |
{ | |
int result = 1; | |
if (*(char *)a < *(char *)b) | |
{ | |
result = -1; | |
} | |
else if (*(char *)a == *(char *)b) | |
{ | |
result = 0; | |
} | |
else | |
{ | |
result = 1; | |
} | |
return result; | |
} | |
char *create_sorted_random_string(int len) | |
{ | |
char *random_string = create_random_string(len); | |
qsort((void *)random_string, len, sizeof(char), int_sort); | |
return random_string; | |
} | |
void create_random_string_example() | |
{ | |
int random_string_length = 5; | |
char *random_string = create_random_string(random_string_length); | |
printf("random_string(length=%d): %s\n", random_string_length, random_string); | |
} | |
void create_sorted_random_string_example() | |
{ | |
int random_string_length = 10; | |
char *sorted_random_string = create_sorted_random_string(random_string_length); | |
printf("sorted_random_string(length=%d): %s\n", random_string_length, sorted_random_string); | |
} | |
#define PORT 80 | |
int manual_http_client_request_via_socket_example(const char *hostname) | |
{ | |
struct hostent *server; | |
server = gethostbyname(hostname); | |
// build request body | |
char request_body[256] = {0}; | |
sprintf(request_body, "GET / HTTP/1.1\r\nHost: %s\n\r\n", hostname); | |
struct sockaddr_in address; | |
int sock = 0, valread; | |
struct sockaddr_in serv_addr; | |
char buffer[1024] = {0}; | |
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) | |
{ | |
printf("\n Socket creation error \n"); | |
return -1; | |
} | |
memset(&serv_addr, '0', sizeof(serv_addr)); | |
serv_addr.sin_family = AF_INET; | |
serv_addr.sin_port = htons(PORT); | |
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); | |
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) | |
{ | |
printf("\nConnection Failed \n"); | |
return -1; | |
} | |
// send request | |
send(sock, request_body, strlen(request_body), 0); | |
printf("request sent\n"); | |
// receive response | |
valread = recv(sock, buffer, 1024, 0); | |
// print response | |
printf("%s\n", buffer); | |
return 0; | |
} | |
int main() | |
{ | |
int a = 8; | |
printf("address of a: %p\n", (void *)&a); | |
printf("sizeof(int): %lu\n", sizeof(int)); | |
printf("sizeof(a): %lu\n", sizeof(a)); | |
const char *name = "brian"; | |
printf("name: %s\n", name); | |
printf("strlen(name): %lu\n", strlen(name)); | |
char *name_copy = (char *)malloc(strlen(name) + 1); | |
strcpy(name_copy, name); | |
printf("name_copy: %s\n", name_copy); | |
printf("strlen(name_copy): %lu\n", strlen(name_copy)); | |
Person *p1 = create_person(name_copy, 40); | |
printf("sizeof(p1): %lu\n", sizeof(p1)); | |
printf("sizeof(Person): %lu\n", sizeof(Person)); | |
print_person(p1); | |
delete_person(p1); | |
// memory_pressure(); | |
// read_file_into_memory_and_print("/Users/brianpfeil/tmp/a.cpp"); | |
// create_random_string_example(); | |
// create_sorted_random_string_example(); | |
manual_http_client_request_via_socket_example("www.google.com"); | |
// printf("\npress any key to continue ..."); | |
// getc(stdin); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment