char*

R

RedLars

This defines a local variable placed on the stack which will be
cleaned up when it goes out of scope, right?
{
char czText[] = "Hello world";
// code

} // out of scope

What about this definition?
{
const char * czText2 = "Hello world";
// code

}
Do I need to clean up this variable by using free() ? Thought I read
somewhere that free should only be used on variables that have been
malloc'ed which isnt the case here.

Appreciate any input.
 
R

Richard Bos

RedLars said:
This defines a local variable placed on the stack which will be
cleaned up when it goes out of scope, right?

You don't know that. All you know is that it _can_ be cleaned up when it
goes out of scope, not that it is.
{
char czText[] = "Hello world";
// code

} // out of scope

What about this definition?
{
const char * czText2 = "Hello world";
// code

}

Subtle difference. cz[urgh]Text2 itself goes out of scope and may, not
must, be cleaned up or removed. The string it points at is a string
literal, which has no scope, but which does have static duration. This
means that all pointers you made point anywhere within that string
literal remain valid as long as _they_ are in scope (and still point
within that string literal, of course).
Do I need to clean up this variable by using free() ?

Definitely not.
Thought I read somewhere that free should only be used on variables that
have been malloc'ed which isnt the case here.

That is correct. It should also be used on calloc()ed and realloc()ed
areas of memory. (If you use realloc(), take care to free() that memory
only once!)

Richard
 
R

RedLars

Thanks for the reply

You don't know that. All you know is that it _can_ be cleaned up when it
goes out of scope, not that it is.

