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 6364453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:09:55+00:00 2026-05-25T00:09:55+00:00

For this example, I am working with objective-c, but answers from the broader C/C++

  • 0

For this example, I am working with objective-c, but answers from the broader C/C++ community are welcome.

@interface BSWidget : NSObject {
    float tre[3];
}
@property(assign) float* tre;

.

- (void)assignToTre:(float*)triplet {
    tre[0] = triplet[0];
    tre[1] = triplet[1];
    tre[2] = triplet[2];
}

.

- (void)copyToTre:(float*)triplet {
    memcpy(tre, triplet, sizeof(tre) );
}

So between these two approaches, and considering the fact that these setter functions will only generally handle dimensions of 2,3, or 4…

What would be the most efficient approach for this situation?

Will gcc generally reduce these to the same basic operations?

Thanks.

  • 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-25T00:09:56+00:00Added an answer on May 25, 2026 at 12:09 am

    A quick test seems to show that the compiler, when optimising, replaces the memcpy call with the instructions to perform the assignment.

    Disassemble the following code, when compiled unoptimised and with -O2, shows that in the optimised case the testMemcpy function does not contain a call to memcpy.

    struct test src = { .a=1, .b='x' };
    
    void testMemcpy(void)
    {
      struct test *dest = malloc(sizeof(struct test));
      memcpy(dest, &src, sizeof(struct test));
    }
    
    void testAssign(void)
    {
      struct test *dest = malloc(sizeof(struct test));
      *dest = src;
    }
    

    Unoptimised testMemcpy, with a memcpy call as expected

    (gdb) disassemble testMemcpy 
    Dump of assembler code for function testMemcpy:
       0x08048414 <+0>: push   %ebp
       0x08048415 <+1>: mov    %esp,%ebp
       0x08048417 <+3>: sub    $0x28,%esp
       0x0804841a <+6>: movl   $0x8,(%esp)
       0x08048421 <+13>:    call   0x8048350 <malloc@plt>
       0x08048426 <+18>:    mov    %eax,-0xc(%ebp)
       0x08048429 <+21>:    movl   $0x8,0x8(%esp)
       0x08048431 <+29>:    movl   $0x804a018,0x4(%esp)
       0x08048439 <+37>:    mov    -0xc(%ebp),%eax
       0x0804843c <+40>:    mov    %eax,(%esp)
       0x0804843f <+43>:    call   0x8048340 <memcpy@plt>
       0x08048444 <+48>:    leave  
       0x08048445 <+49>:    ret 
    

    Optimised testAssign

    (gdb) disassemble testAssign 
    Dump of assembler code for function testAssign:
       0x080483f0 <+0>: push   %ebp
       0x080483f1 <+1>: mov    %esp,%ebp
       0x080483f3 <+3>: sub    $0x18,%esp
       0x080483f6 <+6>: movl   $0x8,(%esp)
       0x080483fd <+13>:    call   0x804831c <malloc@plt>
       0x08048402 <+18>:    mov    0x804a014,%edx
       0x08048408 <+24>:    mov    0x804a018,%ecx
       0x0804840e <+30>:    mov    %edx,(%eax)
       0x08048410 <+32>:    mov    %ecx,0x4(%eax)
       0x08048413 <+35>:    leave  
       0x08048414 <+36>:    ret   
    

    Optimised testMemcpy does not contain a memcpy call

    (gdb) disassemble testMemcpy 
    Dump of assembler code for function testMemcpy:
       0x08048420 <+0>: push   %ebp
       0x08048421 <+1>: mov    %esp,%ebp
       0x08048423 <+3>: sub    $0x18,%esp
       0x08048426 <+6>: movl   $0x8,(%esp)
       0x0804842d <+13>:    call   0x804831c <malloc@plt>
       0x08048432 <+18>:    mov    0x804a014,%edx
       0x08048438 <+24>:    mov    0x804a018,%ecx
       0x0804843e <+30>:    mov    %edx,(%eax)
       0x08048440 <+32>:    mov    %ecx,0x4(%eax)
       0x08048443 <+35>:    leave  
       0x08048444 <+36>:    ret    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a bigger project atm, but I made this simple example to
I'm working with a Google Maps example code , this : http://www.wolfpil.de/v3/drag-from-outside.html which has
This example working great because here containment is body http://jsfiddle.net/roXon/hMmbK/2/ when i use container
I am working with this example to try and learn Sencha Touch. I have
UPDATE: This is the working example. First we create a class to hold weekday,
I have this simple example I can't seems to get working : MERGE INTO
This example is simplified a bit, but in my ASP.NET web page in my
This example is taken from w3schools . CREATE TABLE Persons ( P_Id int NOT
I have this regex working when I test it in PHP but it doesn't
This is related to another StackOverflow question from a year ago. But, a bit

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.