I'm just starting an assignment for uni and it's raised a question for me.
I don't understand how to return a string from a function without having a memory leak.
char* trim(char* line) {
int start = 0;
int end = strlen(line) - 1;
/* find the start position of the string */
while(isspace(line[start]) != 0) {
start++;
}
//printf("start is %d\n", start);
/* find the position end of the string */
while(isspace(line[end]) != 0) {
end--;
}
//printf("end is %d\n", end);
/* calculate string length and add 1 for the sentinel */
int len = end - start + 2;
/* initialise char array to len and read in characters */
int i;
char* trimmed = calloc(sizeof(char), len);
for(i = 0; i < (len - 1); i++) {
trimmed[i] = line[start + i];
}
trimmed[len - 1] = '\0';
return trimmed;
}
as you can see I am returning a pointer to char which is an array. I found that if I tried to make the 'trimmed' array by something like:
char trimmed[len];
then the compiler would throw up a message saying that a constant was expected on this line. I assume this meant that for some reason you can't use variables as the array length when initialising an array, although something tells me that can't be right.
So instead I made my array by allocating some memory to a char pointer.
I understand that this function is probably waaaaay sub-optimal for what it is trying to do, but what I really want to know is:
Can you normally initialise an array using a variable to declare the length like:
char trimmed[len];
If I had an array that was of that type (char trimmed[]) would it have the same return type as a pointer to char (ie char*).
If I make my array by allocating some memory and allocating it to a char pointer, how do I free this memory. It seems to me that once I have returned this array, I can't access it to free it as it is a local variable.
So, because of cramming you may also get bad grades after studying hard. Instead of cramming or studying a lot day before the exam, study the whole session. And agree or not, the student who studies the whole session, never cram.
If you are not good at studies, then you should do some training or vocational courses, which will help you to gain some skills. There are various courses like ITI and other diploma courses in various field. Once you have good practical skills, you will have good chance of getting employed on the basis of those skills.
To address (3) - You can free
the newly allocated string from your calling code, once you are finished with it:
char* tmp = trim(myline);
if (tmp != NULL) {
....
free( tmp );
}
But this places a burden on the caller to remember to free the memory. So instead you might consider passing an allocated buffer and a buffer size to trim()
, for example:
void trim(char* line, char *trimmed_buf, int trimmed_buf_len){ ... }
Neil did an excellent job addressing your other questions. Basically an array declaration char trimmed[len];
will declare a local variable on the stack, so while it is syntactically correct to return a char *
to this memory, the memory location that it points to will no longer be valid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With