473,414 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

what about memset?

When I want to clear memory space this is what I typically do myself,

char a[100];
int i;
for (i=0;i != 100;++i)
a[i]='\0';

Now with the function memset I could do the same thing and it would be
portable. But would it always work?

memset( a, '\0', sizeof a);

Bill
Oct 16 '08 #1
38 3595
Bill Cunningham said:
When I want to clear memory space this is what I typically do myself,

char a[100];
int i;
for (i=0;i != 100;++i)
a[i]='\0';

Now with the function memset I could do the same thing and it would be
portable. But would it always work?

memset( a, '\0', sizeof a);
Yes, for integer types (including char types).

If you only need to do it once, you can do this:

char a[100] = {0};

This is because of default initialisation rules for partial
initialisations.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 16 '08 #2

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:Mb******************************@bt.com...
If you only need to do it once, you can do this:

char a[100] = {0};

This is because of default initialisation rules for partial
initialisations.
So char a[100]={0}; sets all to zero? But that's not exactly \0 is it,
as far as C is concerned?

Bill
Oct 16 '08 #3
Bill Cunningham wrote:
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:Mb******************************@bt.com...
>If you only need to do it once, you can do this:

char a[100] = {0};

This is because of default initialisation rules for partial
initialisations.

So char a[100]={0}; sets all to zero? But that's not exactly \0 is it,
as far as C is concerned?
If that's the case, what is \0?

--
Ian Collins
Oct 16 '08 #4

"Ian Collins" <ia******@hotmail.comwrote in message
news:6l*************@mid.individual.net...
If that's the case, what is \0?
It's supposed to mean null in addition to the string terminator.

Bill
Oct 16 '08 #5

"Ian Collins" <ia******@hotmail.comwrote in message
news:6l*************@mid.individual.net...
If that's the case, what is \0?
When I set all elements of the a array to zero, zero is printed on my
screen 0. When I set all elements to \0 nothing is printed to my screen. No
characters are visible so I am assuming the null character is set.

Bill
Oct 17 '08 #6
Bill Cunningham wrote:
"Ian Collins" <ia******@hotmail.comwrote in message
news:6l*************@mid.individual.net...
>If that's the case, what is \0?
When I set all elements of the a array to zero, zero is printed on my
screen 0. When I set all elements to \0 nothing is printed to my screen. No
characters are visible so I am assuming the null character is set.
Set to 0 or '0'?

--
Ian Collins
Oct 17 '08 #7
"Bill Cunningham" <no****@nspam.invalidwrites:
"Ian Collins" <ia******@hotmail.comwrote in message
news:6l*************@mid.individual.net...
>If that's the case, what is \0?
When I set all elements of the a array to zero, zero is printed on my
screen 0.
The best explanation for this is that you set the elements to '0' not
zero. '0' is some number (most likely 48) that represents a character
in the source character set used by your compiler.
When I set all elements to \0 nothing is printed to my screen. No
characters are visible so I am assuming the null character is set.
\0 is not a number in C so I presume you mean '\0'. '\0' is a integer
constant expression with value 0. '\0' is 0 in almost every way --
the only difference is the way they are written. 0x0 and 000 are
other ways to write the same thing.

--
Ben.
Oct 17 '08 #8

"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
\0 is not a number in C so I presume you mean '\0'. '\0' is a integer
constant expression with value 0. '\0' is 0 in almost every way --
the only difference is the way they are written. 0x0 and 000 are
other ways to write the same thing.
I am pretty sure there's an ascii difference in '\0' that I use and '0'
which would print a bunch of zeros.

Bill
Oct 17 '08 #9
Bill Cunningham wrote:
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>\0 is not a number in C so I presume you mean '\0'. '\0' is a integer
constant expression with value 0. '\0' is 0 in almost every way --
the only difference is the way they are written. 0x0 and 000 are
other ways to write the same thing.
I am pretty sure there's an ascii difference in '\0' that I use and '0'
which would print a bunch of zeros.
What part of '\0' == 0 and '0' != 0 don't you understand?

