memory allocation
Download
Skip this Video
Download Presentation
Memory allocation

Loading in 2 Seconds...

play fullscreen
1 / 11

Memory allocation - PowerPoint PPT Presentation


  • 62 Views
  • Uploaded on

Memory allocation. CSE 2451 Rong Shi. sizeof. The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine: Returning the byte length of a data type Number of bytes reserved for a structure (user defined type) Byte length of an array.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Memory allocation' - piera


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
memory allocation

Memory allocation

CSE 2451

Rong Shi

sizeof
sizeof
  • Thesizeofunary operatorwill return the number of bytes reserved for a variable or data type.
  • Determine:
    • Returning the byte length of a data type
    • Number of bytes reserved for a structure (user defined type)
    • Byte length of an array
returning the length of a data type
Returning the length of a data type

/* How big is an int?

Most machines size ints as 4 bytes */

main()

{

printf("%d \n", sizeof(int));

}

number of bytes reserved for a structure
Number of bytes reserved for a structure

/* On most machines a struct with two ints is the same size as two ints:8 */

main(){

struct {

inta;

intb;

} TwoInts;

printf("%d \n", sizeof(TwoInts));

}

length of an array
Length of an array

main()

{

char String[20];

printf("%d \n", sizeof String);

printf("%d \n", sizeof(String));

}

/* As a unary operator and not a function, parenthesis are not necessary (if the argument is a variable), but aid readability */

dynamic memory functions
Dynamic memory functions
  • In the stdlib.h library:
  • malloc()
    • Allocate a memory block
  • free()
    • De-allocate memory
  • calloc()
    • Allocate space for an array
  • realloc()
    • Change the size of previously allocated memory
  • Each function is used to initialize a pointer with memory from free store (a section of memory available to all programs)
malloc
malloc
  • The function malloc() will allocate a block of memory that is size bytes large. If the requested memory can be allocated a pointer is returned to the beginning of the memory block.
  • Note: the content of the received block of memory is not initialized.
  • malloc() prototype:
    • void * malloc ( size_t size );
  • Parameters:
    • Size of the memory block in bytes.
  • Return value:
    • If the request is successful then a pointer to the memory block is returned.
    • If the function failed to allocate the requested block of memory, a null pointer is returned.
  • Example
    • http://www.codingunit.com/c-reference-stdlib-h-function-malloc
malloc usage
malloc usage

int*ptr = (int*) malloc( sizeof (int) );

int*ptr = (int*) malloc( sizeof (*ptr) );

calloc
calloc
  • calloc() prototype:
    • void * calloc ( size_tnum, size_t size );
  • Parameters:
    • Number of elements (array) to allocate and the size of elements.
  • Return value:
    • Will return a pointer to the memory block. If the request fails, a NULL pointer is returned.
  • Example:
    • http://www.codingunit.com/c-reference-stdlib-h-function-calloc
static vs dynamic memory
Static vs. Dynamic memory
  • Static arrays – size defined at compile time
    • Memory stored on the stack
    • Stack grows when entering new blocks (branches, loops, functions)
    • Stack shrinks when leaving blocks
    • Obeys scoping rules
  • Dynamic array – size defined at run time
    • Memory stored on the heap
    • Stays available until removed
      • In C – manually with function calls
      • In Java – automatically with garbage collection
  • Why have dynamic memory?
    • Input of unknown size
    • Data structures that require dynamic memory allocation
      • Linked lists, trees, etc.
slide11
free
  • The free function deallocates memory
  • free( ptr);
    • Frees the memory of whatever ptr is pointing at
  • After freeing a pointer, it is a good idea to set it to point to 0
    • This makes it a null pointer
    • Invalid dereferencing is easier to spot with NULL pointers
  • (Some compilers support explicitly setting a pointer to NULL)
ad