-
December 20th, 2006, 04:06 PM
#1
memset question
just a quick lil question on memset()
if i do something like this
char buf[128];
memset(buf, 'A', sizeof(buf));
will this overwrite the terminating character \0? since im filling the whole buffer
with A or does memset automatically take into account the \0?
because i kno that \0, \r, and \n only take "1 slot" in the buffer
-
December 20th, 2006, 04:11 PM
#2
Re: memset question
Originally Posted by Alch3mist
will this overwrite the terminating character \0?
There never was a terminating 0 in your buffer.
Originally Posted by Alch3mist
since im filling the whole buffer with A or does memset automatically take into account the \0?
Yes, you are filling the whole thing with 'A'; No, memset doesn't take \0 into account
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
December 20th, 2006, 04:16 PM
#3
Re: memset question
ight so would this work better
char buf[128] = 0;
memset(buf, 'A', sizeof(buf)-1);
or do i have to specify each time after lets say memset, memcpy, strcpy,strcat to place a \0 at the end of my buffer
-
December 20th, 2006, 04:20 PM
#4
Re: memset question
Originally Posted by Alch3mist
ight so would this work better
char buf[128] = 0;
Unless buf is declared as having 129 elements, that is a memory access violation.
Regards,
Paul McKenzie
-
December 20th, 2006, 05:49 PM
#5
Re: memset question
I guess, he meant
char buf[128] = {'\0'};
which will be ok.
-
December 20th, 2006, 07:27 PM
#6
Re: memset question
Originally Posted by Alch3mist
will this overwrite the terminating character \0? since im filling the whole buffer with A or does memset automatically take into account the \0?
because i kno that \0, \r, and \n only take "1 slot" in the buffer
Only subset of standard C function (str* functions, formating funcitons as printf/scanf, fgets/fputs etc.) knows or cares about null-terminated 'strings' - for every other function (including memset) it is just array of chars, with \0 having no special meaning. So, when you want to set null-terminated string with memset, you have to add the terminating null yourself. Also keep in mind that you need space for the terminating null, so for 128-char string, you need array of size for 129 chars. It's mess, i know - if you have chance to use C++ and std::string, go for it
-
December 21st, 2006, 02:06 AM
#7
Re: memset question
right so would this work better
char buf[128] = 0;
memset(buf, 'A', sizeof(buf));
no that won't work cause of coarse your putting your null terminator in the first character of your array... Then you're overwriting that character with the memset... Try this..
Code:
char buf[128];
memset( buff, '\0', sizeof(buff));
memset( buff, 'A', sizeof( buff) -1 );
or you could do this..
Code:
char buff[128];
memset( buff, 'A', sizeof( buff ) -1 );
buff[ sizeof(buff)-1 ] = '\0';
[/code]
-
December 21st, 2006, 02:07 AM
#8
Re: memset question
Originally Posted by Paul McKenzie
Unless buf is declared as having 129 elements, that is a memory access violation.
Regards,
Paul McKenzie
Paul check that... when you initialize an array like that it always begins from the beginning....
you must not have realized it was initializing from the variable declaration..
thus
Code:
char buff[128] = 0; // is fine... possible casting issue
char buff[128] = "yada yad yada"; // is fine
char buff[128] = '\0'; // is also fine
Last edited by JMS; December 21st, 2006 at 02:10 AM.
-
December 21st, 2006, 02:57 AM
#9
Re: memset question
Code:
char buff[128] = 0;
char buff[128] = '\0';
no, these are not valid initializers for an array
-
December 21st, 2006, 03:35 AM
#10
Re: memset question
Originally Posted by spoon!
Code:
char buff[128] = 0;
char buff[128] = '\0';
no, these are not valid initializers for an array
That's correct. They only set the value in pos. 128 to 0, not the whole array. As it was stated before, you need brackets to set the array to 0 when declaring it.
'You help me, and I, in turn, am helped by you!'
-H.S.
-
December 21st, 2006, 03:40 AM
#11
Re: memset question
I think the solution from JMS is probably the best
Code:
char buff[128];
memset( buff, 'A', sizeof( buff ) -1 );
buff[ sizeof(buff)-1 ] = '\0';
Also, call me crazy, but I recall when initializing arrays, you need to use the initializer list
Code:
char buff[128] = {'\0'};
So I don't think either of these are correct for initializing an array
Code:
char buff[128] = 0;
OR
char buff[128] = '\0'
Please rate my post if you felt it was helpful
-
December 21st, 2006, 04:38 AM
#12
Re: memset question
Originally Posted by Paul McKenzie
Unless buf is declared as having 129 elements, that is a memory access violation.
Regards,
Paul McKenzie
compiler won't compile this or have an extension which allows defining that way, in this case it is treated exactly as
Code:
char buf[128] = { 0 };
it will never be an access violation but it is for sure not standard. I believe only very few, obscure compilers implement this.
-
December 21st, 2006, 02:43 PM
#13
Re: memset question
I'm pretty sure that what Paul meant was
would cause an access violation.
Otherwise you are right, JMS.
-Andy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|