--
Ian Collins
Oct 17 '08 #10
"Bill Cunningham" <no****@nspam.invalidwrites:
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>\0 is not a number in C so I presume you mean '\0'. '\0' is a integer
constant expression with value 0. '\0' is 0 in almost every way --
the only difference is the way they are written. 0x0 and 000 are
other ways to write the same thing.
I am pretty sure there's an ascii difference in '\0' that I use and '0'
which would print a bunch of zeros.
Yes. There is a difference between '\0' and '0'. I said exactly that
in my post in a part that you snipped. The part of that post that you
quote says nothing at all about '0'. We are in total agreement!

Of course, the very fact that you posted at all suggests that you
misunderstood something, but I can't tell what or how.

--
Ben.
Oct 17 '08 #11
On Fri, 17 Oct 2008 14:19:42 +1300, Ian Collins wrote:
What part of '\0' == 0 and '0' != 0 don't you understand?
I also need some help here :)
So, '\0' is the real zero while '0' has value 48. It means that, in C, we
have 3 step procedure when we compile a file:

1) everything that is stored as a character (in an array or not) is first
converted to its corresponding ASCII table number

2) and then we get the object file
3) In the end, object file is converted into the machine readable file
containing 1s and 0s only.
4) we run it if we like it enough :P


--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is now UnBlocked :)

Oct 17 '08 #12
On Fri, 17 Oct 2008 10:21:03 +0500, arnuld <su*****@invalid.address>
wrote:
>On Fri, 17 Oct 2008 14:19:42 +1300, Ian Collins wrote:
>What part of '\0' == 0 and '0' != 0 don't you understand?

I also need some help here :)
So, '\0' is the real zero while '0' has value 48. It means that, in C, we
It may be 48 on your system. On mine it is 240.
>have 3 step procedure when we compile a file:
The first sentence has absolutely nothing to do with anything that
follows. The standard identifies the phases of compiling. There are
more than three.
>
1) everything that is stored as a character (in an array or not) is first
converted to its corresponding ASCII table number
Nothing on my system is converted to ASCII.

On your system, what ASCII table number is involved in the statement
char x = 17;

All character literals in your source file are already
in the form they need to be for the compiler. Unless the execution
character set is different than the source, what conversion do you
think is needed for
char y = 'a';
>
2) and then we get the object file
Not until much later. Somewhere the executable statements in the
source code must be translated to actual machine instruction (ignoring
the case where C is interpreted).
>

3) In the end, object file is converted into the machine readable file
containing 1s and 0s only.
Since the only values a bit can assume are 1 and 0, everything was
already 1s and 0s before you started.
>

4) we run it if we like it enough :P
--
Remove del for email
Oct 17 '08 #13
On Thu, 16 Oct 2008 22:35:37 -0700, Barry Schwarz wrote:
It may be 48 on your system. On mine it is 240.
I thought ASCII values are same for all platforms.
Nothing on my system is converted to ASCII.
I don't get it. Why ASCII exists ?
On your system, what ASCII table number is involved in the statement
char x = 17;
All character literals in your source file are already
in the form they need to be for the compiler. Unless the execution
character set is different than the source, what conversion do you
think is needed for
char y = 'a';

#include <stdio.h>
int main( void )
{
char x = 17;
char y = 'a';
char z = '0';
printf("x = %d\n", x);
printf("y = %d\n", y);
printf("z = %d\n", z);

return 0;
}
[arnuld@dune ztest]$ gcc4 -ansi -pedantic -Wall -Wextra test.c
[arnuld@dune ztest]$ ./a.out
x = 17
y = 97
z = 48
[arnuld@dune ztest]$

I always thought that everything inside a c programs is saved as
characters and those characters are converted to ASCII equivalents during
compilation.


--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is now UnBlocked :)

Oct 17 '08 #14
arnuld wrote:
>On Thu, 16 Oct 2008 22:35:37 -0700, Barry Schwarz wrote:
>It may be 48 on your system. On mine it is 240.

I thought ASCII values are same for all platforms.
They are, but not all systems use ASCII.

--
Ian Collins
Oct 17 '08 #15
arnuld wrote:
I don't get it. Why ASCII exists ?
ASCII is a system used to unambiguously express characters as numbers so
they may be saved in digital form. ASCII, however, is not the only such
system; EBCDIC is another common one, in which the value of '0' is 240,
and I take it that is what Barry Schwartz is referring to.
>
I always thought that everything inside a c programs is saved as
characters and those characters are converted to ASCII equivalents during
compilation.
Assuming your system /uses/ ASCII (actually, mine uses UTF-8, but the
low end bits are identical), the source code you save /is/ in ASCII
format. ASCII text just means a plain text file. The text editor
"knows" when it finds the number 48 in a file to display the character
'0' on screen because it is expecting ASCII characters. So, your .c
files contain "characters", but they are also in ASCII format.

