0

I am trying to return an array from the function, it is working fine as long as I am using hard coded value for size of an array. However, when I change it to dynamic (getting calculated from nproc = sysconf(_SC_NPROCESSORS_ONLN);) then I am getting following error:

 -->gcc test.c
test.c: In function ‘getRandom’:
test.c:14:16: error: storage size of ‘r’ isn’t constant
    static int  r[nproc];
                ^
test.c:18:21: warning: implicit declaration of function ‘time’; did you mean ‘nice’? [-Wimplicit-function-declaration]
    srand( (unsigned)time( NULL ) );
                     ^~~~
                     nice

when I change static int r[10]; to static int r[nproc]; its failing. I need to keep the size dynamic as the its going to be runtime calculated. Can someone please help me to get through this problem ?

Code:

#define _GNU_SOURCE
#include <assert.h>
#include <sched.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/* function to generate and return random numbers */
int * getRandom(int nproc ) {

   printf("nproc is %d\n",nproc);
   //static int  r[10];
   static int  r[nproc];
   int i;

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

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

   return r;
}

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

   /* a pointer to an int */
   int *p;
   int i;
   int nproc;
   nproc = sysconf(_SC_NPROCESSORS_ONLN);
   p = getRandom(nproc);

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

   return 0;
}

Need to know how to achieve this in C PROGRAMMING

monk
  • 1,953
  • 3
  • 21
  • 41
  • 1
    Possible duplicate of [Returning a variable length array from a function](https://stackoverflow.com/questions/41823420/returning-a-variable-length-array-from-a-function) – Achal Apr 12 '19 at 18:31
  • The size of a static array must be known at compile time. You can only use a variable length array as an automatic variable. `static int r[nproc];` is probably implemented before any value of `nproc` is known (before `main` is called). Suggest using `malloc`. – Weather Vane Apr 12 '19 at 18:32
  • @WeatherVane is there any alternate way to keep it dynamic as per requirement, I cannot know the number of cpu at compile time ? – monk Apr 12 '19 at 18:35
  • 2
    I suggest using `malloc` and `free` after the caller has no more need for it. – Weather Vane Apr 12 '19 at 18:36
  • `malloc` for `nproc` variable or for the returned array ? – monk Apr 12 '19 at 18:40

2 Answers2

2

You cannot define a static array with a non fixed size because it is like defining a global array with dynamic size. It is illegal in C because the global variable are part of the binary and must have a known size at the compilation.

If you really want to keep it static, you need to define the array of maximum possible size, and then pass the nproc value as the upper limit each time you call the getRandom() function.

Example:

/* function to generate and return random numbers */
int * getRandom(int nproc ) {

   printf("nproc is %d\n",nproc);
   static int  r[MAX_POSSIBLE_LENGTH];
   int i;

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

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

   return r;
}

There is also a possibility to allocate/reallocate each time the required array size (by malloc/realloc) in the caller of getRandom() and the pass the pointer to it and the size to getRandom():

void getRandom(int *pArr, unsigned int size);

In this case you won't need to hold any static arrays.

Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
  • can you please tell me how to do it with `malloc` ? – monk Apr 12 '19 at 19:12
  • can you please check , I have tried implementing your suggestion at https://stackoverflow.com/questions/55658837/print-the-pointer-value-which-is-already-incremented – monk Apr 12 '19 at 20:41
  • `srand( (unsigned)time( NULL ) );` should be remove from `getRandom()`, else 2 quick calls to `getRandom()` return the same number. – chux - Reinstate Monica Apr 12 '19 at 21:30
  • @monk Sorry. I am having a very limited access to the internet right now. Will be able to take a look just in about two days... Sorry. – Alex Lop. Apr 12 '19 at 22:35
1

I think what you want is:

int *r = (int *) malloc(sizeof(int) * nproc);// allocating for nproc integers

Nellie Danielyan
  • 1,001
  • 7
  • 19