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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:36:26+00:00 2026-05-17T01:36:26+00:00

I want to move a large chunk of data i’ve got in my memory.

  • 0

I want to move a large chunk of data i’ve got in my memory. Unfortunately this data is saved as an array, and i cannot change that. I can’t use circular arrays, because the same memory is also used by a couple of fortran methods i do not want to change. On top of it, the arrays are accessed very very often in between the movement. So i can do this:

int *array = (int*) malloc(sizeof(int)*5);
int *array2=NULL;
//Now i want to move my data one step to the left
array=(int*) realloc(array,6);
array2=array+1;
memmove(array,array2,5*sizeof(int));
array=(int*) realloc(array,5);

This should work fine, but it looks so wasteful ;). If i could tell my compiler to take away data on the left side of a shrinking array, my data would sort of creep through the memory, but i wouldn’t have to do any copying. Like this:

int *array = (int*) malloc(sizeof(int)*5);
//Now i want to move my data one step to the left
array=(int*) realloc(array,6);
array=(int*) realloc_using_right_part_of_the_array(array,5);

So basically i want to finish with a pointer to array+1, with the 4 byte left of it freed. I played around with free() and malloc() but it didn’t work…
I’m aware that the realloc could also result in a memcpy call, but not everytime! So it could be faster, couldn’t it?

  • 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-17T01:36:26+00:00Added an answer on May 17, 2026 at 1:36 am

    No. There is no way to give back the lower part of the memory you allocated. Also, your original code is wrong, since you’re copying indeterminate memory.

    int *array = (int*) malloc(sizeof(int)*5);
    // Fill memory:
    // array - {'J', 'o', h', 'n', '\0'}; 
    int *array2=NULL;
    //Now i want to move my data one step to the left
    array=(int*) realloc(array,6);
    // array - {'J', 'o', h', 'n', '\0', X};
    array2=array+1;
    // array2 pointer to 'o of array.
    memmove(array,array2,5*sizeof(int));
    // This copies the indeterminate x:
    // array - {'o', h', 'n', '\0', X, X}
    array=(int*) realloc(array,5);
    // array - {'o', h', 'n', '\0', X}
    

    X means indeterminate.

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

Sidebar

Related Questions

We have an application that exports very large data files to a windows network
I work for a company that develops a very large scale data based application.
I have a large project and want to move to the Yii Framework for
I have a very large dataset - millions of records - that I want
I have a large application in a production environment that I'm trying to move
I want to bulk move a large number of files from one directory to
Suppose I have a function that will return a large data structure, with the
http://jsfiddle.net/NCt4D/ I want it to move (the image is large), but it dont work.
I have a large amount of data to move using two PHP scripts: one
I want to move an image within a circle with my touch on move.

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.