Compilation parses the grammar of your source files and translates
statements like a = b; to some type of binary code specific to your
machine; hence the term "machine code". The character and string
literals in your code, like the string "hello, world\n" in

printf("hello, world\n");

are stored somewhere in this binary representation (as ASCII, if that's
what your platform uses).

Don't confuse ASCII (or whatever encoding your system uses to represent
characters) with machine code. Machine code is instructions to the
computer that make it do things. ASCII is just a way to represent text
as a series of numeric values.
Oct 17 '08 #16

"Ian Collins" <ia******@hotmail.comwrote in message
news:6l*************@mid.individual.net...
What part of '\0' == 0 and '0' != 0 don't you understand?
Evidently all of the " '\0'==0 " part. If '0' is ascii 48 and '\0' is
ascii 0 then they are not the same ascii character.

Bill
Oct 17 '08 #17

"arnuld" <su*****@invalid.addresswrote in message
news:pa****************************@invalid.addres s...
>
3) In the end, object file is converted into the machine readable file
containing 1s and 0s only.
The binary notation means there's about 2-3v of electricity "turned on"
for 1 and "off" for 0. The absence or presence of an electrical charge.

Bill
Oct 17 '08 #18

"Ian Collins" <ia******@hotmail.comwrote in message
news:6l*************@mid.individual.net...
They are, but not all systems use ASCII.
This is true.

Bill
Oct 17 '08 #19
On Fri, 17 Oct 2008 17:17:37 -0400,
Bill Cunningham <no****@nspam.invalidwrote:
>
"Ian Collins" <ia******@hotmail.comwrote in message
news:6l*************@mid.individual.net...
>What part of '\0' == 0 and '0' != 0 don't you understand?
Evidently all of the " '\0'==0 " part. If '0' is ascii 48 and '\0' is
ascii 0 then they are not the same ascii character.
Read it again, and pay attention to where there are quotes and where
there are no quotes.

No one said that '\0' == '0'. The opposite was said. Several times.

Martien
--
|
Martien Verbruggen | Useful Statistic: 75% of the people make up
| 3/4 of the population.
|
Oct 17 '08 #20
On Oct 16, 11:47*pm, "Bill Cunningham" <nos...@nspam.invalidwrote:
Now with the function memset I could do the same thing and it would be
portable. But would it always work?
I haven't seen a system where it didn't work, and C99 with the latest
corrections guarantees that it works for all integer types. For every
integer type, all value bits zero means the value is zero. There was
the possibility if integers had padding bits that all zero padding
bits would be a trap representation; the latest C99 version guarantees
that (all bits zero) is always a valid representation of the value
zero.

Oct 17 '08 #21
christian.bau wrote:
On Oct 16, 11:47 pm, "Bill Cunningham" <nos...@nspam.invalidwrote:
>Now with the function memset I could do the same thing and it would be
portable. But would it always work?

I haven't seen a system where it didn't work, and C99 with the latest
corrections guarantees that it works for all integer types. For every
integer type, all value bits zero means the value is zero. There was
the possibility if integers had padding bits that all zero padding
bits would be a trap representation; the latest C99 version guarantees
that (all bits zero) is always a valid representation of the value
zero.
For what it's worth, Intel C (and Fortran) are expanding the automatic
replacement of explicit 0 setting loops by memset(). Among other
implications, memset(), or an explicit loop eligible to be replaced by
memset(), are preferred over C++ equivalents.
Oct 17 '08 #22
arnuld wrote:
On Thu, 16 Oct 2008 22:35:37 -0700, Barry Schwarz wrote:
It may be 48 on your system. On mine it is 240.

I thought ASCII values are same for all platforms.
The ASCII values are the same, but not all platforms use ASCII values
for characters.
Nothing on my system is converted to ASCII.

