|
|
|
|
|
|
| Author |
Message |
humanmutantman@yahoo.com *nix forums beginner
Joined: 05 Jul 2006
Posts: 11
|
Posted: Tue Jul 18, 2006 9:13 pm Post subject:
How to find memory content at location x?
|
|
|
I'm reading a book that lists the example:
char month[10] = "January";
Memory for this looks something like the following:
| J | a | n | u | a | r | y | \0 | ? | ? |
Q1: How can I find out what is in the two memory locations following
the string terminator?
Q2: Does gcc zero out unused array elements?
Q3: If yes to Q2, where would I find that information?
Thanks! |
|
| Back to top |
|
 |
Ben Pfaff *nix forums Guru
Joined: 08 Apr 2005
Posts: 661
|
Posted: Tue Jul 18, 2006 9:20 pm Post subject:
Re: How to find memory content at location x?
|
|
|
humanmutantman@yahoo.com writes:
| Quote: | I'm reading a book that lists the example:
char month[10] = "January";
Memory for this looks something like the following:
| J | a | n | u | a | r | y | \0 | ? | ? |
Q1: How can I find out what is in the two memory locations following
the string terminator?
|
They're set to 0.
| Quote: | Q2: Does gcc zero out unused array elements?
|
Yes. The language standard requires it.
| Quote: | Q3: If yes to Q2, where would I find that information?
|
C99 6.7.8, para 21:
If there are fewer initializers in a brace-enclosed list
than there are elements or members of an aggregate, or fewer
characters in a string literal used to initialize an array
of known size than there are elements in the array, the
remainder of the aggregate shall be initialized implicitly
the same as objects that have static storage duration.
--
"I don't have C&V for that handy, but I've got Dan Pop."
--E. Gibbons |
|
| Back to top |
|
 |
Walter Roberson *nix forums Guru
Joined: 19 Feb 2005
Posts: 1300
|
Posted: Tue Jul 18, 2006 9:39 pm Post subject:
Re: How to find memory content at location x?
|
|
|
In article <87ejwiobjh.fsf@benpfaff.org>,
Ben Pfaff <blp@cs.stanford.edu> wrote:
| Quote: | C99 6.7.8, para 21:
If there are fewer initializers in a brace-enclosed list
than there are elements or members of an aggregate, or fewer
characters in a string literal used to initialize an array
of known size than there are elements in the array, the
remainder of the aggregate shall be initialized implicitly
the same as objects that have static storage duration.
|
The C89 wording does not have the phrase about characters
in a string literal, but the "intended" behaviour can be deduced
by the example saying that initialization with a string
"is identical to" initialization with the brace-enclosed list
of characters, together with the explicit phrasing about fewer
initializers.
But on the surface, the C89 wording would appear to allow for a
difference between
char month[10] = { "January" };
and
char month[10] = "January";
in that the former is certainly a "brace enclosed list" but the later
is not -- the C89 part about the braces being optional for string
literals does not preclude the possibility of different behaviours
when the braces are or are not present.
--
All is vanity. -- Ecclesiastes |
|
| Back to top |
|
 |
Morris Dovey *nix forums addict
Joined: 22 Jun 2005
Posts: 90
|
Posted: Tue Jul 18, 2006 9:42 pm Post subject:
OT Query
|
|
|
Ben Pfaff (in 87ejwiobjh.fsf@benpfaff.org) said:
| "I don't have C&V for that handy, but I've got Dan Pop."
| --E. Gibbons
Speaking of whom, where is Dan these days?
And while I'm asking OT questions, are you still at the same west
coast institution and do you still find time to make steel sing and
dance?
--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto |
|
| Back to top |
|
 |
humanmutantman@yahoo.com *nix forums beginner
Joined: 05 Jul 2006
Posts: 11
|
Posted: Tue Jul 18, 2006 10:14 pm Post subject:
Re: How to find memory content at location x?
|
|
|
humanmutantman@yahoo.com wrote:
| Quote: | I'm reading a book that lists the example:
char month[10] = "January";
Memory for this looks something like the following:
| J | a | n | u | a | r | y | \0 | ? | ? |
Q1: How can I find out what is in the two memory locations following
the string terminator?
Q2: Does gcc zero out unused array elements?
Q3: If yes to Q2, where would I find that information?
|
Thanks for the answers. Now for two more related questions. I put in a
for loop to print the cell contents:
printf("Month: %s\n", month);
for (cell=0;cell<10;cell++)
{
printf("Cell %d: %c\n",cell+1,month[cell]);
}
Q4: How can I get the hex contents of each cell?
Q5: How can I format "printf("Cell %d: %c\n",cell+1,month[cell]);" so
I can get:
....
Cell 9 ... // Note: There are two spaces before the '9'.
Cell 10 ... // Note: There is one space before the "10".
Thanks! |
|
| Back to top |
|
 |
Walter Roberson *nix forums Guru
Joined: 19 Feb 2005
Posts: 1300
|
Posted: Tue Jul 18, 2006 10:50 pm Post subject:
Re: How to find memory content at location x?
|
|
|
In article <1153260844.873991.264960@p79g2000cwp.googlegroups.com>,
<humanmutantman@yahoo.com> wrote:
| Quote: | I put in a
for loop to print the cell contents:
printf("Month: %s\n", month);
for (cell=0;cell<10;cell++)
{
printf("Cell %d: %c\n",cell+1,month[cell]);
}
Q4: How can I get the hex contents of each cell?
|
Each cell only has value contents, and values are abstract. But you
can ask to have those values represented in hex:
printf("Cell %d: %c(%x)\n", cell+1,month[cell],(unsigned int)month[cell] );
| Quote: | Q5: How can I format "printf("Cell %d: %c\n",cell+1,month[cell]);" so
I can get:
Cell 9 ... // Note: There are two spaces before the '9'.
Cell 10 ... // Note: There is one space before the "10".
|
%2d instead of %d
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell |
|
| Back to top |
|
 |
humanmutantman@yahoo.com *nix forums beginner
Joined: 05 Jul 2006
Posts: 11
|
Posted: Tue Jul 18, 2006 11:06 pm Post subject:
Re: How to find memory content at location x?
|
|
|
| Quote: | I put in a
for loop to print the cell contents:
printf("Month: %s\n", month);
for (cell=0;cell<10;cell++)
{
printf("Cell %d: %c\n",cell+1,month[cell]);
}
Q4: How can I get the hex contents of each cell?
Each cell only has value contents, and values are abstract. But you
can ask to have those values represented in hex:
printf("Cell %d: %c(%x)\n", cell+1,month[cell],(unsigned int)month[cell] );
Q5: How can I format "printf("Cell %d: %c\n",cell+1,month[cell]);" so
I can get:
Cell 9 ... // Note: There are two spaces before the '9'.
Cell 10 ... // Note: There is one space before the "10".
%2d instead of %d
|
That was pretty cool. Thanks! |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Thu Jan 08, 2009 6:34 am | All times are GMT
|
|
Buy Anything On eBay | Problem Mortgage | eHarmony Coupon | Remortgages | Internet Advertising
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|