Is it ok to declare an int i; then later i++?

i’m trying to figure out how to walk through the memory of an arduino to find fragmentation.
I’ve come across a problem that i’ve never seen before and wanted to ask why it is happening.

This is my code

struct __freelist 
{
    size_t size;
    struct __freelist *next;
}; 

extern struct   __freelist *__flp;
extern uint16_t __heap_start;
extern char *__brkval;
extern char     *__malloc_heap_start;
extern char     *__malloc_heap_end;
extern size_t   __malloc_margin;
extern uint16_t __data_start;
extern uint16_t __data_end;
extern uint16_t __bss_start;
extern uint16_t __bss_end;
int *par[10];

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
// setup for later frag on purpose
int seed = analogRead(A0) + analogRead(A3) + analogRead(A2);
  seed ^= (int)micros();
  randomSeed(seed);
  Serial.begin(115200);
  Serial.println(F("allocate 10 chunks\n"));
  for (int i = 0; i < 10; i++)
  {
    int mSize = random(1, 40) * sizeof(int);
    par[i] = (int*) malloc(mSize); // all different sizes
    *par[i] = 0;
  }
  Serial.println(F("free 3 pointers")); // frag the memory
  free(par[3]);
  free(par[5]);
  free(par[7]);
  par[3] = (int*) malloc(10);
  Serial.println(F("done"));
  CountFrag();
}

void loop() {}
void CountFrag()
{ 
  __freelist *fp;
  Serial.println();
  int i; // if you change to int i = 0; without uncomment the line below it will say two when printed instead of 157.. but it should say three
  //Serial.print(F("starting i is ")); Serial.println(i); //if you uncomment this it works fine.

  for(fp=__flp;fp;fp=fp->next)
  i++;
  Serial.print(F("i is "));Serial.println(i);
}

The problem is when

//Serial.print(F("starting i is ")); Serial.println(i); //if you uncomment this it works fine.

is commented out the serial monitor says

allocate 10 chunks

free 3 pointers
done

i is 155

i is not 155, it should be 3. If you include the

Serial.print(F("starting i is ")); Serial.println(i);

in the sketch the serial monitor will say

allocate 10 chunks

free 3 pointers
done

starting i is 0
i is 3

which is correct. If you change int i; to int i=0 the serial monitor will say

allocate 10 chunks

free 3 pointers
done

i is 2

I want to know what is going on? Is this a overflow? Was the sketch before this one was loaded still in the arduino so it thinks i is the value of the old sketch? Do you have to declare the value of an int when you assign it in the sketch before you use ++?

I don't understand why you think that i in your comment should be three.

  int i; // if you change to int i = 0; without uncomment the line below it will say two when printed instead of 157.. but it should say three

i is a local variable. It is assigned to a memory location that contains some junk from the last time that memory location was used. It is a poor programming practice to not initialize ALL local variables.

 i++;

Increment the junk that was there... Not too useful.

which is correct.

No, it isn't.

After you allocate 10 blocks, free 3, and allocate a block that reuses a freed block, the free list length is 2.

No, it isn't.

After you allocate 10 blocks, free 3, and allocate a block that reuses a freed block, the free list length is 2.

The only part I changed is blow

void CountFrag() { __freelist *fp; Serial.println(); int i=0; // initialize i to 0 for good programming practice Serial.print(F("starting i is ")); Serial.println(i);

for(fp=__flp;fp;fp=fp->next) i++; Serial.print(F("i is "));Serial.println(i); }

the serial monitor

allocate 10 chunks

free 3 pointers done

starting i is 0 i is 3

why does it still say 3?

Also, I understand this

for(fp=__flp;fp;fp=fp->next)

looks for the next available space in the program that isn't being used. Is it possible to do something similar where it looks for the next used space in the program so that It can tell you then length of each used space?

why does it still say 3?

Allocating random size blocks, freeing 3 of them, and then allocating a fixed length block may, or may not, result in a freed block being reused.