473,396 Members | 1,990 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,396 software developers and data experts.

pointer concept

Hi All

char *p="str";

How much memory will be allocated for this.

Oct 29 '07 #1
22 1308
gsingh said:
Hi All

char *p="str";

How much memory will be allocated for this.
At least four bytes for the string literal, and sizeof p bytes for the
pointer.

--
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 29 '07 #2
jk
I believe that there will be 4 bytes ('s', 't', 'r', '\0') allocated
for the string, and sizeof(char*) for the pointer (that will vary on
different systems) (the size of the pointer, though, has nothing to do
with where.or how much space the thing it is pointing to is/takes)

cheers

On Oct 29, 1:34 am, gsingh <gs.alca...@gmail.comwrote:
Hi All

char *p="str";

How much memory will be allocated for this.

Oct 29 '07 #3
Richard Heathfield wrote:
gsingh said:
>Hi All

char *p="str";

How much memory will be allocated for this.

At least four bytes for the string literal, and sizeof p bytes for the
pointer.
Why "at least" ? Alignment constraints?
Does sizeof include any byte-count required because of alignment?

- Larry
Oct 29 '07 #4
Larry__Weiss said:
Richard Heathfield wrote:
>gsingh said:
>>Hi All

char *p="str";

How much memory will be allocated for this.

At least four bytes for the string literal, and sizeof p bytes for the
pointer.

Why "at least" ?
Because I couldn't remember whether the Standard required the
implementation to allocate no more memory for string literals than they
actually need, so "at least" was a safe answer.
Alignment constraints?
I'd have thought that unlikely in this case, but yes, perhaps.
Does sizeof include any byte-count required because of alignment?
No, sizeof yields the exact size of its operand in bytes. (For structs,
this can include some padding bytes inserted for alignment reasons.)

--
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 29 '07 #5
Richard Heathfield <rj*@see.sig.invalidwrites:
Larry__Weiss said:
>Richard Heathfield wrote:
>>gsingh said:
char *p="str";

How much memory will be allocated for this.

At least four bytes for the string literal, and sizeof p bytes for the
pointer.

Why "at least" ?

Because I couldn't remember whether the Standard required the
implementation to allocate no more memory for string literals than they
actually need, so "at least" was a safe answer.
It depends on what you mean by "allocated". The size of the string
literal itself (more precisely, of the static array object specified
by the string literal) is exactly 4 bytes. The compiler might reserve
some additional space before or after it in memory, for whatever
reason it likes, or for no reason at all. I'd argue that any such
space is not allocated *for* the string literal, but the standard
doesn't mention this and it's not a particularly meaningful question.

[snip]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 29 '07 #6
jk wrote:
I believe that there will be 4 bytes ('s', 't', 'r', '\0') allocated

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
Oct 29 '07 #7
gsingh <gs********@gmail.comwrites:
char *p="str";

How much memory will be allocated for this.
Such questions don't have simple answers. If the compiler can detect
that 'p' is not required, it may not actually allocate any memory at
all. It is even possible (depending on the context) that the compiler
might eliminate the string literal!

You can say that 'p' will require 'sizeof p' bytes to hold its value,
but even that does not say much about allocated memory. The
implementation may have to align the variable 'p' in some particular
way.

--
Ben
Oct 29 '07 #8
In article <L4*********************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>Because I couldn't remember whether the Standard required the
implementation to allocate no more memory for string literals than they
actually need, so "at least" was a safe answer.
How could you possibly tell? (So long as sizeof returns the expected
answer.)

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Oct 29 '07 #9
Richard Tobin said:
In article <L4*********************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>>Because I couldn't remember whether the Standard required the
implementation to allocate no more memory for string literals than they
actually need, so "at least" was a safe answer.

How could you possibly tell? (So long as sizeof returns the expected
answer.)
I couldn't. And here's another question, while we're in the mood: how could
I possibly care? :-)

--
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 29 '07 #10
Richard Heathfield wrote:
Richard Tobin said:
>In article <L4*********************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>>Because I couldn't remember whether the Standard required the
implementation to allocate no more memory for string literals than they
actually need, so "at least" was a safe answer.
How could you possibly tell? (So long as sizeof returns the expected
answer.)

