Code Comments
Programming Forum and web based access to our favorite programming groups.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,
Post Follow-up to this messagelearning_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.
Post Follow-up to this messagelearning_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.
Post Follow-up to this messageIn 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
Post Follow-up to this messageJonathan 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.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.