I am just starting to learn C. I have run into a problem. Actual code is given at the bottom.
I have a function that modifies a dynamically allocated string taken as input and returns the modified string:
I have a dynamically allocated string A and a pointer char *B in main()
.
I call Func()
in main()
with string A as an argument and assigns it to B:
Func(A)
uses parts of the string to populate a dynamically allocated string, char * output (MemoryBlock1).
I free char * input inside func()
, which is the dynamically created string A in main()
.
I return char * output to pointer B.
So that even though Func(A)
ends and the pointer char * output is destroyed, I can access that MemoryBlock1 through B.
Then I call func()
again.
This time with string B (MemoryBlock1) as the input.
It creates a new dynamically allocated string char * output a second time (MemoryBlock2) using parts from B (MemoryBlock1).
I free char * input, which in this instance is B (MemoryBlock1), which was dynamically allocated and assigned to B in the last Func()
call.
B is now pointing to a freed memory.
But in the next line I return output (MemoryBlock2) back to B and B is now pointing to an active memory again.
Again, even though Func(B) ends and the pointer char * output is destroyed, I should have access to MemoryBlock2 through B.
I print B onto the screen.
However, The program output on the screen is blank.
If I omit freeing char * input in Func()
, the program works.
If I dont call B = Func(B);
the program works.
In short, either I have to create strings C, D, E etc in main()
every time I want to modify a previously modified string:
OR
I dont free input / B and keep on leaking memory from every B that becomes input and is left without a pointer when I reassign B to output, whenever I call B = func(B)
;
Any advice on how I can to make this work without additional pointers or memory leaks? Thank you.
// ACTUAL CODE
// To simplify this example, I am just reducing the string instead of modifying it.
// But everything else is the same
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *Func(char *input, int n) {
char *output = (char *)malloc(n * sizeof(char));
// populate output with parts from input.
for (int i = 0; i < n; i++)
{
output[i] = input[i];
}
output[n] = '\0';
// output is now a valid string.
free(input);
// PROBLEM AREA 1
// If this command is omitted, the program works fine even with PROBLEM AREA 2.
return output;
}
int main(int argc, const char *argv[]) {
char *A = (char *)malloc(100 * sizeof(char));
puts("Enter string:");
gets(A);
char *B;
B = Func(A, 80);
// B is now pointing to memory that was pointed to by output in this Func(A) call.
B = Func(B, 77);
B = Func(B, 62);
// PROBLEM AREA 2
// B should point to memory pointed by new output in Func(B) call. Earlier memory of B should have been
// freed by the free(input) command within Func().
// But B appears to be blank, I suspect due to free(input) within Func().
// If I don't call multiple times, program works fine. Even with PROBLEM AREA 1.
printf("%s\n", B); // print whatever string is at B to screen.
free(B);
B = NULL;
//Since B is pointing to dynamic memory and I no longer need B.
return 0;
}
Expected outcome: B should have been printed the reduced string to screen regardless how many time I call B = Func(B,n); where n is reduced each call.
Actual outcome: Program ends with a blank, new line. No erors are shown during compilation. It just runs and prompts a new command line at the terminal.
NOTE Program doesn't work ONLY IF both PROBLEM AREAS are in effect. If either one of them is ommited, program works fine although it with memory leaks.