A
A
Aren_SH2013-02-15 09:35:10
C++ / C#
Aren_SH, 2013-02-15 09:35:10

free and const

Faced with the following situation. There is a pointer (void *) which is subsequently freed via free()

struct {
  void *value;
  ....
};
...
// Присваивается указатель на какое-нибудь значение
node->value = value;
...
free(node->value);


The problem arises if node->value is assigned a pointer to a const variable (or string constant) that free() cannot clear.

Is it possible to check that a valid pointer is being passed to free() and prevent the problem? Or does it remain on the conscience of the pogromist?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Monnoroch, 2013-02-15
@Aren_SH

void free(void * ptr);

but not
void free(const void * ptr);

Static typing checked for you.
It is not possible to disable the possibility of manual casting.

I
Ilya Evseev, 2013-02-15
@IlyaEvseev

if node->value assign pointer to const variable

A normal compiler should swear at such an assignment.

I
ixSci, 2013-02-15
@ixSci

No.

A
Aren_SH, 2013-02-15
@Aren_SH

Clear. Thanks everyone for the replies.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question