Can someone outline the "best practice" (if any!) to handle newline in ANSI-C standard library functions?
I had some confusion with these functions recently related to char array and char pointer. puts(), printf(), strcpy(), strncpy(), memset().
I seem to understand their basic use, but got quite confused on some situations.
1) For unknown reason, sometime I got output:
With the memset() function inserted, I always got the correct output. But, I do not see memset() is combined with these strncpy()/strcpy() very often.
2) The 4 puts() are in a row, but sometime they gave extra blank line that seems come from extra newline (especially in the next example), but I read puts() only appends newline at the end.
Here is another example where I tested with printf() and puts():
I must have missed important rules to use these functions.
so I'm asking my question with "best practice" to narrow down my struggle if that is reasonable. Thanks.
Can someone outline the "best practice" (if any!) to handle newline in ANSI-C standard library functions?
I had some confusion with these functions recently related to char array and char pointer. puts(), printf(), strcpy(), strncpy(), memset().
None of these do anything weird with newlines at all.
Quote:
//Some said don't use strcpy() but use strncpy()!!!
Blindly using anything without knowing what it's for or what it does will cause problems, as it has here. strcpy would have worked.
Quote:
1) For unknown reason, sometime I got output:
With the memset() function inserted, I always got the correct output. But, I do not see memset() is combined with these strncpy()/strcpy() very often.
strncpy doesn't want the size of the origin string: It wants the size of the destination buffer.
And len1 is shorter than the origin string, anyway. strlen gives you the length without the NULL terminator, forcing strncpy to cut off the terminator, leaving it copied unterminated showing whatever garbage might have been left behind it. If you'd used strcpy, you would have been safe.
Quote:
2) The 4 puts() are in a row, but sometime they gave extra blank line that seems come from extra newline (especially in the second example), but I read puts() only appends newline at the end.
Again, random garbage left afterwards from a string that didn't get terminated.
Quote:
I must have missed important rules to use these functions.
so I'm asking my question with "best practice" to narrow down my struggle if that is reasonable. Thanks.
You just confused yourself with pointer logic. "=" does not copy the contents of a pointer. It alters the pointer!
str3 ends up pointing to the exact same memory as string1 pointed to.
Last edited by Corona688; 07-12-2018 at 09:19 PM..
Thank you so much for your patience with my basics still, as I feel getting better to understand more. strncpy doesn't want the size of the origin string: It wants the size of the destination buffer. ...... If you'd used strcpy, you would have been safe.
To see how things are working in the memory in order to understand those garbage left, I got error in the Block Two of following code:***buffer overflow detected ***: ./memset01 terminated
If I switch the two blocks, there is no buffer overflow for the memset().
What is going on in the memory? Thanks!
Last edited by yifangt; 07-16-2018 at 12:00 PM..
Reason: typos
You are depending on the existence of a terminating NUL. You write 128 total and leave no room in the shorter string str1. You leave no terminating NUL. strcpy printf and lots of other functions depend on the existence of correclty terminated strings. Your sis not.
memset does not care where the end of a string is. Make str1[129], then write less than a total of 129 to str1. Or write fewer characters total to str1.
You are getting undefined behavior. That means I cannot know what your environment is doing or has done. You cannot ever know either. It could crash the program, compose a poem, or order a pizza.
I am guessing. If str1 precedes str2 in memory and each one is word-aligned, then the last character you write to str1[128] effectively cause the end of of str1 to be the actual end of str2. As far as strcpy, printf and so on are concerned.
So, when you exchange order or the two variables you change observed behavior.
These 2 Users Gave Thanks to jim mcnamara For This Post:
Stop that! That's not how you use strncpy! And also not why people use it.
Blindly using strncpy because people call strcpy "bad" is worse than just using strcpy in the first place. You are not correcting the risks strcpy allows (unlimited input lengths despite limited buffer size) and causing problems strcpy didn't have had in the first place.
Grasp the basics first and you'll understand what they're talking about.
Last edited by Corona688; 07-16-2018 at 12:40 PM..
Thanks Jim and Corona:
I was not sure the NULL terminator was handled correctly. If str1 precedes str2 in memory and each one is word-aligned, then the last character you write to str1[128] effectively cause the end of of str1 to be the actual end of str2. This is quite twisting to me!! Now my understanding come to this:
1) Doing strncpy() first caused no NUL terminator, and gave me buffer overflow and strcpy() could not run at all.
2) Instead, doing strcpy() first the correct NUL terminator is ensured, then the following strncpy() seems working which still does not provide NUL terminator. But, because the program exits, just the problem did not show up.
Is this correct? Blindly using strncpy because people call strcpy "bad" is worse than just using strcpy in the first place.
I do not know the risk of strcpy() or the correct use of strncpy(). They just happened to come to my exercise. I thought figure out the details may help understanding what's going on in the memory, which is why I tried memset(). Thanks again.
Which sets the contents of buf to { 'H','E','Y',' ','G','U','Y','S' } and doesn't destroy the world. But because it ran out of room, it couldn't store the null terminator. Meaning buf[] is not guaranteed to contain a proper string after you do strncpy, so trying to use it as a string could cause undefined results and possibly blow up the world. So to be really safe, you do:
Which sets buf to { 'H','E','Y',' ','G','U','Y','\0' }, which guarantees the buffer is null-terminated whether strncpy ran out of room or not, and does it all without destroying the world.
These 2 Users Gave Thanks to Corona688 For This Post:
I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line
if
then... (8 Replies)
I follow the description of wiki (Lamport's bakery algorithm - Wikipedia, the free encyclopedia), then implement that algorithm in C, but it doesn't work, Starving is still here, is the implementation worry?
Only print out:
Thread ID: 0 START!
Thread ID: 0 END!
Thread ID: 0 START!... (2 Replies)
I need to run a cronjob and in the cronjob I execute a script that if there is an error produces standard error so I do
/RUNMYSCRIPT 2> mylogfile.log
However, if it runs correctly, I don't get a standard error output, I get a standard out output. How do I redirect both standard error and... (2 Replies)
Hey, I'm completely new at this and I was wondering if there is a way that I would be able to redirect the log files in a directories standard output and standard error into and excel spreadsheet in anyway?
Please remember don't use too advanced of terminology as I just started using shell... (6 Replies)
Hi there
how can i get the result of a command to not give me its error. For example, on certain systems the 'zfs' command below is not available, but this is fine becaues I am testing against $? so i dont want to see the message " command not found" Ive tried outputting to /dev/null 2>&1 to no... (5 Replies)
Hi everybody,
first of all i apologize if my thread's title doesn't make much sense,but i coudn't find a more appropriate name :)
Then i apologize about my question,which probably will sound trivial for you :) :)
I am working on a program which is being tested in Linux but the final target is... (2 Replies)
Is it not a healthy practice to mix C and C++ standard I/O functions together
e.g.
string name; // this is a declared instance of the string class in C++
printf("\nPlease enter your name: ");
cin >> name;
I did something similar in a program Im designing, and used it several... (1 Reply)
Hi,
I am creating a file in Unix using a shell script. The file is getting created in the Unix - ANSI format. My requirement is to convert it to the PC - ANSI format. Can anyone tell me how to do this?
Thanks,
Sunil (0 Replies)
can somebody explain about the ANSI C vs POSIX. say i was using open and fopen, i know that open is POSIX, and fopen is ANSI C. i read that that POSIX is a system call and ANSI C is like a standard library function. wouldn't the fopen function has to call on open function anyway to open any kind... (2 Replies)
Dear All,
I have to develope some C functions in Unix for a Magic program. The original MSE code which compiles the attached C program uses a +z option, but the cc compiler don't know this. The complete command in the compiler script is 'cc -c -Aa +z myfile.c'. The warning message is 'The -z... (4 Replies)