This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
Malloc issue
- From: Hard Maker <hardmaker at gmail dot com>
- To: newlib at sourceware dot org
- Date: Sat, 18 Feb 2012 14:55:11 -0300
- Subject: Malloc issue
- Authentication-results: mr.google.com; spf=pass (google.com: domain of hardmaker@gmail.com designates 10.101.93.2 as permitted sender) smtp.mail=hardmaker@gmail.com; dkim=pass header.i=hardmaker@gmail.com
Hi list,
I'm working with newlib in a cortex-m3 microcontroller (developing and
study how work), debugging a little aplicatino I found the malloc
assigments no depend of the type of data used. I'm not shure how explain
this in english, but with this code is more simple:
#include <stdio.h>
#include "kit.h"
#include "lcd_drv.h"
#include <malloc.h>
int main(int argc, char *argv[]){
char *m1;
short *m2;
int *m3;
float *m4;
long double *m5;
m1 = (char *)malloc(sizeof(char)); // 1 Byte
m2 = (short *)malloc(sizeof(short)); // 2 Byte
m3 = (int *)malloc(sizeof(int)); // 4 Bytes
m4 = (float *)malloc(sizeof(float)); // 4 Bytes
m5 = (long double *)malloc(sizeof(long double)); // 8 bytes
*m1=4;
*m2=4;
*m3=4;
*m4=4;
*m5=4;
while(1);
return 0;
}
All address assigned to m1, m2, m3, m4 and m5 pointers have 16 bytes of
distance. The values show in debugger after last malloc are:
m1 = 0x20005d90
m2 = 0x20005da0
m3 = 0x20005db0
m4 = 0x20005dc0
m5 = 0x20005dd0
My question is: Why all pointers have 16 bytes of distance if they point
to diferentes types of data? Searching about malloc aligment I found the
default value is 8, there are some relationship?
I'm using newlib version 1.19.0
thank's a lot.
Sergio