Given the method below, say for some strange reason this was called
every minute within an application, would this cause a memory leak?
void foo()
{
int x = 2;
char y[] = "hello world";
char * z = "Bye bye";
}
{
char czText[] = "Hello world";
// code
} // out of scope
What about this definition?
{
const char * czText2 = "Hello world";
// code

Subtle difference. cz[urgh]Text2 itself goes out of scope and may, not
must, be cleaned up or removed. The string it points at is a string
literal, which has no scope, but which does have static duration. This
means that all pointers you made point anywhere within that string
literal remain valid as long as _they_ are in scope (and still point
within that string literal, of course).

So the string literal "Hello world" would continue to live throughout
the lifetime of the application then? Shouldn't I do something about
that? What can I do about it?
 
J

Joachim Schmitz

RedLars said:
Thanks for the reply

You don't know that. All you know is that it _can_ be cleaned up when it
goes out of scope, not that it is.

Given the method below, say for some strange reason this was called
every minute within an application, would this cause a memory leak?
void foo()
{
int x = 2;
char y[] = "hello world";
char * z = "Bye bye";
}
No, it won't leak memory
{
char czText[] = "Hello world";
// code
} // out of scope
What about this definition?
{
const char * czText2 = "Hello world";
// code

Subtle difference. cz[urgh]Text2 itself goes out of scope and may, not
must, be cleaned up or removed. The string it points at is a string
literal, which has no scope, but which does have static duration. This
means that all pointers you made point anywhere within that string
literal remain valid as long as _they_ are in scope (and still point
within that string literal, of course).

So the string literal "Hello world" would continue to live throughout
the lifetime of the application then? Yes

Shouldn't I do something about that?
No need
What can I do about it?
you could use:
const char czText2[] = "Hello world";
But it won't buy you much.

Bye, Jojo
 
R

RedLars

Thanks for the reply
Given the method below, say for some strange reason this was called
every minute within an application, would this cause a memory leak?
void foo()
{
int x = 2;
char y[] = "hello world";
char * z = "Bye bye";
}

No, it won't leak memory

Wouldn't running foo() multiple time create multipe string literal
"Bye bye" that are in fact not reference by any code, hence not used.
So the literal is of no use once the method is finsihed yet continue
to exist. So running foo() 1000 time would create 1000 string literal
in memory. This seem sort of pointless.

Sorry about the silly example I'm using atm.
 
J

Joachim Schmitz

RedLars said:
Newsbeitrag

Thanks for the reply
On 27 Sep, 08:34, (e-mail address removed) (Richard Bos) wrote:
This defines a local variable placed on the stack which will be
cleaned up when it goes out of scope, right?
You don't know that. All you know is that it _can_ be cleaned up when
it
goes out of scope, not that it is.
Given the method below, say for some strange reason this was called
every minute within an application, would this cause a memory leak?
void foo()
{
int x = 2;
char y[] = "hello world";
char * z = "Bye bye";
}

No, it won't leak memory

Wouldn't running foo() multiple time create multipe string literal
"Bye bye" that are in fact not reference by any code, hence not used.
No. Only once.
So the literal is of no use once the method is finsihed yet continue
to exist. So running foo() 1000 time would create 1000 string literal
in memory.
No, only once...

Bye, Jojo
 
R

RedLars

Newsbeitrag
Thanks for the reply
On 27 Sep, 08:34, (e-mail address removed) (Richard Bos) wrote:
This defines a local variable placed on the stack which will be
cleaned up when it goes out of scope, right?
You don't know that. All you know is that it _can_ be cleaned up when
it
goes out of scope, not that it is.
Given the method below, say for some strange reason this was called
every minute within an application, would this cause a memory leak?
void foo()
{
int x = 2;
char y[] = "hello world";
char * z = "Bye bye";
}
No, it won't leak memory
Wouldn't running foo() multiple time create multipe string literal
"Bye bye" that are in fact not reference by any code, hence not used.

No. Only once.
So the literal is of no use once the method is finsihed yet continue
to exist. So running foo() 1000 time would create 1000 string literal
in memory.

No, only once...

So the second time foo() is called the string literal constructed
during the first iteration is re-used? How are string literal stored
and how is this managed? I mean, does the runtime library loop through
some static string literal array when it sees:
const char * x = <text>
to see if it can find <text> already defined?

Thanks for the help.
 
R

Richard Bos

RedLars said:
RedLars said:
On 27 Sep, 10:04, "Joachim Schmitz" <[email protected]>
"RedLars" <[email protected]> schrieb im
Given the method below, say for some strange reason this was called
every minute within an application, would this cause a memory leak?
void foo()
{
int x = 2;
char y[] = "hello world";
char * z = "Bye bye";
}
No, it won't leak memory
Wouldn't running foo() multiple time create multipe string literal
"Bye bye" that are in fact not reference by any code, hence not used.

No. Only once.

So the second time foo() is called the string literal constructed
during the first iteration is re-used?

No. There is only one string literal object per source code string
literal. It is created, like all static scoped objects, when the program
starts, and destroyed only when it exits. The only thing that happens
when you reach the above code is that x, y and z are created; the value
2 is assigned to x; the contents of the pre-existing "hello world"
object are copied to y; z is pointed at the start of the pre-existing
"Bye bye" object; and at the end of the block, x, y, and z are
destroyed, and both string objects are left in existence so they can be
re-used next time.

Richard
 
B

Barry Schwarz

Thanks for the reply
On 27 Sep, 08:34, (e-mail address removed) (Richard Bos) wrote:
This defines a local variable placed on the stack which will be
cleaned up when it goes out of scope, right?
You don't know that. All you know is that it _can_ be cleaned up when it
goes out of scope, not that it is.
Given the method below, say for some strange reason this was called
every minute within an application, would this cause a memory leak?
void foo()
{
int x = 2;
char y[] = "hello world";
char * z = "Bye bye";
}

No, it won't leak memory

Wouldn't running foo() multiple time create multipe string literal
"Bye bye" that are in fact not reference by any code, hence not used.

No. String literals have static duration. This means that they are
created when your program loads (in most systems I would expect they
are created at compile time, not execution time, but that is an
implementation detail), not when the function in invoked. They remain
until your program terminates. On the other hand, z is created each
time the function is invoked (just like x and y) but it will always
point to the same place for any given execution of your program.
So the literal is of no use once the method is finsihed yet continue
to exist. So running foo() 1000 time would create 1000 string literal
in memory. This seem sort of pointless.

No, only one copy of the literal but possibly a thousand pointers to
it.


Remove del for email
 
R

RedLars

Thanks for the reply
On 27 Sep, 08:34, (e-mail address removed) (Richard Bos) wrote:
This defines a local variable placed on the stack which will be
cleaned up when it goes out of scope, right?
You don't know that. All you know is that it _can_ be cleaned up when it
goes out of scope, not that it is.
Given the method below, say for some strange reason this was called
every minute within an application, would this cause a memory leak?
void foo()
{
int x = 2;
char y[] = "hello world";
char * z = "Bye bye";
}
No, it won't leak memory
Wouldn't running foo() multiple time create multipe string literal
"Bye bye" that are in fact not reference by any code, hence not used.

No. String literals have static duration. This means that they are
created when your program loads (in most systems I would expect they
are created at compile time, not execution time, but that is an
implementation detail), not when the function in invoked. They remain
until your program terminates. On the other hand, z is created each
time the function is invoked (just like x and y) but it will always
point to the same place for any given execution of your program.
So the literal is of no use once the method is finsihed yet continue
to exist. So running foo() 1000 time would create 1000 string literal
in memory. This seem sort of pointless.

No, only one copy of the literal but possibly a thousand pointers to
it.

Remove del for email- Skjul sitert tekst -

- Vis sitert tekst -

Thank's for explaining.
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
474,078
Messages
2,570,572
Members
47,204
Latest member
MalorieSte

Latest Threads

Top