4

I want to use a static pointer in a function in order to point to a number of integers. The number of integers is not yet known while programming but it is known on runtime before the function is used first. So I want to give the function a parameter n and tell it to allocate memory space for n integers to the pointer and keep this. However, I learned that static variables have to initiated in their declaration and this doesn't seem to work here because on the one hand I need the * to declare them as pointers and on the other hand I need the variable name without * to allocate memory. What would a correct declaration and initialisation be for a static pointer? I'm trying to save time or else any computer that I can afford will need years for my program. As I learned that local variables are faster than global variables and pointers sometimes faster than arrays I'm experimenting with that. The function is used billions of times even in smaller test runs so any idea to speed it up is welcome. The use of pointers should also make some functions in the program work together a bit better but if they are local and initialized every time the function is called I don't expect it to be really fast.

3
  • Sounds like you want two separate functions, one that is only called once and one that gets called often Commented Jan 29, 2010 at 18:08
  • You seem to be confusing various meanings of the static keyword. Is the static pointer a module or function scoped variable? You can certainly do "static int *mypointer = NULL" inside a function and then alloc with it, but it's not clear why you'd do that. Commented Jan 29, 2010 at 18:14
  • No, I want to have this one function to be called very often but keep its pointer, so the pointer should only be initiated the first time I call the function and afterwards it should just meddle with the contents of the integers it points to. Commented Jan 29, 2010 at 18:15

3 Answers 3

4

Like this:

void foo() {
    static int* numbers = NULL;
    if (numbers == NULL) {
        // Initialize them
    }
}

Be prepared for concurrency issues. Why not make it a global and have a proper init_numbers() and user_numbers() function so that you control when the init happens?

Sign up to request clarification or add additional context in comments.

4 Comments

My big problem is that I am trying to make my program faster. I was told that pointers sometimes run faster than arrays and that local variables run faster than global ones - and that if statements are very slow, so your solution doesn't seem ideal but initialising local pointers every time doesn't either as the function is called billions of times (it is somehow the core of the program).
This won't make your program faster. Now you are checking if it's initialized every time you call foo.
Pointers and arrays are the same thing in C. Use a profiler to really find out where your app is slow. It sounds to me like you are just guessing at this point.
@StefanArentz: Pointers and arrays are definitely not the same thing in C. However, there is an implicit cast from an array to a pointer to the array's first element which will be used in most expression contexts, and it's this pointer that gets used for pointer arithmetic, dereferencing, etc. Note that some operations (e.g. sizeof) don't perform this implicit cast and will treat pointers and arrays differently. (I agree with you that indexing via pointers and arrays will have the same performance characteristics, though, and that use of a profiler is wise.)
1

I would try something like this:

void my_proc(int n)
{
    static int* my_static_pointer(0);

    if (my_static_pointer == 0)
    {
        my_static_pointer = malloc(sizeof(int) * n);
    }

    // check the allocation worked and use the pointer as you see fit
}

Comments

0

You can initialize the pointer to null and reuse it later.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.