Home > Archive > Unix Programming > September 2004 > How can I flush a buffer?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How can I flush a buffer?
|
|
| learning_C++ 2004-09-29, 8:07 pm |
| Hi,
I hope to use the function:
char buffer[20];
sprintf(buffer,"%d + %d = %d",a,b,a+b); But there are some strange
symbols in this buffer. for example: 2 + (null)@PÅ@|úÿ¿ôùÿ¿Ï,@@ùÿ¿á.
How can I empty it before I use it?
Thanks,
| |
| Nils O. Selåsdal 2004-09-29, 8:07 pm |
| learning_C++ wrote:
> Hi,
> I hope to use the function:
>
> char buffer[20];
> sprintf(buffer,"%d + %d = %d",a,b,a+b); But there are some strange
> symbols in this buffer. for example: 2 + (null)@PÅ@|úÿ¿ôùÿ¿Ï,@@ùÿ¿á.
> How can I empty it before I use it?
>
> Thanks,
memset(buffer,0,sizeof(buffer));
However you shouldn't need that, as the sprintf should make it
nul termiated, and you can for must purposes treat it as an ordinary C
string. Also consider snprintf, so you don't accidentally overflow such
buffers.
| |
| Chuck Dillon 2004-09-29, 8:07 pm |
| learning_C++ wrote:
> Hi,
> I hope to use the function:
>
> char buffer[20];
> sprintf(buffer,"%d + %d = %d",a,b,a+b); But there are some strange
> symbols in this buffer. for example: 2 + (null)@PÅ@|úÿ¿ôùÿ¿Ï,@@ùÿ¿á.
> How can I empty it before I use it?
>
> Thanks,
As already posted you shouldn't need to. Just a guess, but, are you
returning buffer from a function? You can't do that since it is an
automatic variable in the scope of the function. When the function
returns the memory assigned to buffer is released for use elsewhere.
By the time you use it it will probably be clobbered.
You need something like:
char *addEm(int a,int b) {
char *buffer=malloc(64); // you can return this memory
if (buffer == NULL) { perror("addEm"); exit(1); };
snprintf(buffer,sizeof(buffer),"%d + %d = %d",a,b,a+b);
return buffer;
}
NOTE: The calling code must free the memory allocated or you will leak
64 bytes for every call to the function. Often times it makes more
sense for the calling code to pass the address of the buffer to the
function...
char *addEm(int a,int b,char *buffer,int buffer_size) {
snprintf(buffer,buffer_size,"%d + %d = %d",a,b,a+b);
return buffer;
}
Or a combination of the two...
char *addEm(int a,int b,char *buffer,int buffer_size) {
if (buffer == NULL) {
buffer=malloc(64);
if (buffer == NULL) { perror("addEm"); exit(1); };
buffer_size=64;
};
snprintf(buffer,buffer_size,"%d + %d = %d",a,b,a+b);
return buffer;
}
HTH...
-- ced
--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.
| |
| Jonathan Adams 2004-09-29, 8:07 pm |
| In article <cjei2q$aer$1@grandcanyon.binc.net>,
Chuck Dillon <spam@nimblegen.com> wrote:
> You need something like:
>
> char *addEm(int a,int b) {
> char *buffer=malloc(64); // you can return this memory
> if (buffer == NULL) { perror("addEm"); exit(1); };
> snprintf(buffer,sizeof(buffer),"%d + %d = %d",a,b,a+b);
> return buffer;
> }
That's not going to work -- sizeof(buffer) will almost certainly *not*
be 64 (it's likely to be 4 or 8). A better approach is something like:
char *
addEm(int a, int b)
{
size_t sz = 64;
char *buffer = malloc(sz);
if (buffer == NULL) {
perror("addEm");
exit(1);
}
(void) snprintf(buffer, sz, "%d + %d = %d", a, b, a+b);
return (buffer);
}
i.e. use a separate variable to track the buffer size. Or use one of the
other approaches you outlined.
Cheers,
- jonathan
| |
| Chuck Dillon 2004-09-29, 8:07 pm |
| Jonathan Adams wrote:
> In article <cjei2q$aer$1@grandcanyon.binc.net>,
> Chuck Dillon <spam@nimblegen.com> wrote:
>
>
>
>
> That's not going to work -- sizeof(buffer) will almost certainly *not*
> be 64 (it's likely to be 4 or 8). A better approach is something like:
Thanks for catching that.
-- ced
>
> char *
> addEm(int a, int b)
> {
> size_t sz = 64;
> char *buffer = malloc(sz);
> if (buffer == NULL) {
> perror("addEm");
> exit(1);
> }
> (void) snprintf(buffer, sz, "%d + %d = %d", a, b, a+b);
> return (buffer);
> }
>
> i.e. use a separate variable to track the buffer size. Or use one of the
> other approaches you outlined.
>
> Cheers,
> - jonathan
--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.
|
|
|
|
|