Declaring array twice (C99)

Go To StackoverFlow.com

0

I'm a noob so I'll make this short. Is the following legal (C99)? I don't want to store more than the one array at a time. I could make an array that is of maximum needed length and use only part of it, but I'd rather do this.

int main(){
  double drm[15000];
  //do some stuff
  double drm[14000];  
  //do some other stuff
}
2012-04-04 18:19
by user1313527


1

No, you can't do that. They need to be in different blocks. You could do:

int main(void)
{
  {
    double drm[15000];
    // do some stuff
  }
  {
    double drm[14000];
    // do some other stuff
  }
}

But why not just call a function? Even if you put them in different blocks, you don't really have any guarantees about what the compiler is going to do vis-a-vis storage.

2012-04-04 18:22
by Carl Norum
Thanks! I didn't put it in my minimal example, but this is actually inside a for(blah=0;blahuser1313527 2012-04-04 19:26
Yes - a new block starts anywhere there is a { and ends where ever the matching } occurs - Carl Norum 2012-04-04 20:43


0

As arrays are strongly related to pointers in C, you could work with dynamic arrays as well. First, allocate a new double array with 15 000 elements. When you don't need it anymore, delete it. Create a new array with 14 000 elements then.

Don't forget to free your heap after using your arrays. People tend to forget this. Moreover, you should check whether the allocation was successful.

int main(void)
{
    double *drm = 0;
    drm = (double*)malloc(15000 * sizeof(double));
    // do something
    free(drm);
    drm = (double*)malloc(14000 * sizeof(double));
    // do some other stuff
    free(drm); // don't forget me
}

However, I would agree with Carl Norum: use functions instead.

2012-04-04 18:27
by Dominik M.
Post is tagged as C99, not C++. Please update your example to use malloc() and free() - tomlogic 2012-04-04 18:28
when was new added to C99 - KevinDTimm 2012-04-04 18:29
Oh sorry, I probably mixed these two languages up. I'll fix that later - Dominik M. 2012-04-04 18:30
"As arrays are pointers in C" -- No, they most certainly are not. Read section 6 of the comp.lang.c FAQ. And your malloc call is better written as drm = malloc(15000 * sizeof *drm);Keith Thompson 2012-04-04 19:04
But they're strongly related, that was what I meant. Thank you - Dominik M. 2012-04-04 19:07
@DominikM.: Then say that. You're propagating a common misunderstanding. Arrays are not pointers. (And you're not checking whether malloc() succeeded. - Keith Thompson 2012-04-04 19:09
Usually, I'm checking whether malloc succeeded. But my code is just an example, of course one should include error handling. However, I don't know the complexity or the size of the opener's project. In order not to confuse him or her, I decided not to add this to the example. You're right indeed: I should have said that as well. Forgive me please - Dominik M. 2012-04-04 19:19
@DominikM.: You know you can edit your answer, right? If you'll remove the incorrect statement that "arrays are pointers in C", I'll withdraw my downvote - Keith Thompson 2012-04-04 19:32
@KeithThompson fixed - Dominik M. 2012-04-04 19:49


0

You could use malloc and free (or realloc if you don't mind keeping the data):

#include <stdlib.h>
int main(void)
{
      double *drm = (double *)malloc(sizeof(double)*15000);
      //do stuff
      free(drm);
      drm = (double *)malloc(sizeof(double)*14000);
      //do other stuff
      free(drm);
}
2012-04-04 18:29
by Jacob
Do not cast the result of malloc( - KevinDTimm 2012-04-04 18:53
@KevinDTimm why not? There's nothing wrong with it - Jacob 2012-04-04 18:56
@JacobAbrahams: There's nothing right with it. An expression of type void* may be implicitly converted to any pointer type (other than a pointer-to-function type). The cast is just clutter, and depending on the circumstances it can mask errors. For example, in C90 if you forget the #include <stdlib.h> (not <malloc.h>; that's non-standard), the compiler will assume that malloc returns int; casting the result to double* can cause incorrect code to be generated - Keith Thompson 2012-04-04 19:06
@KeithThompson If I forget stdlib.h, compiler assumes a return type of type of int and we, inadvertently will be casting it to some pointer which is still incorrect. In fact is this case compiler can generate a "cant convert from int to some*" erro - Pavan Manjunath 2012-04-04 19:14
Oops! It seems that C doesn't mind something to something* conversion! Ahh, I always run C programs with a CPP compiler. My bad : - Pavan Manjunath 2012-04-04 19:24
@PavanManjunath - re: your first comment, the compiler returns an error when you've forgotten stdlib.h, you wouldn't realize this error if you did the cast -- and that's why we don't cast : - KevinDTimm 2012-04-04 20:04
Ads