does the pointer need to be freed ?

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I'm not often using pointers to let's say arrays of undefined length but in the case below does the char pointer need to be freed ? Sure I always can use a fixed length but I'd like to know...

this is only a skeleton but enough to explain I think, the "StringToStruct" function copies from a global buffer into the different members of the structure. The function from the command_list is executed and copies a new message in the .message member. The "StructToString" function sends an output made from members of the structure. I initialized the "command_message.message" with NULL because the compiler complained. Now does the "*message" pointer has to be freed up afterwards ? Or is the memory free if the local variable dies ?

typedef struct{

    char context[3];
    char *message;
    uint8_t valid;
    uint16_t comno_int;

}a_command_message;

void Receive_command(void){
  
    a_command_message command_message;

    command_message.message = NULL;

    command_message = StringToStruct(ReceiveBuffer);

    command_list[comno](command_message.message);

    StructToString(command_message);

}

Thank you and sorry but I never really came across the question as I skipped C++ and went to C# instead and otherwise only used C with static arrays.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If I understand your question correctly, you have nothing to worry about.
.message is a char pointer 'within' a_command_message. command_message is created in function Receive_command(), and will be created on the stack. When Receive_command() exits, command_message will go out of scope and all members of that struct will be 'lost'/'freed' whatever you like to call it.

--greg
Still learning, don't shout at me, educate me.
Starting the fire is easy; the hardest part is learning how to keep the flame!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You only need to "free" something if you "malloc"ed that something to create it in the first place. "free" removes variables that were "malloc"ed on the heap. What you have is a local variable that is created automatically on the stack, not on the heap. So as Greg says, it will be removed automatically when the function exits.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

alright, thank you both !

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And to be technically nitpicking: It is not pointers you free, but what they point to.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quite right!
I do love a good nit pick ;)

--greg
Still learning, don't shout at me, educate me.
Starting the fire is easy; the hardest part is learning how to keep the flame!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:
And to be technically nitpicking: It is not pointers you free, but what they point to.

Oh, and never use a pointer to access, especially with a write, that which it points to, after what it points to has been freed.

Continuation of nit pick...

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Oh, and never use a pointer to access, especially with a write, that which it points to, after what it points to has been freed.

If you always set pointers back to 0 after free(ptr) you can guard against this - on most systems (but not AVR) a write through NULL will cause a seg fault.

(on AVR it's very unlikely you'd be using malloc()/free() anyway)