avr-libc-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-libc-dev] realloc shrinking corrupts last two bytes of shrunk chunk


From: Francesco Potortì
Subject: [avr-libc-dev] realloc shrinking corrupts last two bytes of shrunk chunk
Date: Sun, 01 Dec 2013 00:40:06 +0100

This is a demo "sketch" written for Arduino:

/*
 * Output should be:
 *  0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55
 *  0x55 0x55 0x55 0x55 0x55 0x55
 *
 * Output is:
 *  0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55
 *  0x55 0x55 0x55 0x55 0x6 0x0
 *
 */

void setup()
{
  char hexstring[3];
  Serial.begin(115200);

  int len = 14;

  byte *ptr = (byte *)malloc(len);
  memset(ptr, 0x55, len);
  for (byte i=0; i<len; ++i)
    {
      Serial.print(" 0x");
      Serial.print(itoa(ptr[i], hexstring, 16));
    }
  Serial.println("");

  ptr = (byte *)realloc(ptr, len-8);
  for (byte i=0; i<len-8; ++i)
    {
      Serial.print(" 0x");
      Serial.print(itoa(ptr[i], hexstring, 16));
    }
  Serial.println("");
}

void loop()
{
}



reply via email to

[Prev in Thread] Current Thread [Next in Thread]