Fix allocation error by using malloc(sizeof(...))

master
Tait Hoyem 5 years ago
parent 2e292d7d85
commit 21fda67c88

@ -51,8 +51,8 @@ const char* char_to_morse(char letter){
char* string_to_morse(char* string){ char* string_to_morse(char* string){
int string_len = strlen(string); int string_len = strlen(string);
// worse possible case is 8 times the length (assuming all numbers/punctuation, and adding spaces) // worse possible case is 8 times the length (assuming all numbers/punctuation, and adding spaces)
char result[string_len*8]; // +1 for NULL terminator
char* result_ptr = result; char* result = malloc(sizeof(char)*string_len*8);
// sets everything to null in the string, just in case there was data there previously. // sets everything to null in the string, just in case there was data there previously.
strcpy(result, ""); strcpy(result, "");
for (int i = 0; i < string_len; i++){ for (int i = 0; i < string_len; i++){
@ -61,7 +61,7 @@ char* string_to_morse(char* string){
strcat(result, " "); strcat(result, " ");
} }
} }
return result_ptr; return result;
} }
const char morse_to_char(const char* morse){ const char morse_to_char(const char* morse){
@ -72,18 +72,16 @@ const char morse_to_char(const char* morse){
} }
} }
char* morse_to_string(char* morse_to_cpy){ char* morse_to_string(const char* morse_to_cpy){
int morse_length = strlen(morse_to_cpy); int morse_length = strlen(morse_to_cpy);
char morse[morse_length]; char morse[morse_length];
strcpy(morse, morse_to_cpy); strcpy(morse, morse_to_cpy);
// TODO: make this dynamic // allocate the amount of space the morse takes up for the text string
int max_size = 1024; // this could be changed, but I don't see the point in optimizing the small things here.
char result[max_size]; char* result = malloc(sizeof(char)*strlen(morse));
printf("%d\n", morse_length);
char* result_ptr = result;
// copy nothing to the string to avoid extra data // copy nothing to the string to avoid extra data
strcpy(result, ""); //strcpy(result, "");
// split by the space character // split by the space character
char* morse_ptr = strtok(morse, " "); char* morse_ptr = strtok(morse, " ");
@ -95,7 +93,7 @@ char* morse_to_string(char* morse_to_cpy){
// reset the morse ptr for the next section between a space // reset the morse ptr for the next section between a space
morse_ptr = strtok(NULL, " "); morse_ptr = strtok(NULL, " ");
} }
return result_ptr; return result;
} }
char *multi_tok(char *input, char *delimiter) { char *multi_tok(char *input, char *delimiter) {

@ -4,7 +4,7 @@ const char * char_to_morse(char letter);
char * string_to_morse(char* string); char * string_to_morse(char* string);
char * from_morse(char* string); char * from_morse(char* string);
const char morse_to_char(const char* morse); const char morse_to_char(const char* morse);
char * morse_to_string( char* morse); char * morse_to_string( const char* morse);
void print_morse(char* string, bool b1, bool b2, long s1, long s2); void print_morse(char* string, bool b1, bool b2, long s1, long s2);
void reverse_morse(char* string); void reverse_morse(char* string);

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include "morse.h" #include "morse.h"
int tests_run = 0; int tests_run = 0;
@ -21,15 +22,18 @@ const char* FAIL_FORMAT = "\e[31m[%s]: \"%s\" %s \"%s\"\e[0m\n";
const char* PASS_CHAR_FORMAT = "\e[32m[%s]: \'%c\' %s \'%c\'\e[0m\n"; const char* PASS_CHAR_FORMAT = "\e[32m[%s]: \'%c\' %s \'%c\'\e[0m\n";
const char* FAIL_CHAR_FORMAT = "\e[31m[%s]: \'%c\' %s \'%c\'\e[0m\n"; const char* FAIL_CHAR_FORMAT = "\e[31m[%s]: \'%c\' %s \'%c\'\e[0m\n";
const char* FUNCTION_CALL_CFORMAT = "%s(\'%c\'):\n";
const char* FUNCTION_CALL_SFORMAT = "%s(\"%s\"):\n";
void assert_char_eq(const char c1, const char c2){ void assert_char_eq(const char c1, const char c2){
tests_run++; tests_run++;
if (c1 == c2){ if (c1 == c2){
printf(PASS_CHAR_FORMAT, PASS, c1, EQ, c2); printf(PASS_CHAR_FORMAT, PASS, c1, EQ, c2);
tests_passed++; tests_passed++;
} else { } else {
printf(FAIL_CHAR_FORMAT, FAIL, c1, NEQ, c2); printf(FAIL_CHAR_FORMAT, FAIL, c1, NEQ, c2);
tests_failed++; tests_failed++;
} }
} }
void assert_str_eq(const char* s1, const char* s2){ void assert_str_eq(const char* s1, const char* s2){
@ -56,24 +60,28 @@ void assert_str_neq(const char* s1, const char* s2){
void assert_ctm(char input, char* output){ void assert_ctm(char input, char* output){
const char* result = char_to_morse(input); const char* result = char_to_morse(input);
printf(FUNCTION_CALL_CFORMAT, "char_to_morse", input);
assert_str_eq(result, output); assert_str_eq(result, output);
} }
void assert_stm(char* input, char* output){ void assert_stm(char* input, char* output){
char result[strlen(input)*8]; char* result = malloc(sizeof(char)*strlen(input)*8);
// fill with blanks in case of previous data // fill with blanks in case of previous data
// strcpy(result, ""); // strcpy(result, "");
strcpy(result, string_to_morse(input)); result = string_to_morse(input);
printf(FUNCTION_CALL_SFORMAT, "string_to_morse", input);
assert_str_eq(result, output); assert_str_eq(result, output);
} }
void assert_mtc(char* input, char output){ void assert_mtc(char* input, char output){
char result = morse_to_char(input); char result = morse_to_char(input);
printf(FUNCTION_CALL_SFORMAT, "morse_to_char", input);
assert_char_eq(result, output); assert_char_eq(result, output);
} }
void assert_mts(char* input, char* output){ void assert_mts(char* input, char* output){
char* result = morse_to_string(input); char* result = morse_to_string(input);
printf(FUNCTION_CALL_SFORMAT, "morse_to_string", input);
assert_str_eq(result, output); assert_str_eq(result, output);
} }

Loading…
Cancel
Save