0

Is it good coding practice, to use sizeof(struct) when the struct has a fixed length array?

#define NUM_TRACKS 10
#define NUM_SAMPLES 250
typedef struct _DATA_PROC
{
    uint16_t count[NUM_TRACKS] ;
    int16_t data[NUM_TRACKS][NUM_SAMPLES];
} tDataProcessing ;



tDataProcessing* tDp = malloc(sizeof(tDataProcessing)) ;
5
  • Some relavant issues for others reading this: stackoverflow.com/questions/57662381/… Commented Aug 26, 2019 at 17:29
  • 3
    Why wouldn't it be? Commented Aug 26, 2019 at 17:30
  • i want to make sure that some compilers do not interpret it as: typedef struct _DATA_PROC { uint16_t* count ; int16_t** data ; } tDataProcessing ; Commented Aug 26, 2019 at 17:31
  • Please put code in the question, it's not readable in a comment. And no, it won't do that. sizeof will give you the size you need to store the object. The arrays are within the object itself, not pointed to by it. Commented Aug 26, 2019 at 17:35
  • 2
    No compiler is allowed to misinterpret the struct in the question as typedef struct _DATA_PROC { uint16_t* count; int16_t** data; } tDataProcessing; — at least, it can't claim to be a standard-conforming C compiler if it does. Commented Aug 26, 2019 at 17:37

2 Answers 2

1

I personally prefer to use the pointer variable itself rather than the type:

tDataProcessing *tDp = malloc(sizeof *tDp);

This is sometimes clearer in cases with pointers buried in types and such.

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

5 Comments

I'm not sure this is actually safe since you dereference the uninitialized tDp, even though you don't do anything with it. I believe the argument to sizeof can still have side-effects occur.
@ThomasJager: It's fine — sizeof doesn't dereference the pointer. I'm not sure this is an answer to the question, but what it suggests is kosher.
Yes, sizeof does not evaluate its expression.
@JonathanLeffler Ah, ok, I confused it being able to have side-effects with it always having side-effects.
This is safer in case the type of tDp is changed.
0

For starters in expressions, with rare exceptions including the sizeof operator, arrays are implicitly converted to pointers to their element types.

So for example the array

int16_t data[NUM_TRACKS][NUM_SAMPLES];

is converted in expressions like

int16_t ( *data )[NUM_SAMPLES];

not as you wrote in a comment like

int16_t** data

However a declaration is not an expression. So the sizeof operator returns the number of bytes to accommodate all data members of the structure without any implicit conversion.

From the C Standard (6.5.3.4 The sizeof and alignof operators)

2 The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand

Here is a demonstrative program

#include <stdio.h>

int main(void) 
{
    enum { N = 10 };
    int a[N];

    printf( "sizeof( a ) = %zu\n", sizeof( a ) );

    struct A
    {
        int a[N];
    };

    printf( "sizeof( struct A ) = %zu\n", sizeof( struct A ) );

    return 0;
}

Its output is

sizeof( a ) = 40
sizeof( struct A ) = 40

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.