C : RNA Transcription - Segmentation fault

Hi there,

I am having an issue with submitting the RNA Transcription solution for C.

I coded the solution in Visual Studio and it works fine, but I get the following error when I submit my solution:

make: *** [makefile:22: test] Segmentation fault (core dumped)

As a test, I changed the code to a simple return statement to see if the tests would run. Even with this test I am still receiving the segmentation fault. The simple test code I tried to see if the tests would run is:

char* to_rna(const char* dna) {
if (dna != NULL) return ā€œAā€;
else return ā€œUā€;
}

The above test code works in Visual Studio and outputs ā€œAā€.

What is your complete solution? What is the complete test failure message?

Hi Isaac,

I found the issue with my solution. The segmentation fault was caused by me not returning a properly generated empty string for the NULL / empty string input test.

The code that causes the segmentation fault on the tests.

if (dna == NULL || dna[0] == '\0') return "";

This code worked with the tests.

if (dna == NULL || dna[0] == '\0') {
	char* empty_str = (char*)malloc(1 * sizeof(char));
	if (empty_str == NULL) return NULL; // to make VS happy
	empty_str[0] = '\0';
	return empty_str;
}
2 Likes