Reputation: 393
I have a method that takes a pointer T to a valid memory block. Is the following wise within that method given that no stack overflow will result since the constant some_defined constant is known to be small?
if (T)
{
p = T;
q = T + k;
}
else
{
unsigned long long TT[2 * some_defined constant];
p = TT;
q = TT + k;
}
<code using p and q>
I would never contemplate doing this except that I know the method is being called concurrently; it is called five times, each call being on a separate thread. The method that makes such a call uses an OMP loop. If that calling method invoking the OMP loop is to cater for the required extra memory required and pre-allocated in T it needs to request from the caller an allocation of 10 * some_defined constant words, if the method called within the OMP loop allocates 2 * some_defined constant then that would work very well. It would be great to make this conditional so as that when the risk of a stack overflow appears to be possible the call to the top method can be made after allocating 10 * some_defined constant words on the heap.
Upvotes: 2
Views: 544
Reputation: 91119
In this case I would do (pseudo-code):
void function_which_does_the_job(... p, ... q)
{
<code using p and q>
}
if (T)
{
function_which_does_the_job(T, T+k);
}
else
{
unsigned long long TT[2 * some_defined constant];
function_which_does_the_job(TT, TT+k);
}
Then you don't repeat yourself and nevertheless can do what you want.
If your compiler indeed doesn't support VLAs, do
if (T)
{
function_which_does_the_job(T, T+k);
}
else
{
unsigned long long * TT = malloc(2 * some_defined constant * sizeof(*TT));
function_which_does_the_job(TT, TT+k);
free(TT);
}
instead.
(Of course, you'll have to pass other variables down to the function as well; if k
is amongst these, you can omit q
and determine it in the function.)
Upvotes: 3
Reputation: 6788
You can use the alloca
function for dynamically allocating bytes on the stack. Like the C99 variable length arrays, it provides no means to detect allocation failure, so it bears a certain risk unless you ensure that the allocated amount is small.
if(T) {
p = T;
q = T + k;
} else {
p = alloca(2 * some_defined constant * sizeof(long long));
q = p + k;
}
/* use p and q until the end of the function scope */
Upvotes: 1
Reputation: 110172
The lifetime of the TT
array only extends until leaving the block in which it was declared (the else
block). So your code will result in undefined behavior when it is accessed past the end of the else
block.
If you move the declaration of TT
to above the if
statement, the code will be correct. But of course it will be allocated on the stack even when it isn't needed.
As noted in a comment by user self, You can change the size of TT
based on whether it is needed by using C99 variable length arrays:
unsigned long long TT[is_needed ? some_size : 1];
Upvotes: 7