char* load_text(char* text, int how_many)
{
char* result = 0;
char* here = 0;
result = fgets(text, how_many, stdin);
if (result)
{
here = strchr(text, '\n');
if (here)
*here = '\0';
else
while (getchar() != '\n')
continue;
}
return result;
}
This function is simple, send string from the keyboard and put to the pointer variable and then search new line '\n' in this string and remove in this place with null character '\0' Then remove new line from function when you pressed ENTER
My question is:
Is it good to put string direct to the pointer from function fgets() to pointer *result?
Or is it a bad practice?
Because i searched a lot information about this problem And many peaople told me:
Never put string direct to the pointer, which points to the random address in RAM without making static space or dynamic allocation in RAM !!!
I learn C programming language from Stephen Prat book "C Primer Plus, 6th Edition" and i founded this example in this book
From my point of view, I think putting strings directly to the random address RAM without static declare space for string or dynamic allocation is very bad idea
Correct me if i am wrong? I think this example make really mess in RAM
I want know if my point of view is correct.
CodePudding user response:
There is a notion that memory is associated with pointers. For example, in int *p = malloc(3 * sizeof *p);
, some people say that memory is “allocated to p
”. This is not correct. Memory is allocated, the address of that memory is returned, and that address is assigned to p
, meaning that the value of p
is set to that address. No ownership of the memory is associated with p
, nor is any enduring relationships between p
and the memory formed other than that p
currently holds the address of the memory.
You could follow this with int *q = p;
and int *r = p;
, and then p
, q
, and r
would have the same address, and none of them would have any privileged status over the others. They are simply variables that hold values.
In result = fgets(text, how_many, stdin);
, the value returned by fgets
is assigned to result
. There is no need to allocate any memory for result
to point to before this. In fact, doing so would be counterproductive, because the address assigned to result
would be overwritten by result = fgets(text, how_many, stdin);
.
Is it good to put string direct to the pointer from function fgets() to pointer *result?
It is fine.
And many peaople told me: Never put string direct to the pointer, which points to the random address in RAM without making static space or dynamic allocation in RAM !!!
You should never use a pointer before it has been assigned an appropriate address. For example, you should never use *result
, as in *result = x;
or x = *result;
, before result
has been set to pointer to an appropriate address.
result = fgets(text, how_many, stdin);
sets result
to an appropriate address, so it is fine.
CodePudding user response:
there is no simple answer - it all depends, but your function depends on the caller supplying a valid pointer and a valid length. But c coding is like that , you are at the mercy of your caller. Look at the fgets
function, it has to depend on its caller providing valid arguments too
CodePudding user response:
This function has no responsibility to allocate memory, that has been pushed to the caller. If the caller does not allocate correctly when calling, this function will fail, but through no fault of its own.
Since you do check the result of fgets
if there is an error you do return an error, so it all works out.
When you say this:
Never put string direct to the pointer, which points to the random address in RAM without making static space or dynamic allocation in RAM !!!
I think what you mean is "never write to an uninitialized pointer", which is true. It does not mean "never put a string direct to the pointer" as that doesn't mean anything.
For example, if you had this:
char* x;
load_text(x, 100);
That is undefined behaviour, no allocation was made, but it's not the fault of the load_text
function itself. This is just buggy code that needs to be fixed.