I couldn't. And here's another question, while we're in the mood: how could
I possibly care? :-)
Well, in the extreme case you might be a bit miffed if a 3-character
(plus null terminator) string literal used all available memory to store
itself!
Oct 29 '07 #11
Larry__Weiss wrote:
Richard Heathfield wrote:
>gsingh said:
>>Hi All

char *p="str";

How much memory will be allocated for this.

At least four bytes for the string literal, and sizeof p bytes for the
pointer.

Why "at least" ? Alignment constraints?
Does sizeof include any byte-count required because of alignment?
It depends on what your definition of "for" is.

The implementation is always permitted to use more
memory, more registers, more CPU time, more pixie dust
than you might think is "necessary." (It usually has a
reason that the implementors thought was a good one.)
The nameless array created for the string literal above
is four bytes long, but it might be accompanied by more
memory that is not part of the array. For example, the
DECC compiler on Alpha stuffed short literals into eight-
byte slots apart from the memory used for longer strings;
eight-byte accesses were particularly favored on Alpha.

However, any such extra memory is not part of the
anonymous array object. sizeof("str") is four, exactly,
even if those four bytes wallow in the middle of a forty-
megabyte sea of wasted memory. So the question of how many
bytes are allocated for the literal depends on whether your
idea of "for" means "as a part of" or "on account of," with
the answers "four" and "at least four," respectively.

sizeof(anyThingOrType) includes any padding that the
anyThingOrType needs to ensure its own proper alignment,
but does not include uncalled-for overhead.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Oct 29 '07 #12
Philip Potter said:
Richard Heathfield wrote:
>Richard Tobin said:
>>In article <L4*********************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:

Because I couldn't remember whether the Standard required the
implementation to allocate no more memory for string literals than
they actually need, so "at least" was a safe answer.
How could you possibly tell? (So long as sizeof returns the expected
answer.)

I couldn't. And here's another question, while we're in the mood: how
could I possibly care? :-)

Well, in the extreme case you might be a bit miffed if a 3-character
(plus null terminator) string literal used all available memory to store
itself!
Oh, I dunno... it might be worth it, just to have a fresh addition to my
store of "horrible warning" anecdotes.

--
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 29 '07 #13
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <L4*********************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>>Because I couldn't remember whether the Standard required the
implementation to allocate no more memory for string literals than they
actually need, so "at least" was a safe answer.

How could you possibly tell? (So long as sizeof returns the expected
answer.)
By examining the generated code.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 29 '07 #14
Richard Heathfield wrote:
gsingh said:
>char *p="str";

How much memory will be allocated for this.

At least four bytes for the string literal, and sizeof p bytes
for the pointer.
The literal may not be needed. For example:

char *o = "First str";
char *p = "str";

in which *p may well point to the "str" portion of "First str".

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Oct 29 '07 #15
gsingh wrote:
>
char *p="str";

How much memory will be allocated for this.
sizeof p

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 29 '07 #16
On Oct 30, 7:29 am, Keith Thompson <ks...@mib.orgwrote:
rich...@cogsci.ed.ac.uk (Richard Tobin) writes:
Richard Heathfield <r...@see.sig.invalidwrote:
>Because I couldn't remember whether the Standard required the
implementation to allocate no more memory for string literals than they
actually need, so "at least" was a safe answer.
How could you possibly tell? (So long as sizeof returns the expected
answer.)

By examining the generated code.
There might not be any generated code, depending on
which optimisations the compiler is able to make.

Oct 29 '07 #17
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>>>Because I couldn't remember whether the Standard required the
implementation to allocate no more memory for string literals than they
actually need, so "at least" was a safe answer.
>How could you possibly tell? (So long as sizeof returns the expected
answer.)
>By examining the generated code.
Well yes, but by "you" I mean a conforming program, which is the
arbiter of standardness (by the "as if" rule).

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Oct 29 '07 #18
Old Wolf <ol*****@inspire.net.nzwrites:
On Oct 30, 7:29 am, Keith Thompson <ks...@mib.orgwrote:
>rich...@cogsci.ed.ac.uk (Richard Tobin) writes:
Richard Heathfield <r...@see.sig.invalidwrote:
Because I couldn't remember whether the Standard required the
implementation to allocate no more memory for string literals than they
actually need, so "at least" was a safe answer.
How could you possibly tell? (So long as sizeof returns the expected
answer.)

