Array Size Allocation question

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would like to declare an array with a size determined by a [const] variable -- for readability purposes. This is the only place I am using this "variable", so a #define macro seems like overkill. From my understanding of the C99 standard, this is allowed. When I compile the following code, I get no warnings/errors. When I test it, it seems to work.

My questions:
1. Is this truly safe, or are my tests that have made it seem like it works just not exhaustive enough (i.e., I am just getting lucky with memory)?

2. Even if it is truly safe, is it bad form? Would it work for C90? Should that even be a consideration, or is C99 almost universal now?

3. Is the "const" necessary? Should it be "static const"?

Code:

int16_t main(void) {
   const uint8_t arraySize = 10;
   myStruct_t packets[arraySize];
   foo(packets, arraySize);
}
void foo(myStruct_t foo1, uint8_t sz) {
   //Do stuff
}

Science is not consensus. Science is numbers.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hobbss wrote:
a #define macro seems like overkill
Try compiling with a macro and with a [const] variable. Compare the resulting code sizes.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
1. Is this truly safe
With C99 yes, it is safe.
Quote:
Would it work for C90?
No, it would not compile.
Quote:
Is the "const" necessary?
No. The point of having the ability is to select the array size at run time.
Quote:
Should it be "static const"?
I see no advantage of this.

But if it is really only this one place and a constant, then I would use #define.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use enum for integer constants, i.e.

enum { arraySize = 10 };

Sid

Life... is a state of mind

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the responses.

I have compared code sizes (C99) for:

1. #define
2. variable declaration
3. enum{}

In all three cases, the code was exactly the same size. For portability purposes, I will use the #define option.

Science is not consensus. Science is numbers.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Both the #define and the enum would be portable. For a single value that you know will never change, either one would work as well. You need to look at the specifics on how you are using it to determine which is clearer or easier to maintain.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

#defines are handled by the preprocessor, enums are handled by the compiler.

Make an error in one of your #defines and you will not know until you actually use it - maybe years later - and the error message, if you get one, will not necessarily be easy to understand.

Sid

Life... is a state of mind

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What a lame argument.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:
What a lame argument.

Oh yeah ?

Well, at least I don't have a huge ugly nose like you do.

Sid

Life... is a state of mind

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In some environments I've developed for, static const symbols stand a better chance of being represented in the symbolic debug tables for the executable code than preprocessor #defines do. And in some cases, enums are better still (like when the returned value from that subroutine you just "stepped over" shows up as "TANK_EMPTY" instead of 42)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't think I've ever used a static const, although I can see how they are useful for constants that can't be represented as an enum value.

One reason I prefer enums is that the constants are never stored unless you want them to be. You have to declare a variable or parameter for them to take up any space.

In C++ they add type safety, too. You're not allowed to pass a FourInchNail to a function that expects a Screw-type parameter.

Sid

Life... is a state of mind