2

I'm studying C and reading this

#include <stdio.h>
#include <time.h>

/* function to generate and retrun random numbers. */
int * getRandom( ) {

   static int  r[10];
   int i;

   /* set the seed */
   srand( (unsigned)time( NULL ) );

   for ( i = 0; i < 10; ++i) {
      r[i] = rand();
      printf("%d\n", r[i] );
   }

   return r;
}

/* main function to call above defined function */
int main () {

   /* a pointer to an int */
   int *p;
   int i;

   p = getRandom();

   for ( i = 0; i < 10; i++ ) {
      printf("*(p + [%d]) : %d\n", i, *(p + i) );
   }

   return 0;
}

The article explains that, since a pointer declared in a function is a local variable, hence it has to be defined as 'static'. (line 7, variable r)

There are three points I'm concerned.

  • Firstly, "static int r[10]" declares that they want to create an int array of size 10. When it's returned from the getRandom function with "return r", the function actually returns the address of the first member of r[10], or the pointer to the first address of r[10]?

  • Secondly, in main function, p = getRandom() means the address or pointer returned from getRandom() is assign to pointer p. Eventhough r will poof as soon as getRandom() stops working, the address would have been already assigned to p. So, why is it needed to be declared as static?

    Or getRandom() returns address to the pointer pointing to r[10], hence when disappear, p would point to empty address not to r[10]?

    Or, not only pointer to r, but also r[10] array would disappear when getRandom() stops, so it's needed to be static?

  • Thirdly, is it correct that, variables in any function declared without malloc would be stored as stack memory?

2
  • 4
    It is a long question. I would suggest to read a good C programming book, compile your example with warnings & debug info (gcc -Wall -Wextra -g if using GCC...) and run your example step by step in a debugger (gdb) and query the value of interesting variables Commented Mar 2, 2016 at 14:05
  • 1
    As tutorials go, that one has a horrible mistake in it. To see what I mean, call getRandom() at least three times in a row and compare the numbers it emits... Commented Mar 2, 2016 at 15:22

3 Answers 3

1

getRandom() returns pointer to the first element of r[10].

Quote from N1256 6.3.2.1 Lvalues, arrays, and function designators

3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

If r[10] is not static, the array wil vanish on returning from getRandom() and accessing after that will invoke undefined behavior.

"variables in any function declared without malloc would be stored as stack memory" is not correct. Any variables in C need not be stored as stack memory. Typically, static variables are not stored on stack while variables with automatic storage duration are stored on stack.

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

2 Comments

I don't think quoting the standard is worth it or meaningful altogether in this case.
So, the place where static variables are stored is not specified by the standard and it could be on the stack?
1

the function actually returns the address of the first member of r[10], or the pointer to the first address of r[10]?

A: Address of the first member.

Eventhough r will poof as soon as getRandom() stops working,....

A: No, static variables do not go into program stack memory (unlike local variables). They live entire time of the program execution.

Thirdly, is it correct that, variables in any function declared without malloc would be stored as stack memory?

In short, yes. OTOH, malloc()-ed memory lifetime is not limited by the function scope.

Comments

0

The article explains that, since a pointer declared in a function is a local variable, hence it has to be defined as 'static'. (line 7, variable r)

r is a array of integers, not a pointer. And local need not imply static, which infers another meaning.

the function actually returns the address of the first member of r[10], or the pointer to the first address of r[10]?

It returns the address of the first member: this happens automatically when you type return r which is equivalent to the more verbose return &r[0].

static variables exist from the beginning of the program and are destroyed at its end -- thus before main and after main. To return a pointer to local data, like from a function, it has to be static, otherwise -as you say- you would be accessing deallocated data, which is not allowed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.