When to use memory on heap or stack

P

Pieter Claassen

I have a number of functions that does the following:


char * test(){

char * test;
....
return test;
}

I am aware that this will allocated a memory for a pointer on the stack
and then return that.

1. How do I free that memory?
2. Is this healthy or should I be mallocing that memory? Even if it is for
a pointer.

Thanks,
Pieter
 
J

Joona I Palaste

Pieter Claassen said:
I have a number of functions that does the following:

char * test(){
char * test;
....
return test;
}
I am aware that this will allocated a memory for a pointer on the stack
and then return that.

No it won't. It might allocate memory from anywhere or not at all. There
is no "stack" or "heap" in C.
1. How do I free that memory?

Depends on how you allocated it. If you used malloc(), calloc() or
realloc(), you call free() like anywhere else. If you used the address
of an automatic object, you don't free it at all.
2. Is this healthy or should I be mallocing that memory? Even if it is for
a pointer.

Depends entirely on your code. You know, "...." isn't telling us much of
what your function actually does, least of all where the memory is
coming from. If we replace "...." with code that doesn't assign anything
to test, then you're not returning a pointer to *any* memory at all,
it's uninitialised.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"As a boy, I often dreamed of being a baseball, but now we must go forward, not
backward, upward, not forward, and always whirling, whirling towards freedom!"
- Kang
 
J

Jens.Toerring

Pieter Claassen said:
I have a number of functions that does the following:
char * test(){
char * test;
....
return test;
}
I am aware that this will allocated a memory for a pointer on the stack
and then return that.

You don't allocate anything here, you just got an automatic variable
(that may be on the stack or somewhere else, you can't tell unless
you find out how your implementation is doing things, but which would
make it off-topic here). If you return from the function it will
vanish automatically.

And, no you're not returning the pointer, you're returning what it's
pointing to. Big difference! And since you don't show anything that
sets the value of 'test' it's impossible to say if you return anything
at all that's not some random, unitialized value.
1. How do I free that memory?

There's no memory allocated here (at least not that you show it), so
you can't free() anything. The 'test' pointer itself will vanish into
thin air when you leave the function without you needing to do anything.
2. Is this healthy or should I be mallocing that memory? Even if it is for
a pointer.

All this makes no sense at all. You just have an automatic variable
that's pointing to nothing you can use. It's definitely unhealthy if
you return what 'test' points to unless you make it point to some
memory you own, e.g. memory you got by calling malloc() or calloc()
or realloc().
Regards, Jens
 
B

Barry Schwarz

You don't allocate anything here, you just got an automatic variable
(that may be on the stack or somewhere else, you can't tell unless
you find out how your implementation is doing things, but which would
make it off-topic here). If you return from the function it will
vanish automatically.

And, no you're not returning the pointer, you're returning what it's
pointing to. Big difference! And since you don't show anything that
sets the value of 'test' it's impossible to say if you return anything
at all that's not some random, unitialized value.

Actually, he is returning the value of the pointer which is the
address of what it is pointing to. To return what test points to, he
would have to code
return *test;
There's no memory allocated here (at least not that you show it), so
you can't free() anything. The 'test' pointer itself will vanish into
thin air when you leave the function without you needing to do anything.


All this makes no sense at all. You just have an automatic variable
that's pointing to nothing you can use. It's definitely unhealthy if
you return what 'test' points to unless you make it point to some
memory you own, e.g. memory you got by calling malloc() or calloc()
or realloc().

Or by assigning to test the address of some other char that will
persist after the function returns.



<<Remove the del for email>>
 
M

Malcolm

Pieter Claassen said:
I have a number of functions that does the following:


char * test(){

char * test;
....
return test;
}
Pointers are copied when returned, the memory they point to isn't.


void foo(void)
{
char *ptr;

ptr = bad();
free(ptr);
}

char *bad(void)
{
char buff[1024];
char *answer;

strcpy(buff, "This string will be returned incorrectly");
answer = buff;

return answer;
}

char *good(void)
{
char *answer;

answer = malloc(1024);
if(answer)
strcpy(answer, "This string will be returned properly");

return answer;
}

void bar(void)
{
char buff[1024];

thefastway(buff);
printf("%s\n", buff);
}

void thefastway(char *buff)
{
strcpy(buff, "this string will be returned without malloc() overhead");
}
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top