Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

const allocation 1

Status
Not open for further replies.

hughLg

Programmer
Feb 18, 2002
136
MY
how's the constants declared with keyword const is allocated in the program? if i declared a constant as a global, when the memory is allocated? when the program start running? i think so.

but how about the constant in function? when the memory is allocated for it? it's allocated when the program start running like global or when the execution reach inside that funtion?

i want to know this for static object too. the static variable is allocated (definition) for 1 time only, but what's the time it occurs?

and this apply also to the char point declared like:

Code:
{
   char *s = "hello world...";
}

the location pointed to by s is a constant memory location or...?
 
how's the constants declared with keyword const is allocated in the program?

Ans: Same as any other variable.


if i declared a constant as a global, when the memory is allocated? when the program start running? i think so.

Ans: yes - depends on the OS. If it is in PROM, it is already allocated before the program starts.

but how about the constant in function? when the memory is allocated for it? it's allocated when the program start running like global or when the execution reach inside that funtion?

Ans: Allocated inside the function on the stack so don't point to it globally otherwise your pointer might be out of scope.

i want to know this for static object too. the static variable is allocated (definition) for 1 time only, but what's the time it occurs?

Ans: When the program starts. Also statics are local to the module describing it. Unfortunately static in C has 2 meanings: once only and not public.

and this apply also to the char point declared like:

{
char *s = "hello world...";
}

the location pointed to by s is a constant memory location or...?

Ans: yes it is to a constant memory location. Actually it should be

const char* s = "hello world...";

because "hello world..." is a constant.
 
what's the difference between

Code:
{
   static const char *s = "Hello world...";
   /* and */
   const char *s = "Hello world...";
}

i want to know which method is better for memory usage. i want to create a clean program, that's mean the program that can released all the use of memory after the program is exit.
 
Both of your examples are "clean." Neither dynamically allocates memory and fails to free it.

Even if you dynamically allocated something and should have freed it on your own, the program does it for you when it exits, that's just not considered "clean."

As for the difference between declaring s static and non-static, it depends on where/how you use it. I'll assume you're wondering about using it in a function.

Say you have the following code:

[tt]void printHello();

int main( void )
{
for ( int i = 0; i < 10; ++i )
printHello();

return 0;
}

void printHello()
{
const char *s = &quot;Hello;

printf( &quot;%s\n&quot;, s );
}
[/tt]

Theoretically, here's what happens: &quot;Hello&quot; is allocated once at the beginning of the program and stored somewhere as a constant. s gets created 10 times (each time the function is called), and each time is made to point to the constant &quot;Hello&quot;.

If s had been declared static instead, &quot;Hello&quot; would get allocated once at the beginning of the program, and s would be created once, also at the beginning of the program, and made to point to the constant &quot;Hello&quot;.

Looking at it this way, it seems that static would be more efficient since s would only have to be created and initialized once.

However, in reality, the compiler could probably pull some tricks and optimize away s anyway, so it more than likely doesn't matter either way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top