By examining the generated code.

There might not be any generated code, depending on
which optimisations the compiler is able to make.
If there's no generated code, then the amount of allocated space is
zero (though it's still the same in the abstract machine).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 29 '07 #19
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>>>>Because I couldn't remember whether the Standard required the
implementation to allocate no more memory for string literals than they
actually need, so "at least" was a safe answer.
>>How could you possibly tell? (So long as sizeof returns the expected
answer.)
>>By examining the generated code.

Well yes, but by "you" I mean a conforming program, which is the
arbiter of standardness (by the "as if" rule).
A conforming program can examine its own machine code. (Remember the
extremely weak defintion of "conforming program".)

If you're thinking of strictly conforming programs, the standard
doesn't limit itself to those. Correct programs must still work
correctly.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 29 '07 #20
On Oct 29, 1:38 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
gsingh <gs.alca...@gmail.comwrites:
char *p="str";
How much memory will be allocated for this.

Such questions don't have simple answers. If the compiler can detect
that 'p' is not required, it may not actually allocate any memory at
all. It is even possible (depending on the context) that the compiler
might eliminate the string literal!
Interesting way of thinking :):)

Karthik Balaguru

Oct 30 '07 #21
Keith Thompson said:
Old Wolf <ol*****@inspire.net.nzwrites:
>On Oct 30, 7:29 am, Keith Thompson <ks...@mib.orgwrote:
>>rich...@cogsci.ed.ac.uk (Richard Tobin) writes:
<snip>
>>>
How could you possibly tell? (So long as sizeof returns the expected
answer.)

By examining the generated code.

There might not be any generated code, depending on
which optimisations the compiler is able to make.

If there's no generated code, then the amount of allocated space is
zero (though it's still the same in the abstract machine).
C can be interpreted. Interpreters do not (necessarily) generate code in
the sense of an object file that you can examine. So do C interpreters run
their programs in 0 memory?

--
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 30 '07 #22
CBFalconer said:
Richard Heathfield wrote:
>gsingh said:
>>char *p="str";

How much memory will be allocated for this.

At least four bytes for the string literal, and sizeof p bytes
for the pointer.

The literal may not be needed. For example:

char *o = "First str";
char *p = "str";

in which *p may well point to the "str" portion of "First str".
That's actually a good example of the string literal needing more than four
bytes.

--
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 30 '07 #23

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

Similar topics

1
by: ypjofficial | last post by:
Dear All, According to OOPs , a base class pointer can to point to derived class object....call this as fact1 But somehow I am not comfortable while understanding this concept. The explanaition...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
5
by: Cancerbero | last post by:
Hi (first, excuse me for my bad english) As I know, the semantics for typedef is: typedef A B; I think this makes B a synonym of A, where A is an existing data type. Is that right? Based...
22
by: Neo | last post by:
Hi Folks, #include<stdio.h> int main() { int (*p); int arr; int i;
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
22
by: nick | last post by:
i do not know what is the use of (e.g. void *pt), when will use it. thanks!
2
by: yogesh basediya | last post by:
i want to full detail of pointer concept. means how can i access pointer in my programs? why pointer is always consume 2 bytes? and what is the main concept behind the double pointer?
3
by: ali | last post by:
Hi, When I pass a pointer as an argument of a method, is it safe if I change the data pointed by the argument and return it upon completion ? For example: Object* someFunction(Object* ob)...
13
by: Phil Bouchard | last post by:
I am currently writting a smart pointer which is reasonnably stable and I decided supporting allocators for completion because of its increase in efficiency when the same pool used by containers is...
34
by: Davy | last post by:
Hi all, I am writing a function, which return the pointer of the int. But it seems to be wrong. Any suggestion? int * get_p_t(int t) { return &t; } int main()
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.