Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8187155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:28:09+00:00 2026-06-07T02:28:09+00:00

struct image_struct { unsigned int width; unsigned int height; char mode; char depth; unsigned

  • 0
struct image_struct {
    unsigned int width;
    unsigned int height;
    char mode;
    char depth;
    unsigned char data[13];
}

image_struct* newImage( unsigned int width, unsigned int height, char depth ) {

    image_struct* image = (image_struct*)malloc(
        sizeof(image_struct) - 13 + width * height * depth );

    return( image );
}

Visual Studio doesn’t complain about accessing the fixed array beyond the 13 bytes, is this inadvisable? My intent was to avoid processing headers in file IO by using straight memory writes for structs with built-in headers. Apologies for the title. :\

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:28:12+00:00Added an answer on June 7, 2026 at 2:28 am

    There’s a trick you can do where you define a zero-length array at the end of a struct. You can then allocate the sizeof the struct plus the size of the array you want and you get an array of any size you want, decided at run-time rather than compile-time. Here is some info on it:

    http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html

    Points to note:

    1. You must allocate the right amount of memory. You may be able to access the memory beyond the struct even if you didn’t allocate it. But that’s a bug in your code. The memory may be used for something else, or cross a boundary etc. Worst case it’ll overwrite some other data and you won’t discover it until some other part of your program behaves oddly. Never use heap memory you didn’t allocate.

    2. Once allocated you cannot resize the array without reallocing the entire struct + array size.

    3. The array has to be the last element of the array

    4. Make sure you know how long the array is meant to be. Maybe store the length in a field in the struct and do your own bounds checking to ensure you don’t go wrong with your pointer arithmetic (/array index access).

    This only applies to structs allocated on the heap, not automatic variables on the stack.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote in C a struct like that struct IMAGE { unsigned int x,
struct pointsto_val_def { unsigned int lhs; bitmap rhs; struct pointsto_val_def *next; }; typedef struct
struct Foo { int data; Foo() = default; Foo(const Foo& arg) = default; };
struct { char a; int b; } x; Why would one define a struct
typedef struct{ IMAGE *fuente; IMAGE *destino; int saltos; } ARGTHREAD; ARGTHREAD values; DWORD WINAPI
I have a function that translates data using std::map struct HistoParameter { int nbins;
I have created the following C library for reading an image: typedef struct {
struct SomeStruct { int a; int b; }; SomeStruct someFn( int init ) {
struct Div { int i; int j; }; class A { public: A(); Div&
struct struct0 { int a; }; struct struct1 { struct struct0 structure0; int b;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.