Total number of bytes allocated in the below program

Status
Not open for further replies.

embeddedlover

Full Member level 5
Joined
Aug 10, 2007
Messages
277
Helped
46
Reputation
92
Reaction score
38
Trophy points
1,308
Visit site
Activity points
3,155
#define ROWS 2
#define COLUMNS 4
int main()
{
int(*p)[COLUMNS];
p = (int(*)[COLUMNS])malloc(ROWS*sizeof(*p)));
}
 

hmm... this code does not seem to have much sense.
int (*p)[COLUMNS] is not a valid C statement, maybe you mean int* p[COLUMNS], but I can't understand the next statement.
What are you trying to do?
I suppose you want to allocate a dynamic array of ROWS*COLUMNS integers, in this case malloc will allocate ROWS*COLUMNS*2, provided int has size 2 bytes in your compiler
 

*p points to the array of four integers which is 4*2byte=8byte (assuming that the integer size is 2 byte).
ROWS*sizeof(*p) is 2*8byte=16 byte (hex 0x10 as kalbun said )

Alex
 
Your byte size seems to be convincing but i referred in one site and it was mentioned 14 bytes.
Even me tried to compile in Dev C++ on windows and giving me 16 bytes.
 

I don't see a way to get a result of 14 byte, did they explain the calculation?
The allocated size is 2 * 4 * integer_size which is 8 * integer size, a result of 14 byte would mean an integer size of 1.75 bytes and doesn't make sense unless the array size was different.
The only alternative solution would be if integer was represented with 4 bytes (this it true in 32bit ARM based mcu in keil uVision ), in that case the result would be 8*4bytes=32 bytes.

Alex
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…