niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C
How to find memory content at location x?
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
Author Message
humanmutantman@yahoo.com
*nix forums beginner


Joined: 05 Jul 2006
Posts: 11

PostPosted: Tue Jul 18, 2006 9:13 pm    Post subject: How to find memory content at location x? Reply with 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!
Back to top
Ben Pfaff
*nix forums Guru


Joined: 08 Apr 2005
Posts: 661

PostPosted: Tue Jul 18, 2006 9:20 pm    Post subject: Re: How to find memory content at location x? Reply with quote

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

PostPosted: Tue Jul 18, 2006 9:39 pm    Post subject: Re: How to find memory content at location x? Reply with quote

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

PostPosted: Tue Jul 18, 2006 9:42 pm    Post subject: OT Query Reply with quote

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

PostPosted: Tue Jul 18, 2006 10:14 pm    Post subject: Re: How to find memory content at location x? Reply with quote

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

PostPosted: Tue Jul 18, 2006 10:50 pm    Post subject: Re: How to find memory content at location x? Reply with quote

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

PostPosted: Tue Jul 18, 2006 11:06 pm    Post subject: Re: How to find memory content at location x? Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 6:34 am | All times are GMT
navigation Forum index » Programming » C
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts postfix out of memory error - please help metind Postfix 0 Mon Sep 11, 2006 1:54 am
No new posts Move Oracle 10g database to another location Selt Server 0 Fri Jul 21, 2006 2:14 pm
No new posts Non IBM memory in p630 Ron AIX 0 Fri Jul 21, 2006 2:05 pm
No new posts database Share Memory Limit (2 GB ) in a Instance is tota... sadanjan@gmail.com IBM DB2 0 Fri Jul 21, 2006 12:57 pm
No new posts Where is this code not freeing memory ? jithoosin C++ 2 Fri Jul 21, 2006 9:39 am

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
[ Time: 0.1817s ][ Queries: 16 (0.0884s) ][ GZIP on - Debug on ]