| | |
continuous string
![]() |
Here is the code i have written to accept string from the user.
Can you please modify the code so as it can even count the space as well as print the whole line with the spaces instead of the first word.
Thanks a lot.
Can you please modify the code so as it can even count the space as well as print the whole line with the spaces instead of the first word.
Thanks a lot.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <assert.h> #include <malloc.h> #include <conio.h> int strlength (char input[]) { int count = 0; while (input[count++] != '\0'); return count; } main ( ) { char* s = 0; char inputBuffer; int index = 0; printf("\nEnter the name u want to enter : "); while ((inputBuffer = getchar()) != '#') // some termintaing cond. { s = malloc(sizeof(char)); s[index] = inputBuffer; } printf("The length of this string is %d", strlength(s)); printf("\n"); }
I don't accept change; I don't deserve to live.
>>Can you please modify the code so ...
No, I don't do homework for anyone.
>> s = malloc(sizeof(char));
This just allocates one character, causing a 1 byte memory leak on every loop iteration.
>> s[index] = inputBuffer;
Since the malloc allocates only one character, this line will just scribble all over memory and most likely cause your program to crash big-time.
How to fix the above problems: use realloc and pass the number of bytes to allocate
s = realloc(s,index+1);
Tip: sizeof(char) is ALWAYS 1 regardless of computer, operating system, or compiler.
No, I don't do homework for anyone.
>> s = malloc(sizeof(char));
This just allocates one character, causing a 1 byte memory leak on every loop iteration.
>> s[index] = inputBuffer;
Since the malloc allocates only one character, this line will just scribble all over memory and most likely cause your program to crash big-time.
How to fix the above problems: use realloc and pass the number of bytes to allocate
s = realloc(s,index+1);
Tip: sizeof(char) is ALWAYS 1 regardless of computer, operating system, or compiler.
Last edited by Ancient Dragon; Jun 24th, 2006 at 9:29 am.
>#include <assert.h>
You haven't used this, so there's no point in including it.
>#include <malloc.h>
stdlib.h is the header you want for malloc.
>#include <conio.h>
You don't use anything from conio, so why kill your program's portability?
>main ( )
The correct definition for main is
>while (input[count++] != '\0');
This will never return 0, even for an empty string. Is that what you want?
>s = malloc(sizeof(char));
This is your single biggest problem. It's a memory leak, and you don't allocate enough memory.
>s[index] = inputBuffer;
This is a guaranteed buffer overflow.
>printf("The length of this string is %d", strlength(s));
This is a guaranteed buffer overflow because you don't terminate the string with '\0'.
>}
main returns an int, even when it's implicitly defined. That means you must return a value or invoke undefined behavior.
You haven't used this, so there's no point in including it.
>#include <malloc.h>
stdlib.h is the header you want for malloc.
>#include <conio.h>
You don't use anything from conio, so why kill your program's portability?
>main ( )
The correct definition for main is
int main ( void ).>while (input[count++] != '\0');
This will never return 0, even for an empty string. Is that what you want?
>s = malloc(sizeof(char));
This is your single biggest problem. It's a memory leak, and you don't allocate enough memory.
>s[index] = inputBuffer;
This is a guaranteed buffer overflow.
>printf("The length of this string is %d", strlength(s));
This is a guaranteed buffer overflow because you don't terminate the string with '\0'.
>}
main returns an int, even when it's implicitly defined. That means you must return a value or invoke undefined behavior.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> int strlength ( char input[] ) { int count = 0; while ( input[count] != '\0' ) ++count; return count; } int main ( void ) { char* s = 0; char inputBuffer; int index = 0; printf ( "Enter the name u want to enter: " ); fflush ( stdout ); while ( ( inputBuffer = getchar() ) != '\n' ) { char *temp = realloc ( s, index + 2 ); if ( temp == 0 ) { fputs ( "Memory allocation error", stderr ); break; } s[index++] = inputBuffer; } s[index] = '\0'; puts ( s ); printf ( "The length of this string is %d\n", strlength ( s ) ); return 0; }
I'm here to prove you wrong.
>Why don't you use s[256] so you don't need malloc.
Why 256? That's the problem with using an array, the size is almost always completely arbitrary. This imposes an unnecessary limit on the program.
>Use gets for string input.
Any credibility you might have had with this post was destroyed when you mentioned gets without first preceding it with "NEVER USE".
>if (s[index++] == 0x20) numSpaces++;
Okay, and now your code doesn't quite work on a machine that uses EBCDIC instead of ASCII. C offers you character constants to avoid such a problem:
Why 256? That's the problem with using an array, the size is almost always completely arbitrary. This imposes an unnecessary limit on the program.
>Use gets for string input.
Any credibility you might have had with this post was destroyed when you mentioned gets without first preceding it with "NEVER USE".
>if (s[index++] == 0x20) numSpaces++;
Okay, and now your code doesn't quite work on a machine that uses EBCDIC instead of ASCII. C offers you character constants to avoid such a problem:
C Syntax (Toggle Plain Text)
if (s[index++] == ' ') numSpaces++;
I'm here to prove you wrong.
>Why 256?
I thought its easier that way, no need for malloc or realoc.
>That's the problem with using an array, the size is almost always completely arbitrary. This imposes an >unnecessary limit on the program.
I know that, but after all its only a homework
>Use gets for string input.
>Any credibility you might have had with this post was destroyed when you mentioned gets without first >preceding it with "NEVER USE".
I admit never used string input from user in my life (maybe in school only) so I don't know whats the problem with gets, but if U say so I belive you. Sorry to misled the young student. Use fgets.
>if (s[index++] == 0x20) numSpaces++;
>Okay, and now your code doesn't quite work on a machine that uses EBCDIC instead of ASCII. C offers you >character constants to avoid such a problem:
agree with this
I thought its easier that way, no need for malloc or realoc.
>That's the problem with using an array, the size is almost always completely arbitrary. This imposes an >unnecessary limit on the program.
I know that, but after all its only a homework
>Use gets for string input.
>Any credibility you might have had with this post was destroyed when you mentioned gets without first >preceding it with "NEVER USE".
I admit never used string input from user in my life (maybe in school only) so I don't know whats the problem with gets, but if U say so I belive you. Sorry to misled the young student. Use fgets.
>if (s[index++] == 0x20) numSpaces++;
>Okay, and now your code doesn't quite work on a machine that uses EBCDIC instead of ASCII. C offers you >character constants to avoid such a problem:
C Syntax (Toggle Plain Text)
if (s[index++] == ' ') numSpaces++;
agree with this
Last edited by andor; Jun 24th, 2006 at 11:03 am.
If you want to win, you must not loose (Alan Ford)
Thanks a lot for all your help but let me get one thing straight.
Friend, neither do i approve of the person who does it. By saying that can you write a code i meant that couldsomeone help me write a code.
No friend this is not a homework and i never did mention so, this is just a program i have written to improve my understanding.
@AncientDragon
Can you please explain the meaning of memory leak on each iterationand the scribbling on the memory. I am totally new to such memory concepts and i have run the prog and it is giving the correct output no junk value so how do we know that it is scribbling.
@Miss Narue
using printf this way causes buffer overflow ???
But i have modifed the prog a bit and its workin for strings.
Thanks to all and sorry if i said somthing bad.
•
•
•
•
No, I don't do homework for anyone.
•
•
•
•
I know that, but after all its only a homework
•
•
•
•
>> s = malloc(sizeof(char));
This just allocates one character, causing a 1 byte memory leak on every loop iteration.
>> s[index] = inputBuffer;
Since the malloc allocates only one character, this line will just scribble all over memory and most likely cause your program to crash big-time.
Can you please explain the meaning of memory leak on each iterationand the scribbling on the memory. I am totally new to such memory concepts and i have run the prog and it is giving the correct output no junk value so how do we know that it is scribbling.
@Miss Narue
•
•
•
•
>printf("The length of this string is %d", strlength(s));
This is a guaranteed buffer overflow because you don't terminate the string with '\0'.
But i have modifed the prog a bit and its workin for strings.
C Syntax (Toggle Plain Text)
int main ( ) { char* s = 0; char inputBuffer, prevBuffer; int index = 0; printf("\nEnter the name u want to enter : "); while ((inputBuffer = getchar()) != '#') { s = malloc(sizeof(char)); s[index++] = inputBuffer; prevBuffer = inputBuffer; } printf("The length from main (including null) is %d", index); printf("\nThe length from function (including null) is %d", strlength(s, prevBuffer)); printf("\n"); return 0; }
Thanks to all and sorry if i said somthing bad.
I don't accept change; I don't deserve to live.
>I thought its easier that way, no need for malloc or realoc.
Yes, it is easier. The code is shorter, faster, and easier to debug. But if I asked a question that clearly wanted to use dynamic allocation and you told me to use an array, I'd be tempted to smack you.
>I know that, but after all its only a homework
How do you know that the homework doesn't require dynamic allocation?
>I admit never used string input from user in my life
It doesn't have to be user input, you can just as easily redirect from a file and the problems are still there.
>so I don't know whats the problem with gets
What if a line is longer than the buffer that you pass to gets?
>using printf this way causes buffer overflow ???
No, using strlength that way causes buffer overflow. In strlen, this is your loop:
If the first character in the string is '\0' then there's not a problem, but because it's extremely difficult and unlikely for a user to actually have getchar return 0, that first character will not be '\0' unless you explicitly set it as such, which you don't. Therefore, the loop will walk all over memory that you don't own, and that's a buffer overflow.
>But i have modifed the prog a bit and its workin for strings.
Just because it works doesn't mean it's correct. Your modified program has all of the same problems we've already pointed out to you. I suggest you try to understand what's going on rather than just change code until it appears to work.
Yes, it is easier. The code is shorter, faster, and easier to debug. But if I asked a question that clearly wanted to use dynamic allocation and you told me to use an array, I'd be tempted to smack you.
>I know that, but after all its only a homework
How do you know that the homework doesn't require dynamic allocation?
>I admit never used string input from user in my life
It doesn't have to be user input, you can just as easily redirect from a file and the problems are still there.
>so I don't know whats the problem with gets
What if a line is longer than the buffer that you pass to gets?
>using printf this way causes buffer overflow ???
No, using strlength that way causes buffer overflow. In strlen, this is your loop:
C Syntax (Toggle Plain Text)
while ( input[count] != '\0' )
>But i have modifed the prog a bit and its workin for strings.
Just because it works doesn't mean it's correct. Your modified program has all of the same problems we've already pointed out to you. I suggest you try to understand what's going on rather than just change code until it appears to work.
I'm here to prove you wrong.
>>meaning of memory leak
it occurs when you use one of the functions that allocate memory, such as malloc() then do not free the memory before using the pointer again. In the code you posted, pointer variable s was allocated over and over without ever freeing the memory. You should have called realloc() instead of malloc() so that the memory block would have been expanded.
>>scribbling on the memory
that means the program is writing outside the allocated memory, and it is not know where the program will write the data, could be almost anywhere. In the code you posted, the program could very easly be writing into other variable's memory address, destroying whatever value was legatimately there. Its something like giving a little kid a crayola and a color book -- watch the child scribble all over the page not paying any attention at all to the lines that define the picture. That's what your program was doing.
it occurs when you use one of the functions that allocate memory, such as malloc() then do not free the memory before using the pointer again. In the code you posted, pointer variable s was allocated over and over without ever freeing the memory. You should have called realloc() instead of malloc() so that the memory block would have been expanded.
>>scribbling on the memory
that means the program is writing outside the allocated memory, and it is not know where the program will write the data, could be almost anywhere. In the code you posted, the program could very easly be writing into other variable's memory address, destroying whatever value was legatimately there. Its something like giving a little kid a crayola and a color book -- watch the child scribble all over the page not paying any attention at all to the lines that define the picture. That's what your program was doing.
Last edited by Ancient Dragon; Jun 24th, 2006 at 6:21 pm.
•
•
•
•
Originally Posted by Ancient Dragon
>>meaning of memory leak
it occurs when you use one of the functions that allocate memory, such as malloc() then do not free the memory before using the pointer again. In the code you posted, pointer variable s was allocated over and over without ever freeing the memory. You should have called realloc() instead of malloc() so that the memory block would have been expanded.
Even then i have heeded your advices and rewritten the code.
I even read the tutorials concering the disadvantages of scanf and gets and also flushed the stream buffer before takin input.
It compiles welll but jumps out during execution ie runtime error. The updated code is as follows :
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> // function for evaluating string length int strlength ( char input[] ) { int count = 0; while ( input[count] != '\0' ) ++count; return count; } int main(void) { int ch, index = 0; char *input = 0; char inputBuffer; // procedure for bypassing the junk in stream while ((ch = getchar()) != '\n' && ch != EOF); fputs ("Enter some text: ", stdout); while ((inputBuffer = getchar()) != '/n') { //reallocating taking in consideration the '\0' char* input = (char*) realloc (input, index + 2); if (input == NULL) // mem alloc failed { fputs("Memory allocation error", stderr); exit(1); } else { input[index++] = inputBuffer; // this is the line bothering me } } input[index] = '\0'; printf("The length of the string is %d", strlength(input)); return 0; }
Hope my third attempt of coding is better than the prev two.
Thanks for always evaluating my code.
Thanks to all for help.
I don't accept change; I don't deserve to live.
![]() |
Other Threads in the C Forum
- Previous Thread: Phonebook program!
- Next Thread: Convert characters from lowercase to uppercase
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram homework i/o ide inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix meter microsoft mysql number oddnumber open opendocumentformat openwebfoundation pdf pointer posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard strchr string suggestions systemcall test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






