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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:27:39+00:00 2026-06-09T05:27:39+00:00

I want to copy some data stored in a struct to an other. Does

  • 0

I want to copy some data stored in a struct to an other. Does the code below work?? Is it recommended or not?

#define SIZE 100
struct {
int *a;
int *b;
} Test;
Test t1;
t1.a = malloc(SIZE);
t1.b = malloc(SIZE);

Test t2;
memcpy(t2,t1,sizeof(Test));
  • 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-09T05:27:40+00:00Added an answer on June 9, 2026 at 5:27 am

    Whether it works depends on what you intend it to do. It copies the bits from t1 to t2, including padding, but of course it copies the pointers, not the pointed-to values.

    If you don’t care about the padding bits – and why should you – a simple assignment

    Test t2 = t1;
    

    is all you need to copy the pointers.

    If you want the pointed-to values duplicated and copied, neither your code nor simple assignment works.

    To copy the pointed-to memory blocks, first of all, you must know their size. There is no (portable) way to find out the size of the pointed-to memory block from the pointer.

    If the size is given by a #define, you can of course reuse that, otherwise you need to store the sizes of the allocated blocks somewhere.

    But since the newly allocated memory blocks have different addresses than the blocks to be copied, we need not copy the pointer vlaues from t1 to t2 at all.

    Test t2;
    t2.a = malloc(SIZE);  /* be aware that this is bytes, not number of ints */
    t2.b = malloc(SIZE);
    if (t2.a == NULL || t2.b == NULL) {
        /* malloc failed, exit, or clean up if possible */
        fprintf(stderr,"Allocation failure, exiting\n");
        exit(EXIT_FAILURE);
    }
    /* malloc was successful in both cases, copy memory */
    memcpy(t2.a, t1.a, SIZE);
    memcpy(t2.b, t1.b, SIZE);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file, where I want to copy some data of the file
I want to copy my NSDictionary's data into plist and also edit some data
I Want to Copy some Data between two Data Bases, so I use an
I have some data stored as ArrayList . And when I want to backup
I want to copy some functions from OpenCV library to my embedded application. Rewriting
Is there some additional logic performed by Associate() operation? I want to programmatically copy
I wrote some functional VBA: Sheets(Src).Range(A2:A9).Copy Destination:=Sheets(Dest).Range(A2) I want to extract the source range
For some simple Windows batch script, I want to temporarily create a copy of
i want copy pdf file from application path(/data/data/package name) to sdcard.for that i have
I need to copy some data from one table to another in Oracle, while

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.