I don't get it. Why ASCII exists ?
To standardize the method used in the United Stated to represent
English words. There's also a completely different system called
EBCDIC which is still in use on many systems. ASCII had to be extended
significantly to be of any use in most of Europe, where most languages
have various special characters that don't occur in English. ISO
created 15 extended versions of ASCII called ISO 8859-1 through ISO
8859-16 (ISO 8859-12 was abandoned). However, extending ASCII is a
totally inadequate strategy for most East Asian languages, where the
number of different characters can run into the thousands. There are
hundreds of different character encodings in use somewhere in the
world, and dozens that are pretty common.

Unicode is a pretty popular standard that was created with the goal of
cleaning up this mess. It assigns unique code points to each of more
than 100,000 characters from a very large and diverse set of
languages, and it has lots of unused code points that have been
reserved for additional characters, if needed. UTF-8 is a popular
variable-length encoding for Unicode code points: some characters
require only a single byte, others require multiple bytes; all of the
characters that can be represented in ASCII are represented by a
single byte with the same value it has in ASCII. There's also a 16 bit
variable length encoding, and a 32 bit fixed-width encoding, and a few
other alternatives as well.

C has a certain limited amount of support for Unicode. Section 6.4.3
describes Universal Character Names (UCN), such as \U0B4D. According
to Appendix D, \U0B4D represents a character from the Oriya language.
UCNs can appear in identifiers, character literals, and string
literals. The intent was that editors would be created which could
display some or all of the Unicode characters that are not in the
basic C character set. Any UCN that corresponded to a character that
the editor knows how to display would be displayed as that character;
for any character that it could not display, the corresponding UCN
would be displayed as a UCN. I have no idea whether such editors
actually existed, I've never had a need to use a UCN.

....
I always thought that everything inside a c programs is saved as
characters and those characters are converted to ASCII equivalents during
compilation.
The encoding used for a C source code file is not required to be
ASCII. In general, characters from the input file do not get copied as
such to the output file, except when they occur inside a character
literal, a string literal, or an identifier with external linkage.
Even then, many such characters are transformed in various ways during
translation of the program. For instance, inside the string literal
"Ding\07!", the three characters '\', '0', and '7' will (generally)
cause a single byte with a value of 7 to be stored in the executable.
Escape sequences like '\n' and UCNs cause similar transformations to
occur. What finally gets written to the output file will use the
encoding for the execution character set, which is also not required
to be ASCII, and which might not be the same as the encoding used in
the source file.
Oct 17 '08 #23
"Bill Cunningham" <no****@nspam.invalidwrites:
"arnuld" <su*****@invalid.addresswrote in message
news:pa****************************@invalid.addres s...
>>
3) In the end, object file is converted into the machine readable file
containing 1s and 0s only.

The binary notation means there's about 2-3v of electricity "turned on"
for 1 and "off" for 0. The absence or presence of an electrical charge.
Maybe. Or it means that there's 2-3v for 0 and "off" for 1. Or a
particular piece of iron oxide is magnetized in one way or an other.
Or there's a pit or lack of pit on the aluminum surface of a CD or
DVD. Or there is or isn't a hole in a punch card, or paper tape, or
mylar tape.

Files are stored as sequnces of 1s and 0s, typically (but not
necessarily) organized into bytes, typically (but not necessarily)
organized into blocks. But none of these details are relevant, as
long as the 0s and 1s stay the same when they're supposed to, and
change the way they're supposed to.

