CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

How to initialize or free a dynamic memory allocation

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

How to initialize or free a dynamic memory allocation
PostPosted: Mon Feb 12, 2024 11:01 am     Reply with quote

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: 19231

View user's profile Send private message

PostPosted: Mon Feb 12, 2024 12:10 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Feb 12, 2024 4:25 pm     Reply with quote

Telmah, thanks for the clarification, now I have it clearer.
leach67



Joined: 13 Feb 2024
Posts: 3

View user's profile Send private message

PostPosted: Fri Feb 16, 2024 5:41 am     Reply with quote

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..
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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