checking if the memory was allocted previously

M

mjoachimiak

Hi,

I used smth like this (forgive me typos):

//////////////////////////
mystrucure *dynamic_element = NULL

void some_function()
{
if (dynamic_element == NULL)
dynamic_element = malloc(sizeofm(mystructure));
if (dynamic_element != NULL) do_smth(dynamic_element);
}

main()
{
call some_function() many times;
if (dynamic_element != NULL)
free(dynamic_element);
}
/////////////////////////////

there is a doc about GMutex
http://library.gnome.org/devel/glib/unstable/glib-Threads.html#GMutex
that says that this line

if (!mutex) mutex = g_mutex_new ();


can cause race condition.

what about my previous memory allocation ?
Can it cause race condition also?

BR
 
B

Bart van Ingen Schenau

Hi,

I used smth like this (forgive me typos):

//////////////////////////
mystrucure *dynamic_element = NULL

void some_function()
{
if (dynamic_element == NULL)
                 dynamic_element = malloc(sizeofm(mystructure));
if (dynamic_element != NULL) do_smth(dynamic_element);

}

main()
{
call some_function() many times;
if (dynamic_element != NULL)
   free(dynamic_element);}

/////////////////////////////

there is a doc about GMutexhttp://library.gnome.org/devel/glib/unstable/glib-Threads.html#GMutex
that says that this line

 if (!mutex) mutex = g_mutex_new ();

can cause race condition.

what about my previous memory allocation ?
Can it cause race condition also?

If some_function can be called from more than one thread, and you
can't guarantee that the memory has been allocated before those
threads are started, then your code also has a race condition, the
same as for the GMutex case.

If your program only has one thread (which is the default), then it is
absolutely impossible to have race conditions.

Bart v Ingen Schenau
 
M

Marcel Müller

Hi!
//////////////////////////
mystrucure *dynamic_element = NULL

void some_function()
{
if (dynamic_element == NULL)
dynamic_element = malloc(sizeofm(mystructure));
if (dynamic_element != NULL) do_smth(dynamic_element);
}

If you use C++ you should prefer new/delete over malloc where possible.
This eliminates the need for the second NULL check since new will not
return NULL.
main()
{
call some_function() many times;
if (dynamic_element != NULL)
free(dynamic_element);
}
/////////////////////////////
what about my previous memory allocation ?
Can it cause race condition also?

Yes, if and only if "call some_function() many times" calls
some_function in parallel.

You should put the initialization of dynamic_element in main before the
other processing. This would completely eliminate the need for the NULL
checks in some_funtion.

However, the content of mystructure has to be accessed synchronized or
atomic or read only anyway if you intend to operate in parallel without
race conditions.


Marcel
 

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

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top