(Well, they can certainly become relevant if you're doing very
low-level programming, but I don't think you're doing that kind of
thing -- or should be.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 18 '08 #24

"Tim Prince" <tp*****@nospamcomputer.orgwrote in message
news:ZS*****************@nlpi064.nbdc.sbc.com...
christian.bau wrote:
>On Oct 16, 11:47 pm, "Bill Cunningham" <nos...@nspam.invalidwrote:
>>Now with the function memset I could do the same thing and it would be
portable. But would it always work?

I haven't seen a system where it didn't work, and C99 with the latest
corrections guarantees that it works for all integer types. For every
integer type, all value bits zero means the value is zero. There was
the possibility if integers had padding bits that all zero padding
bits would be a trap representation; the latest C99 version guarantees
that (all bits zero) is always a valid representation of the value
zero.
For what it's worth, Intel C (and Fortran) are expanding the automatic
replacement of explicit 0 setting loops by memset(). Among other
implications, memset(), or an explicit loop eligible to be replaced by
memset(), are preferred over C++ equivalents.
The thing is that memset returns a void* and if I'm correct and could
very well not be, you can't really error check.

Bill
Oct 18 '08 #25
"Bill Cunningham" <no****@nspam.invalidwrites:
"Tim Prince" <tp*****@nospamcomputer.orgwrote in message
news:ZS*****************@nlpi064.nbdc.sbc.com...
[...]
>For what it's worth, Intel C (and Fortran) are expanding the automatic
replacement of explicit 0 setting loops by memset().
That's purely an implementation detail. Compilers are free to
generate whatever code they like (in this case, replacing an explicit
loop by a call to memset) as long as the effect is the same.
> Among other
implications, memset(), or an explicit loop eligible to be replaced by
memset(), are preferred over C++ equivalents.
There may be some C++ issues here that I won't get into; beyond that,
see above.
The thing is that memset returns a void* and if I'm correct and could
very well not be, you can't really error check.
Right. It's up to the caller (whether it's user-written code or
compiler-generated code) to get the arguments right. Assuming that's
done, there are no detectable errors.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 18 '08 #26
On Fri, 17 Oct 2008 11:39:09 +0500, arnuld <su*****@invalid.address>
wrote:
>On Thu, 16 Oct 2008 22:35:37 -0700, Barry Schwarz wrote:
>It may be 48 on your system. On mine it is 240.

I thought ASCII values are same for all platforms.
>Nothing on my system is converted to ASCII.

I don't get it. Why ASCII exists ?
ASCII happens to be one of several standards that are used to
represent characters internal to a computer. Not all systems use that
standard.
>
>On your system, what ASCII table number is involved in the statement
char x = 17;
>All character literals in your source file are already
in the form they need to be for the compiler. Unless the execution
character set is different than the source, what conversion do you
think is needed for
char y = 'a';


#include <stdio.h>
int main( void )
{
char x = 17;
char y = 'a';
char z = '0';
printf("x = %d\n", x);
printf("y = %d\n", y);
printf("z = %d\n", z);

return 0;
}
[arnuld@dune ztest]$ gcc4 -ansi -pedantic -Wall -Wextra test.c
[arnuld@dune ztest]$ ./a.out
x = 17
y = 97
z = 48
[arnuld@dune ztest]$
Those are the values that result from treating a character as a binary
value on an ASCII system that uses 8-bit bytes. That is not in
dispute. What is your point? Do you think that is the only type of
system that exists. If I compiled and executed your code on my
(EBCDIC) system, I would get 17, 129, and 240.

That is why there are frequent posts here that say do not use
if (x == 48) ...
which works only on your system but use
if (x == '0') ...
which will work on both our systems.

The same rational deals with
if (ch >= 'A' && ch <= 'Z') ...
compared with
if (isupper(ch))...
On an ASCII system, if the first evaluates true, ch must contain an
upper case letter. That is not the case on my system. The second
works correctly on both our systems.

On an ASCII system
if (isupper(ch)) ch += 0x20;
will convert ch to lower case. On my system, ch would become a
different letter, a number, or an unprintable/undisplayable control
character. But
if (isupper(ch)) ch = tolower(ch);
will work on both systems.
>
I always thought that everything inside a c programs is saved as
characters and those characters are converted to ASCII equivalents during
compilation.
Look at that sentence again. The compiler reads a (saved) source file
during compilation. The program is made up of characters. If your
system is an ASCII system, what standard was used to represent the
characters stored in the source file? Why do you think they are
converted to something else for the compiler?

--
Remove del for email
Oct 18 '08 #27
On Fri, 17 Oct 2008 11:39:09 +0500, arnuld <su*****@invalid.addresswrote:
>On Thu, 16 Oct 2008 22:35:37 -0700, Barry Schwarz wrote:
It may be 48 on your system. On mine it is 240.

