|
|
View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
How to initialize or free a dynamic memory allocation |
Posted: Mon Feb 12, 2024 11:01 am |
|
|
Hi, Could someone tell me how to initialize or free a dynamic memory allocation in CCS, just for summary purposes I have the following code:
Code: | #include <18F4620.h>
#fuses HS,WDT32768,PROTECT,NOLVP,MCLR,BROWNOUT,PUT
#use delay(clock=20MHz)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)// RS232 Estándar
#include <stdio.h>
char* myFuntion1() {
char message1[] = "Hello, from the function1";
return message1;
}
char* myFuntion2() {
char message2[] = "Hello, from the function2";
return message2;
}
void main() {
char* issue1;
char* issue2;
issue1 = myFuntion1();
issue2 = myFuntion2();
printf("%s\r\n", issue1);
printf("%s\r\n", issue2);
while (TRUE);
}
|
Where when I call the two functions the results are passed to two pointers but when I try to print the contents It prints only the last one that has been called.
I need to initialize or free the pointer, can this be done?
The Functions 1 and 2 should not be modified |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Mon Feb 12, 2024 12:10 pm |
|
|
Your problem is not freeing a memory allocation, It is the dynamic nature
of temporary variables.
A variable declared inside a function _only exists while the function is
running, unless it is declared as static_. So neither 'message' variable
actually exists once you exit the function. The RAM they use is available
for re-use. Some of this is then used for the second variable. It is
fundamentally fallacious to pass a pointer to a temporary variable,
except to a function 'inside' another, where the variable will still exist
while you are still inside the outer function.
This is standard C.
If you want to pass a pointer to a variable to be used outside it either
needs to be global or static. |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Mon Feb 12, 2024 4:25 pm |
|
|
Telmah, thanks for the clarification, now I have it clearer. |
|
|
leach67
Joined: 13 Feb 2024 Posts: 3
|
|
Posted: Fri Feb 16, 2024 5:41 am |
|
|
Ttelmah wrote: | Your problem is not freeing a memory allocation, It is the dynamic nature
of temporary variables.
A variable declared inside a function _only exists while the function is
running, unless it is declared as static_. So neither 'message' variable
actually exists once you exit the function. The RAM they use is available
for re-use. Some of this is then used for the second variable. It is
fundamentally fallacious to pass a pointer to a temporary variable,
except to a function 'inside' another, where the variable will still exist
while you are still inside the outer function.
This is standard C.
If you want to pass a pointer to a variable to be used outside it either
needs to be global or static. |
hey
thanks for this.. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|