Memory allocation

R

Rafi Kfir

HI,

I would like to ask if the follwoing example is correct:

char *Title=NULL;

Title = "This is message 1";
----
----
---
Title = This is another message";

It works fine for me but I was advised to use memory allocation like
malloc. Is it necessary? I don't seem to have problem with it...

Thank you
 
J

Joona I Palaste

Rafi Kfir said:
HI,

I would like to ask if the follwoing example is correct:
char *Title=NULL;
Title = "This is message 1";
It works fine for me but I was advised to use memory allocation like
malloc. Is it necessary? I don't seem to have problem with it...

If you only ever intend to read the contents of Title, this way works
fine. But if you want to modify the string itself, instead of moving
Title to point at another string, you need memory allocation like
malloc.
 
M

Mike Wahler

Rafi Kfir said:
HI,

I would like to ask if the follwoing example is correct:

char *Title=NULL;

Title = "This is message 1";

Once you fix the syntax error:

Title = "This is another message";

It's fine.

It works fine for me but I was advised to use memory allocation like
malloc. Is it necessary?

Not in this case. All you're doing is creating a pointer, then
assigning it the addresses of literal strings. Just don't try
to modify them (i.e. by writing via a dereference of the pointer
'Title'). You can help protect against this if you define your
pointer thus:

char const *Title = NULL;

Then if you try to write through this pointer, you should
get a compiler error or warning.

If you do need to modify what 'Title' points to, then change
its definition to an array, e.g.:

char Title[100];

or allocate memory and assign
its address to it.

char *Title = malloc(100);

If you allocate memory, don't forget to 'free()' it when
you're done with it.

-Mike
 
R

Rafi Kfir

">
or allocate memory and assign
its address to it.

char *Title = malloc(100);

If you allocate memory, don't forget to 'free()' it when
you're done with it.

-Mike

Thanks Mike,

Do you still have to 'free()' it if it is inside a local procedure.
Doesn't it get freed automatically where the procedure is returned?

Thanks
Rafi
 
J

Joona I Palaste

Thanks Mike,
Do you still have to 'free()' it if it is inside a local procedure.
Yes.

Doesn't it get freed automatically where the procedure is returned?

No. The memory is still there, even if local pointer variables pointing
to it have gone out of scope.
It's the same as having this code:

int getnumber() {
int a = 1;
return a;
}

and asking whether the return value can be used, because the number 1
was freed when the function returned.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola
 
M

Mike Wahler

Rafi Kfir said:
">

Thanks Mike,

Do you still have to 'free()' it if it is inside a local procedure.
Yes.

Doesn't it get freed automatically where the procedure is returned?

No. Memory allocated with 'malloc()', 'calloc()', or 'realloc()'
remains allocated for the duration of the program's execution.
Most operating systems will reclaim the memory upon termination
of the program, but it's never a good idea to depend upon that.
If you allocate it, you should free it.

-Mike
 

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

No members online now.

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top