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

  • SEARCH
  • Home
  • 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 6737461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:15:06+00:00 2026-05-26T11:15:06+00:00

I have an array of structs, and I have some functions that will be

  • 0

I have an array of structs, and I have some functions that will be using several of the members of those structs. I would like to avoid the dereference in every line. I would think that there would be some way to declare a variable at a certain memory location… something like:

someStruct &myStruct = arrayOfStructs[i];
myStruct.x = foo+bar*myStruct.y*myStruct.w;
//Instead of myStruct->x = foo+bar*myStruct->y*myStruct->w;
//It would/should even be possible to access the members in a similar way:
int &x = &myStruct.x;
x = x+4*y+2*z;
//This should avoid overhead of dereferencing the pointer, and offsetting to the member
//by just accessing that particular address of memory as though it was where the variable
//had always been.

This bit of example code may help explain:

#define NUM_BIGSTRUCTS 10000

typedef struct {
  int a,b,c;
  float d,e,f;
} bigStruct;

bigStruct* arrayOfStructs;

void foo() {
  for(int i=0; i<NUM_BIGSTRUCTS; i++) {
    bigStruct* temp = arrayOfStructs[i];
    temp->f = (temp->d+temp->e)*((float)temp->a+temp->e);
    //more similar, with conditionals, etc...
    //actually I've got nested loops, and a very very large array
    //so any gains per inner loop would decrease my number of instructions exponentially

    //So, if I could declare a bigStruct and set its address to the location of a bigStruct in the array
    //then I could avoid a dereference every time I access a member of that bigStruct
    //Leaving just the member access overhead... which could be handled in a similar manner
    //if possible, and when appropriate
  }
}

int main(int argx, char** argv) {
  arrayOfStructs = g_new0(bigStruct,NUM_BIGSTRUCTS); //Allocate and 0 memory for simplicity

  foo();

  return 0;
}

I never have had great success on SO, so hopefully I explained what I’m trying to do. I’m using C99 btw, and I would believe it’d be possible given the low level nature of c.

[edit]
Looks like I was looking for ‘References’ from C++, but for C. Even so, they only allow assignment once(initialization), which wouldn’t work in my example. I’ve decided to rely on the compiler to optimize away multiple accesses to the same section of memory.

Thanks,
James Newman

  • 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-05-26T11:15:06+00:00Added an answer on May 26, 2026 at 11:15 am

    You’re attempting something that the compiler optimization does much better than you can do manually. Also, C99, does not have these referencing constructs the way you are attempting to define them in your example—specifically the C++ dereferencing declarations—if you’re also getting really big and deep, I suggest that you rethink your algorithm. If you attempt to introduce a number of temporary variables and more memory around to do referencing you are going to make your life harder.

    For instance if you look at:

    struct some_struct {
            int a;
            struct {
                    float f;
                    double d;
            } s;
    };
    
    struct some_struct array[10000];
    
    int process1(struct some_struct *r) {
    #define R (*r)
            R.a+= 1;
            R.s.f = R.s.f/2;
            R.s.d = ( R.s.d + R.s.f ) * 2;
    }
    
    int process2(struct some_struct *r) {
            r->a+= 1;
            r->s.f = r->s.f/2;
            r->s.d = ( r->s.d + r->s.f ) * 2;
    }
    
    int doit() {
            int i;
            for (i = 0; i < sizeof(array)/sizeof(struct some_struct); i++ ) {
                    struct some_struct *r = &array[i]; /* via reference */
                    process1(r);
                    process2(r);
            }
    }
    

    process1 and process2 generate identical assembly outputs using gcc -O2 on x86_64 platform:

            .file   "foo.c"
            .text
            .p2align 4,,15
            .globl  process1
            .type   process1, @function
    process1:
    .LFB11:
            .cfi_startproc
            movss   .LC0(%rip), %xmm0
            addl    $1, (%rdi)
            mulss   8(%rdi), %xmm0
            movss   %xmm0, 8(%rdi)
            unpcklps        %xmm0, %xmm0
            cvtps2pd        %xmm0, %xmm0
            addsd   16(%rdi), %xmm0
            addsd   %xmm0, %xmm0
            movsd   %xmm0, 16(%rdi)
            ret
            .cfi_endproc
    .LFE11:
            .size   process1, .-process1
            .p2align 4,,15
            .globl  process2
            .type   process2, @function
    process2:
    .LFB12:
            .cfi_startproc
            movss   .LC0(%rip), %xmm0
            addl    $1, (%rdi)
            mulss   8(%rdi), %xmm0
            movss   %xmm0, 8(%rdi)
            unpcklps        %xmm0, %xmm0
            cvtps2pd        %xmm0, %xmm0
            addsd   16(%rdi), %xmm0
            addsd   %xmm0, %xmm0
            movsd   %xmm0, 16(%rdi)
            ret
            .cfi_endproc
    .LFE12:
            .size   process2, .-process2
            .p2align 4,,15
            .globl  doit
            .type   doit, @function
    doit:
    .LFB13:
            .cfi_startproc
            xorl    %edx, %edx
    
            movss   .LC0(%rip), %xmm2
            .p2align 4,,10
            .p2align 3
    .L4:
            leaq    (%rdx,%rdx,2), %rax
            addq    $1, %rdx
            leaq    array(,%rax,8), %rax
            movss   8(%rax), %xmm1
            addl    $2, (%rax)
            mulss   %xmm2, %xmm1
            cmpq    $10000, %rdx
            unpcklps        %xmm1, %xmm1
            cvtps2pd        %xmm1, %xmm0
            mulss   %xmm2, %xmm1
            addsd   16(%rax), %xmm0
            movss   %xmm1, 8(%rax)
            unpcklps        %xmm1, %xmm1
            cvtps2pd        %xmm1, %xmm1
            addsd   %xmm0, %xmm0
            addsd   %xmm1, %xmm0
            addsd   %xmm0, %xmm0
            movsd   %xmm0, 16(%rax)
            jne     .L4
            rep
            ret
            .cfi_endproc
    .LFE13:
            .size   doit, .-doit
            .comm   array,240000,32
            .section        .rodata.cst4,"aM",@progbits,4
            .align 4
    .LC0:
            .long   1056964608
            .ident  "GCC: (GNU) 4.6.1"
            .section        .note.GNU-stack,"",@progbits
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of objects that have QTTime structs as attributes. The objects
I have an array of structs and one of the fields in the struct
I have data represented in CF as an array of structs e.g.: var foo
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
An array is defined of assumed elements like I have array like String[] strArray
Because I would like to make some tests with the libpcap and a small
I have a C function that does some pixel manipulation on a raw 2D
I would like some advice on safe ways to deal with struct's when the
Following situation: I have an array with some constant values, which represent ranges. A
For eg. I have an array of structs 'a' as below: struct mystruct{ int

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.