Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

How can I flush a buffer?
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,

Report this thread to moderator Post Follow-up to this message
Old Post
learning_C++
09-30-04 01:07 AM


Re: How can I flush a buffer?
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Nils O. Selåsdal
09-30-04 01:07 AM


Re: How can I flush a buffer?
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Chuck Dillon
09-30-04 01:07 AM


Re: How can I flush a buffer?
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

Report this thread to moderator Post Follow-up to this message
Old Post
Jonathan Adams
09-30-04 01:07 AM


Re: How can I flush a buffer?
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Chuck Dillon
09-30-04 01:07 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:40 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.