T
Thomas Matthews
Hi,
I've found with my compiler, that when a function contains const
data in a function, that data is first copied to the stack[1].
Given:
/* Example 1 */
char Buffer[1024];
void My_Func(void)
{
const char TEXT[] = {'H', 'e', 'l', 'l', 'o', '\0'};
memcpy(Buffer, TEXT, sizeof(TEXT));
return;
}
My compiler copies the data in TEXT to some memory location,
then passes a pointer to that location to the memcpy routine.
If I move the declaration to outside of the function:
/* Example 2 */
const char TEXT[] = {'H', 'e', 'l', 'l', 'o', '\0'};
void My_Func(void)
{
memcpy(Buffer, TEXT, sizeof(TEXT));
return;
}
or label it as static (and keep it in the function):
/* Example 3 */
void My_Func(void)
{
static const char TEXT[] = {'H', 'e', 'l', 'l', 'o', '\0'};
memcpy(Buffer, TEXT, sizeof(TEXT));
return;
}
then the compiler passes a pointer to the text to memcpy,
without making an interim copy.
Here are my questions:
Q1: Is the interim duplication mandated by the C language
specification or is this a problem with my compiler?
Q2: Is the _static_ modifier supposed to have this effect?
Q3: My understanding is that constant data is placed into
a constant area and not copied. Am I wrong?
I'm working on an embedded system and I don't want a duplicate
copy of the data created. In our system, we've replaced
memcpy with memcpy_byte (our proprietary function) and we don't
want the library version invoked, which is what the compiler
does in Example 2.
------
[1] Yes, I know that the standard doesn't require implementations
to have a stack, call it read/write memory if "stack" bothers
you.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
I've found with my compiler, that when a function contains const
data in a function, that data is first copied to the stack[1].
Given:
/* Example 1 */
char Buffer[1024];
void My_Func(void)
{
const char TEXT[] = {'H', 'e', 'l', 'l', 'o', '\0'};
memcpy(Buffer, TEXT, sizeof(TEXT));
return;
}
My compiler copies the data in TEXT to some memory location,
then passes a pointer to that location to the memcpy routine.
If I move the declaration to outside of the function:
/* Example 2 */
const char TEXT[] = {'H', 'e', 'l', 'l', 'o', '\0'};
void My_Func(void)
{
memcpy(Buffer, TEXT, sizeof(TEXT));
return;
}
or label it as static (and keep it in the function):
/* Example 3 */
void My_Func(void)
{
static const char TEXT[] = {'H', 'e', 'l', 'l', 'o', '\0'};
memcpy(Buffer, TEXT, sizeof(TEXT));
return;
}
then the compiler passes a pointer to the text to memcpy,
without making an interim copy.
Here are my questions:
Q1: Is the interim duplication mandated by the C language
specification or is this a problem with my compiler?
Q2: Is the _static_ modifier supposed to have this effect?
Q3: My understanding is that constant data is placed into
a constant area and not copied. Am I wrong?
I'm working on an embedded system and I don't want a duplicate
copy of the data created. In our system, we've replaced
memcpy with memcpy_byte (our proprietary function) and we don't
want the library version invoked, which is what the compiler
does in Example 2.
------
[1] Yes, I know that the standard doesn't require implementations
to have a stack, call it read/write memory if "stack" bothers
you.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book