Does malloc() allocate a contiguous block of memory?

Posted by user66854 on Stack Overflow See other posts from Stack Overflow or by user66854
Published on 2009-03-09T07:16:13Z Indexed on 2010/04/04 8:43 UTC
Read the original article Hit count: 239

Filed under:
|
|
|

I have a piece of code written by a very old school programmer :-) . it goes something like this

typedef struct ts_request
{ 
  ts_request_buffer_header_def header; 
  char                         package[1]; 
} ts_request_def; 

ts_request_buffer_def* request_buffer = 
malloc(sizeof(ts_request_def) + (2 * 1024 * 1024));

the programmer basically is working on a buffer overflow concept. I know the code looks dodgy. so my questions are:

  1. Does malloc always allocate contiguous block of memory ?. because in this code if the blocks are not contiguous , the code will fail big time

  2. Doing free(request_buffer) , will it free all the bytes allocated by malloc i.e sizeof(ts_request_def) + (2 * 1024 * 1024), or only the bytes of the size of the structure sizeof(ts_request_def)

  3. Do you see any evident problems with this approach , i need to discuss this with my boss and would like to point out any loopholes with this approach

© Stack Overflow or respective owner

Related posts about malloc

Related posts about c