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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:36:19+00:00 2026-06-11T11:36:19+00:00

EDIT : I substantially changed my code to resemble the actual structure of the

  • 0

EDIT: I substantially changed my code to resemble the actual structure of the original code (which I can’t post because I would have to write pages and pages explaining what everything is).


I’ve been struggling with this problem. I have six int arrays, ID1, ID2 and ID3, and array1, array2 and array3, where the names with the same indexes have the same length (len1, len2 and len3, respectively). The idea is that I’m re-creating them in a for loop, because the length of these arrays changes inside it. I’m doing this as follows:

/* Before entering the loop, I define these three arrays, where 
   len1, len2 and len3 are all equal to 100. */

int i,S,len1,len2,len3;
len1=100;
len2=100;
len3=100;
int* ID1; 
int* ID2; 
int* ID3;
ID1=(int*) malloc((len1)*sizeof(int));
ID2=(int*) malloc((len2)*sizeof(int));
ID3=(int*) malloc((len3)*sizeof(int));
/* Then I fill the arrays with values from a loop which is not relevant 
   to my problem. Let's just fill them with a simple for loop: */
for(i=0;i<len1;i++){
    ID1[i]=i;
    ID2[i]=i;
    ID3[i]=i;
}
/* Now I enter a loop, in which I create 3 more arrays named array1, array2 
   and array3: */
for(S=98;S>=0;S--){
 printf("ID3[99]:%d (before)\n",ID3[99]); // 1st call to printf
 int* array1;
 int* array2;
 int* array3;
 array1=(int*) malloc((len1)*sizeof(int));
 array2=(int*) malloc((len2)*sizeof(int));
 array3=(int*) malloc((len3)*sizeof(int));
 printf("ID3[99]:%d (after)\n",ID3[99]);  // 2nd call to printf
 /* I do more stuff here. Here len1, len2 and len3 changes, so I have to 
    re-create the "ID" arrays and the ones named "array". The idea is to fill 
    the new "ID1", "ID2" and "ID3" arrays with the values of another set of 
    arrays that I filled with values from complex calculations named 
    "AnotherArray1", "AnotherArray2" and "AnotherArray3". 
    The lenghts are always > 100.*/
 free(ID1);
 free(ID2);
 free(ID3);
 free(array1);
 free(array2);
 free(array3);
 int* ID1;
 int* ID2;
 int* ID3;
 ID1=(int*) malloc((len1)*sizeof(int));
 ID2=(int*) malloc((len2)*sizeof(int));
 ID3=(int*) malloc((len3)*sizeof(int));
 for(i=0;i<len1;i++){
    ID1[i]=AnotherArray1[i];
 }
 for(i=0;i<len2;i++){
    ID2[i]=AnotherArray2[i];
 }
 for(i=0;i<len3;i++){
    ID3[i]=AnotherArray3[i];
 }
 /* Finally, I need to free the "AnotherArray" arrays, because in the loop 
    I need to create them again and do some complex calculations with the 
    "ID" arrays. */
 free(AnotherArray1);
 free(AnotherArray2);
 free(AnotherArray3); 
 printf("ID3[99]:%d (before, after starting the loop again)\n",ID3[99]); // 3rd call to printf
}

The problem is that when I do this, the 3rd call of the printf function is different from the 1st and 2nd calls (i.e., when the loop starts again, the value of ID3 in some of its elements suddenly changes!). I really don’t know what’s going on here…any advice? If you need more details, please let me know.

  • 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-11T11:36:20+00:00Added an answer on June 11, 2026 at 11:36 am

    /* (array is created before the loop starts) */

    printf(“array[0]: %d (before)”,array[0]);

    printf(“array[0]: %d (after)”,array[0]);

    int* array;
    array=(int*) malloc((len1)*sizeof(int));

    /* Fill array with some values, and the loop begins again */

    I would note that you are allocating a new chunk of memory for the array each time. Since it is a different chunk of memory, it is likely to have different (garbage) values. I suspect that it would be a good idea to free(array) before mallocing it again.

    There is a code smell in there

    len2=100;
    len3=100;
    int* ID1; 
    int* ID2; 
    int* ID3;
    ID1=(int*) malloc((len1)*sizeof(int));
    ID2=(int*) malloc((len2)*sizeof(int));
    ID3=(int*) malloc((len3)*sizeof(int));
    
    ... 
    
    int* ID1;
    int* ID2;
    int* ID3;
    ID1=(int*) malloc((len1)*sizeof(int));
    ID2=(int*) malloc((len2)*sizeof(int));
    ID3=(int*) malloc((len3)*sizeof(int));
    

    You are declaring ID1, ID2 and ID3 a second time.
    That is likely to confuse the issue.
    It is effectivly a second set of variables. That may be the source of your problem.

    I would note that the original allocation never gets freed.

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

Sidebar

Related Questions

We have a SharePoint 2007 deployment which will have a substantially large document library.
I have a queue in which I can enqueue different threads, so I can
Edit 1 begins The behaviour of the original post applies only to Windows XP
EDIT: See my answer below--> I am wanting to have a view that when
Edit (updated question) I have a simple C program: // it is not important
EDIT : It turned out that this can only be done through an external
EDIT following resolution, this has been substantially edited to dump irrelevant detail and explain
Edit: I would like to model a 1 to 0:1 relationship between User and
--EDIT-- I believe this is a valid question that may have multiple answers (as
I have the strangest thing going on here which I'm sure there's a perfectly

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.