I thought ASCII values are same for all platforms.
Yes, ASCII values are the same on all platforms that support the `ASCII'
standard and use it for the machine-specific representation of text.

The tricky part is that there are platforms out there that do not use
ASCII for representing text.

Oct 18 '08 #28

"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
Right. It's up to the caller (whether it's user-written code or
compiler-generated code) to get the arguments right. Assuming that's
done, there are no detectable errors.
I have also read that void* is the generic pointer. I am not quite sure
of this but the way I understand that is that somehow void* can also be a
pointer to any type so somehow one might be able to manipulate void* so that
it would be a char* or int* .

Bill
Oct 18 '08 #29
"Bill Cunningham" <no****@nspam.invalidwrites:
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
>Right. It's up to the caller (whether it's user-written code or
compiler-generated code) to get the arguments right. Assuming that's
done, there are no detectable errors.

I have also read that void* is the generic pointer. I am not quite sure
of this but the way I understand that is that somehow void* can also be a
pointer to any type so somehow one might be able to manipulate void* so that
it would be a char* or int* .
Yes, of course. void* is a generic pointer type; you can convert a
pointer of any other type (other than a pointer-to-function) to void*
and back again without loss of information. Such conversions are
usually done implicitly, i.e., there's no need for a cast.

This has all been discussed here many many times.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 19 '08 #30
On Sat, 18 Oct 2008 19:18:45 -0400, "Bill Cunningham" <no****@nspam.invalidwrote:
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
>Right. It's up to the caller (whether it's user-written code or
compiler-generated code) to get the arguments right. Assuming that's
done, there are no detectable errors.

I have also read that void* is the generic pointer. I am not quite
sure of this but the way I understand that is that somehow void* can
also be a pointer to any type so somehow one might be able to
manipulate void* so that it would be a char* or int* .
If by `manipulate' you mean `assign to', then `yes' for `char *', but
`not necessarily' for `int *' . The `n1256.pdf' copy that I have
locally says:

§6.2.5 (27)

A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type.[39] Similarly,
pointers to qualified or unqualified versions of compatible types
shall have the same representation and alignment requirements. All
pointers to structure types shall have the same representation and
alignment requirements as each other. All pointers to union types
shall have the same representation and alignment requirements as each
other. Pointers to other types need not have the same representation
or alignment requirements.

[39] The same representation and alignment requirements are meant to
imply interchangeability as arguments to functions, return values
from functions, and members of unions.

There are systems where `int *' pointers have stricter alignment
requirements from `char *' or `void *' pointers.

Oct 19 '08 #31
On Oct 19, 5:17 am, Giorgos Keramidas <keram...@ceid.upatras.gr>
wrote:
On Sat, 18 Oct 2008 19:18:45 -0400, "Bill Cunningham" <nos...@nspam.invalidwrote:
I have also read that void* is the generic pointer. I am not quite
sure of this but the way I understand that is that somehow void* can
also be a pointer to any type so somehow one might be able to
manipulate void* so that it would be a char* or int* .

If by `manipulate' you mean `assign to', then `yes' for `char *', but
`not necessarily' for `int *' . The `n1256.pdf' copy that I have
locally says:

§6.2.5 (27)

A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type.[39] Similarly,
pointers to qualified or unqualified versions of compatible types
shall have the same representation and alignment requirements. All
pointers to structure types shall have the same representation and
alignment requirements as each other. All pointers to union types
shall have the same representation and alignment requirements as each
other. Pointers to other types need not have the same representation
or alignment requirements.

[39] The same representation and alignment requirements are meant to
imply interchangeability as arguments to functions, return values
from functions, and members of unions.

There are systems where `int *' pointers have stricter alignment
requirements from `char *' or `void *' pointers.

That quote is for representations. If I understood correctly you used
that quote to support that the following is not valid?

void *p;
int *q;
/* ... */
p = q;
q = p;

In that case, you're wrong. It merely talks about representations.
Oct 19 '08 #32
"Bill Cunningham" <no****@nspam.invalidwrote in message
>
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
>Right. It's up to the caller (whether it's user-written code or
compiler-generated code) to get the arguments right. Assuming that's
done, there are no detectable errors.

I have also read that void* is the generic pointer. I am not quite sure
of this but the way I understand that is that somehow void* can also be a
pointer to any type so somehow one might be able to manipulate void* so
that
it would be a char* or int* .
You can't manipulate a void *. You can only store another pointer into it,
or retrieve the value from it.
The value you retrieve must be of the same type as you stored, so if you
store an int * int a void *, you must assign the void * to an int * before
using it, and so on.

(The only exception is that you may assign a void * to an unsigned char * to
treat the object pointed to as a series of bytes, this is how memcpy() and
the like work).

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Oct 19 '08 #33

