ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Yes, that's what it does for some reason. The code below prints garbage if I use puts() but it is fine if I use printf() instead (see sample of output at the bottom of this post).
Any idea what could be causing this? The odd thing is that the mess always starts after exactly eight properly printed characters. That sort of regularity can't be a coincidence, can it? It almost looks like an encoding issue (I'm using UTF-8) but if it really is, then I don't understand why the printf() is unaffected.
Code:
#include <stdio.h>
#include <string.h>
const char *nChars (const char *, int);
int main (int argc, char * argv[]){
char * name ="Count Dracula++";
int len = strlen(name);
if (argv[1] == 0)
argv[1]="printf";
printf("--->%d characters\n", len);
if (argc >= 2 && !strcmp(argv[1], "puts"))
for (int i = 1; i <= len; i++)
puts(nChars(name, i));
else
for (int i = 1; i <= len; i++)
printf("%s\n", nChars(name, i));
return 0;
}
const char * nChars (const char * strptr, int n){ //returns the next n characters of input
char * ptr;
char nChars[n+1];
int slen = strlen(strptr);
if (slen < n){ // if the string is shorter than n chars, return it unmodified
printf ("String too short. Returning %d.\n", slen);
return strptr;
} else {
ptr = nChars;
for (int i = 0; i < n; i++)
ptr[i]=strptr[i];
ptr[n]='\0'; //append null character
return ptr;
}
}
output of "nChars":
Quote:
C
Co
Cou
Coun
Count
Count
Count D
Count Dr
Count Dra
Count Drac
Count Dracu
Count Dracul
Count Dracula
Count Dracula+
Count Dracula++
output of "nChars puts":
Code:
Co
Cou
Coun
Count
Count
Count D
Count Dr
Count Dr�
Count Dr�
Count Dr��
Count Dr���1
Count Dr���1
Count Dr���1�
Count Dr���1�
It looks to me like nChars is returning a pointer ( ptr ) to stack memory ( the nChars[n+1] char array) . This might not actually be the cause of the symptom you see but it's an "all bets are off" kind of problem. Your program will behave unpredictably until you fix it.
#include <stdio.h>
#include <string.h>
const char *nChars (const char *, int);
int main (int argc, char * argv[]){
char * name ="Count Dracula++";
int len = strlen(name);
if (argv[1] == 0)
argv[1]="printf";
printf("--->%d characters\n", len);
if (argc >= 2 && !strcmp(argv[1], "puts"))
for (int i = 1; i <= len; i++)
puts(nChars(name, i));
else
for (int i = 1; i <= len; i++)
printf("%s\n", nChars(name, i));
return 0;
}
const char * nChars (const char * strptr, int n){ //returns the next n characters of input
char * ptr;
char nChars[n+1];
int slen = strlen(strptr);
if (slen < n){ // if the string is shorter than n chars, return it unmodified
printf ("String too short. Returning %d.\n", slen);
return strptr;
} else {
ptr = nChars;
for (int i = 0; i < n; i++)
ptr[i]=strptr[i];
ptr[n]='\0'; //append null character
return ptr;
}
}
It looks to me like nChars is returning a pointer ( ptr ) to stack memory ( the nChars[n+1] char array) . This might not actually be the cause of the symptom you see but it's an "all bets are off" kind of problem. Your program will behave unpredictably until you fix it.
Yep. A pointer to heap allocated entity should have been used instead.
I do not want to modify the original array so I create a new array (nChars) to copy the parts that I need. The ptr is a pointer to that new array. I guess I could return the array instead of the pointer that doesn't help.
So you mean I'll need to use malloc? I'm vaguely aware of the concept since I dabbled some in C++ a year or two ago but I have a chapter or two to go in my C manual before I reach that part. Well, I'll put it aside for now and give it another try in another few days.
So you mean I'll need to use malloc? I'm vaguely aware of the concept since I dabbled some in C++ a year or two ago but I have a chapter or two to go in my C manual before I reach that part. Well, I'll put it aside for now and give it another try in another few days.
Stack variable seize existence when the scope housing them is left. Period.
If you do not want to use 'malloc', allocate the buffer pointer to which you want to return in the calling scope, pass the buffer as argument and return nothing, i.e. implement a void function.
Last edited by Sergei Steshenko; 07-21-2010 at 08:31 PM.
If you do not want to use 'malloc', allocate the buffer pointer to which you want to return in thew calling scope, pass the buffer as argument and return nothing, i.e. implement a void function.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.