Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics 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.

Total number of bytes allocated in the below program

Status
Not open for further replies.

embeddedlover

Full Member level 5
Full Member level 5
Joined
Aug 10, 2007
Messages
277
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)));
}
 

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
 

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
 

Yes indeed it is a pointer to an array of four integers.
 

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.
 
Can you explain me the total number of bytes allocated?
 

*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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top