Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

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
279
Helped
46
Reputation
92
Reaction score
38
Trophy points
1,308
Activity points
3,155
#define ROWS 2
#define COLUMNS 4
int main()
{
int(*p)[COLUMNS];
p = (int(*)[COLUMNS])malloc(ROWS*sizeof(*p)));
}
 

kalbun

Full Member level 2
Joined
Oct 2, 2010
Messages
134
Helped
37
Reputation
74
Reaction score
36
Trophy points
1,318
Location
Firenze, Italy
Activity points
2,114
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
 

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,021
Reputation
4,158
Reaction score
2,031
Trophy points
1,393
Location
Greece
Activity points
64,377
int (*p)[COLUMNS] is not a valid C statement
Actually it is, int (*p)[4] is a pointer to an array of four integers while int *p[4] is an array of four pointers to integer

Alex
 

embeddedlover

Full Member level 5
Joined
Aug 10, 2007
Messages
279
Helped
46
Reputation
92
Reaction score
38
Trophy points
1,308
Activity points
3,155
Yes indeed it is a pointer to an array of four integers.
 

kalbun

Full Member level 2
Joined
Oct 2, 2010
Messages
134
Helped
37
Reputation
74
Reaction score
36
Trophy points
1,318
Location
Firenze, Italy
Activity points
2,114
Indeed!
My fault... I tried to compile the code on my compiler but did a typo.

Anyway, GCC reports ROWS*sizeof(*p) = 0x10 and this seems logical as *p occupies four integers.
 

embeddedlover

Full Member level 5
Joined
Aug 10, 2007
Messages
279
Helped
46
Reputation
92
Reaction score
38
Trophy points
1,308
Activity points
3,155
Can you explain me the total number of bytes allocated?
 

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,021
Reputation
4,158
Reaction score
2,031
Trophy points
1,393
Location
Greece
Activity points
64,377
*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
 

embeddedlover

Full Member level 5
Joined
Aug 10, 2007
Messages
279
Helped
46
Reputation
92
Reaction score
38
Trophy points
1,308
Activity points
3,155
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.
 

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,021
Reputation
4,158
Reaction score
2,031
Trophy points
1,393
Location
Greece
Activity points
64,377
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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top