G
gsingh
Hi All
char *p="str";
How much memory will be allocated for this.
char *p="str";
How much memory will be allocated for this.
Hi All
char *p="str";
How much memory will be allocated for this.
Richard said:gsingh said:
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?
Richard Heathfield said:Larry__Weiss said:
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.
jk said:I believe that there will be 4 bytes ('s', 't', 'r', '\0') allocated
gsingh said:char *p="str";
How much memory will be allocated for this.
Richard Heathfield said: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 said:Richard Tobin said:
I couldn't. And here's another question, while we're in the mood: how could
I possibly care?
Larry__Weiss said:Why "at least" ? Alignment constraints?
Does sizeof include any byte-count required because of alignment?
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!
How could you possibly tell? (So long as sizeof returns the expected
answer.)
gsingh said:char *p="str";
How much memory will be allocated for this.
Richard said:gsingh said:
At least four bytes for the string literal, and sizeof p bytes
for the pointer.
By examining the generated code.
[/QUOTE]How could you possibly tell? (So long as sizeof returns the expected
answer.)
By examining the generated code.
Old Wolf said:There might not be any generated code, depending on
which optimisations the compiler is able to make.
Well yes, but by "you" I mean a conforming program, which is the
arbiter of standardness (by the "as if" rule).
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.