0

I have the following declaration inside a function

int f[20000]

I want the number 20000 to be dynamic, How can i declare such array in code?

To be more specific, I have the following code to calculate PI.

#include <stdlib.h> 
#include <stdio.h> 

#define BITS 2000
int a=10000,b,c=BITS*7/2,d,e,f[BITS*7/2+1],g; 

int main() 
{   
    for(;b-c;) 
        f[b++]=a/5; 
    for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a) 
        for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);     
    //getchar();
    return 0;
} 

I changed to

#include <stdlib.h> 
#include <stdio.h> 

int main(int argc, char *argv[]) 
{   
    //
    // .... omit some lines here
    // read bits from user input at runtime
    // say precision = 200
    //
    int a=10000,b,c=precision *7/2,d,e,f[precision *7/2+1],g; 
    for(;b-c;) 
        f[b++]=a/5; 
    for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a) 
        for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);     
    //getchar();
    return 0;
} 

It doesn't work, I googled then changed to

int a=10000,b,c=precision *7/2,d,e,g; 
int *f=calloc(precision *7/2+1, sizeof(int));

It still doesn't work, I mean the program doesn't crash, the value it calculated is not correct. What's wrong? Thank you.

11
  • 1
    Please define It still doesn't work Commented Mar 18, 2015 at 20:04
  • What is the value in precision. Where are you reading the user value for precision. Commented Mar 18, 2015 at 20:08
  • a 20000 * sizeof int array, (80000 bytes) on the stack is a very bad idea. probably resulting in an overflow of the stack. suggest: int arraySize = 20000; int pf = malloc( arraySizesizeof int); (then check that 'pf' does not contain NULL) (be sure to use: 'free(pf);' when done with the array Commented Mar 18, 2015 at 20:10
  • this code does not cleanly compile. a couple of the reasons are the parameters argc and argv are not used. suggest using 'int main(void) Commented Mar 18, 2015 at 20:12
  • the ''7/2' is performing an integer divide, so the result will always be '3' not 3.5' Commented Mar 18, 2015 at 20:14

4 Answers 4

3

There are two ways to achieve what you want.

  1. use dynamic memory allocation. malloc()/calloc()
  2. use variable-length array (in c99)

That said, as pointed out by @user3386109, the problem in your second code snippet is use of uninitiated variable b. You may want to explicitly initialize the local variables before using their value.

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

Comments

3

You get a dynamically sized array by allocating on the heap using malloc (or calloc).

Replace

int f[20000];

with

int *f = (int *) malloc(20000 * sizeof(int) );

Comments

1

The difference is that global variables are guaranteed to be initialized to 0 (unless initialized to some other value). But local variables are garbage unless you initialize them. So the problem is that variable b starts out as garbage in the second snippet.

In the original code:

int a=10000,b;
int main(void)
{
}

a will start with the value 10000 because you initialized it, and b will start as 0 because it is an uninitialized global variable.

In the changed code:

int main(void)
{
    int a=10000,b;
}

a will start with the value 10000 because you initialized it, and b will start as some random value (e.g. 0x5315fe) because it is an uninitialized local variable.

2 Comments

the question was NOT 'what is wrong with my code>', (of which there are lots of things wrong). the question was 'how to dynamically size an array?'
The question itself had two perfectly good ways to dynamically allocate an array, but from the OP's point-of-view neither appeared to work. So then the question does become, "Why doesn't this code work?"
-1

Replace int f[2000]

with

int *f = new int[2000];

then use the array as f[0] = 1, f[1] = 2 etc...

when finished free up memory with delete [] f;

the array size could be allocated by a variable

eg. int x = 2000; f = new int[x];

1 Comment

This question is tagged c and AFAIK, new is not c.

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.