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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:23:22+00:00 2026-05-17T02:23:22+00:00

is there anyway to do what I do in Lines 2 and 3, with

  • 0

is there anyway to do what I do in Lines 2 and 3, with smth similar to Line 1?

If I just put Line 1, then both “a” and “one.index1” will be pointing to similar location, which I do not want. What I really want is done by Lines 2 and 3. So, is it the only way, or can anyone give better way?

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
 int *index1;
} data;

void doo(int *);

int main(int argc, char *argv[])
{
 int *a = (int *) malloc(10*sizeof(int));
 int i;

 for(i=0; i<10; i++)
 {
  a[i] = 2*i;
 }

 doo(a);

 data one;
 //one.index1 = a;                           // Line 1
 /*******************/
 one.index1 = (int *) malloc(5*sizeof(int)); // Line 2
 for(i=0; i<5; i++) one.index1[i] = a[i];    // Line 3
 /*******************/

 printf("%d\n", one.index1[4]);

 free(a);

 printf("%d\n", one.index1[4]);

 free(one.index1);
 return 0;
}

void doo(int *b)
{
 b = (int *) realloc(b, 5*sizeof(int));
 return;
}

Thanks in advance!

  • 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-17T02:23:23+00:00Added an answer on May 17, 2026 at 2:23 am

    doo will not work as it is. It would need to be:

    void doo(int **b) 
    { 
     *b = (int *) realloc(*b, 5*sizeof(int)); 
     return; 
    } 
    

    and called by

     doo(&a);
    

    Your original version may occasionally accidently work, if the realloc occurs immedaitely after the malloc, so there is space to expand the memory block in place. But that should not be counted on.

    Now, to answer your actual question, Since we are dealing with simple data items (i.e., ints), you can use memmove() or memcpy() to copy them: (memmove is safe if the memory blocks overlap; memcpy isn’t, but that’s not a problem here)

      one.index1 = (int *) malloc(5*sizeof(int)); // Line 2 
      memcpy(one.index1, a, sizeof(int) * 5); 
    

    As for the efficency of memmove/memcpy, that’s pretty much an unknown area. memmove does a bit more range checking then memcpy, so it’ll be a hair slower the memcpy. As far memcpy vs a loop, hard to say. memcpy has a bit more overhead, but it’s called a lot, so compiler vendors have a guy who spends a lot of time making sure it’s a fast as possible.

    Note however, given a small, fixed number of elements to copy, the fastest way to going to be to just copy them directly:

      one.index1 = (int *) malloc(5*sizeof(int)); // Line 2 
      one.index1[0] = a[0];
      one.index1[1] = a[1];
      one.index1[2] = a[2];
      one.index1[3] = a[3];
      one.index1[4] = a[4];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to remove the dotted line rectangle, which indicates the keyboard
Is there any way to get the source line number in Javascript, like __LINE__
Is there anyway to have a sort of virtual static member in C++? For
Is there anyway to configure a WCF service with a failover endpoint if the
Is there anyway in Java to delete data (e.g., a variable value, object) and
Is there anyway to upgrade the installed JRE in the system? We are having
Is there anyway to declare an object of a class before the class is
Is there anyway to combine all resources into a single exe file such as
Is there anyway to build a solution to target 64 bit environment in vs2003?
Is there anyway to have items in an ASP.NET DropDownList have either their Text

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.