"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
Yes, of course. void* is a generic pointer type; you can convert a
pointer of any other type (other than a pointer-to-function) to void*
and back again without loss of information. Such conversions are
usually done implicitly, i.e., there's no need for a cast.

This has all been discussed here many many times.
So does this mean I can get a return value somehow using memset ?

Bill
Oct 19 '08 #34
Bill Cunningham wrote:
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
>Yes, of course. void* is a generic pointer type; you can convert a
pointer of any other type (other than a pointer-to-function) to void*
and back again without loss of information. Such conversions are
usually done implicitly, i.e., there's no need for a cast.

This has all been discussed here many many times.

So does this mean I can get a return value somehow using memset ?
Don't be daft (or obtuse?) read the declaration of memset, there's
nothing magical it its return value.

--
Ian Collins
Oct 19 '08 #35
"Bill Cunningham" <no****@nspam.invalidwrites:
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
>Yes, of course. void* is a generic pointer type; you can convert a
pointer of any other type (other than a pointer-to-function) to void*
and back again without loss of information. Such conversions are
usually done implicitly, i.e., there's no need for a cast.

This has all been discussed here many many times.

So does this mean I can get a return value somehow using memset ?
The documentation for memset will answer this question for you.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 19 '08 #36
On 17 Oct, 00:43, "Bill Cunningham" <nos...@nspam.invalidwrote:
"Ian Collins" <ian-n...@hotmail.comwrote in message
news:6l*************@mid.individual.net...
If that's the case, what is \0?

* * It's supposed to mean null in addition to the string terminator.
but it doesn't. The nul *character* is spelt like this '\0'
(the single quotes are significant). \0 (without single quotes) is
meaningless
--
Nick Keighley
Oct 20 '08 #37
On 16 Oct, 23:47, "Bill Cunningham" <nos...@nspam.invalidwrote:
* * When I want to clear memory space this is what I typically do myself,

char a[100];
int i;
for (i=0;i != 100;++i)
a[i]='\0';

Now with the function memset I could do the same thing and it would be
portable. But would it always work?
how could it be "portable" and "not work"? What do you mean by these
words?

memset( a, '\0', sizeof a);

--
Nick Keighley
Oct 20 '08 #38
Nick Keighley <ni******************@hotmail.comwrites:
On 17 Oct, 00:43, "Bill Cunningham" <nos...@nspam.invalidwrote:
>"Ian Collins" <ian-n...@hotmail.comwrote in message
news:6l*************@mid.individual.net...

>If that's the case, what is \0?

Â* Â* It's supposed to mean null in addition to the string terminator.

but it doesn't. The nul *character* is spelt like this '\0'
Nit (and it is a small one): The standard (correctly) calls it "the
null character". It has an abbreviated three-letter ASCII name, NUL,
but that is not how to refer to it in text. (Just as one would write,
in English, "the escape character" rather than "the esc character".)

--
Ben.
Oct 20 '08 #39

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Aing | last post by:
Anyone knows what can be cause of this problem? //////////////////////////////////////////////////////////// typedef struct _date_struct { int date,month,year; }date_struct; Class Date {...
12
by: Sacha Schär | last post by:
Suppose I have an array of Foo's: class Foo { public: int SomeMethode(void); int i; char c; char * p;
6
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the...
26
by: 69dbb24b2db3daad932c457cccfd6 | last post by:
Hello, I have to initialize all elements of a very big float point array to zero. It seems memset(a, 0, len) is faster than a simple loop. I just want to know whether it is safe to do so, since I...
22
by: srivatsan_b | last post by:
Hi, Can somebody explain whether an explicit typecast is mandatory while calling memset function for a structure? like in the following code snapshot..... struct some_structure x;...
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
27
by: volunteers | last post by:
I met a question about memset and have no idea right now. Could anybody give a clue? Thanks memset is sometimes used to initialize data in a constructor like the example below. What is the...
18
by: dykeinthebox | last post by:
Consider the following program: #include <stdlib.h> #include <string.h> int main( void ) { void *p = malloc( 4 ); if ( p ) {
18
by: Gaijinco | last post by:
I'm having a headache using memset() Given: int v; memset((void*)v, 1, sizeof(v)); Can I be 100% positive than v = 1 for i 0, or there is something else I have to do?.
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.