Showing posts with label computer class Satellite-Ahmedabad. Show all posts
Showing posts with label computer class Satellite-Ahmedabad. Show all posts

Wednesday, September 29, 2021

Stack vs. Heap

 

In programming language we need memory to store data.  The data stores in Stack and Heap memory according to local and global data.

What is Stack and Heap? Let us see.

Definition:

Stack:

It's a special region of your computer's memory that stores temporary variables created by each function (including the  main() function).

When to use stack?

·         You would use the stack if you know exactly how much data you need to allocate before compile time and it is not too big.

Heap:

The heap is a region of your computer's memory that is not managed automatically.

When to use Heap?

·         You would use the heap if you don't know exactly how much data you will need at run time or if you need to allocate a lot of data.

Which one faster?

The stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it (a pointer/integer is simply incremented or decremented), while the heap has much more complex bookkeeping involved in an allocation or free.

The Heap

The heap is a region of your computer's memory that is not managed automatically for you, and is not as tightly managed by the CPU. It is a more free-floating region of memory (and is larger). To allocate memory on the heap, you must use malloc() or calloc(), which are built-in C functions. Once you have allocated memory on the heap, you are responsible for using free() to deallocate that memory once you don't need it any more. If you fail to do this, your program will have what is known as a memory leak. That is, memory on the heap will still be set aside (and won't be available to other processes). As we will see in the debugging section, there is a tool called valgrind that can help you detect memory leaks.

Unlike the stack, the heap does not have size restrictions on variable size (apart from the obvious physical limitations of your computer). Heap memory is slightly slower to be read from and written to, because one has to use pointers to access memory on the heap. We will talk about pointers shortly.

Unlike the stack, variables created on the heap are accessible by any function, anywhere in your program. Heap variables are essentially global in scope.

Examples

Here is a short program that creates its variables on the stack. It looks like the other programs we have seen so far.

#include <stdio.h>
 
double multiplyByTwo (double input) {
  double twice = input * 2.0;
  return twice;
}
 
int main (int argc, char *argv[])
{
  int age = 30;
  double salary = 12345.67;
  double myList[3] = {1.2, 2.3, 3.4};
 
  printf("double your salary is %.3f\n", multiplyByTwo(salary));
 
  return 0;
}
double your salary is 24691.340

Stack

Heap

·         It's a special region of your computer's memory that stores temporary variables created by each function (including the main() function).

·         The heap is a region of your computer's memory that is not managed automatically for you, and is not as tightly managed by the CPU.

·         Stack is used for static memory allocation

·         Heap is used for dynamic memory allocation

·         The stack is faster

·         Heap is slower

·         Stack stores local variables only

·         Heap stores global variable

  • space is managed efficiently by CPU, memory will not become fragmented

 

  • no guaranteed efficient use of space, memory may become fragmented over time as blocks of memory are allocated, then freed

 

  • limit on stack size (OS-dependent)

 

  • no limit on memory size

 

  • variables cannot be resized

 

  • variables can be resized using realloc()

 

  • when a function exits, all of its variables are popped off of the stack (and hence lost forever).

Heap variables are essentially global in scope.

  •  

 

So, both stack and heap have their own pros and cons. Which one be used in programming, it will depend on requirement of data and scope.

To learn more about Programming

Visit us @ tccicomputercoaching.com

Call us @ 98256 18292.

 

Friday, May 17, 2019

Stack vs. Heap - tccicomputercoaching.com

tccicomputercoaching.com

In programming language we need memory to store data. The data stores in Stack and Heap memory according to local and global data.
What is Stack and Heap? Let us see.
definition:
Stack:
It's a special region of your computer's memory that stores temporary variables created by each function (including the main() function).
When to use stack?
· You would use the stack if you know exactly how much data you need to allocate before compile time and it is not too big.
Heap:
The heap is a region of your computer's memory that is not managed automatically.
When to use Heap?
· You would use the heap if you don't know exactly how much data you will need at run time or if you need to allocate a lot of data.
which one faster?
The stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it (a pointer/integer is simply incremented or decremented), while the heap has much more complex bookkeeping involved in an allocation or free.

The Heap

The heap is a region of your computer's memory that is not managed automatically for you, and is not as tightly managed by the CPU. It is a more free-floating region of memory (and is larger). To allocate memory on the heap, you must use malloc() or calloc(), which are built-in C functions. Once you have allocated memory on the heap, you are responsible for using free() to deallocate that memory once you don't need it any more. If you fail to do this, your program will have what is known as a memory leak. That is, memory on the heap will still be set aside (and won't be available to other processes). As we will see in the debugging section, there is a tool called valgrind that can help you detect memory leaks.
Unlike the stack, the heap does not have size restrictions on variable size (apart from the obvious physical limitations of your computer). Heap memory is slightly slower to be read from and written to, because one has to use pointers to access memory on the heap. We will talk about pointers shortly.
Unlike the stack, variables created on the heap are accessible by any function, anywhere in your program. Heap variables are essentially global in scope.

Examples

Here is a short program that creates its variables on the stack. It looks like the other programs we have seen so far.
#include <stdio.h>

double multiplyByTwo (double input) {
  double twice = input * 2.0;
  return twice;
}

int main (int argc, char *argv[])
{
  int age = 30;
  double salary = 12345.67;
  double myList[3] = {1.2, 2.3, 3.4};

  printf("double your salary is %.3f\n", multiplyByTwo(salary));

  return 0;
}
double your salary is 24691.340
Stack Heap
· It's a special region of your computer's memory that stores temporary variables created by each function (including the main() function). · The heap is a region of your computer's memory that is not managed automatically for you, and is not as tightly managed by the CPU.
  • Stack is used for static memory allocation
  • Heap is used for dynamic memory allocation
  • The stack is faster
  • Heap is slower
  • Stack stores local variables only
  • Heap stores global variable
  • space is managed efficiently by CPU, memory will not become fragmented
  • no guaranteed efficient use of space, memory may become fragmented over time as blocks of memory are allocated, then freed
  • limit on stack size (OS-dependent)
  • no limit on memory size
  • variables cannot be resized
  • variables can be resized using realloc()
  • when a function exits , all of its variables are popped off of the stack (and hence lost forever).
  • Heap variables are essentially global in scope.
So, both stack and heap have their own pros and cons. Which one be used in programming, it will depend on requirement of data and scope.
To learn more about Programming
Visit us @ tccicomputercoaching.com
Call us @ 98256 18292.

Friday, December 28, 2018

Data Types Tccicomputercoaching.com

A data type in a programming language is a set of data with values having predefined characteristics. Examples of data types are: integer, floating point unit number, character, string, and pointer. Usually, a limited number of such data types come built into a language.

When we write source code, compiler have to convert that source code into machine code. The data used in source code have to be differentiating as digit, real number or text (alphabetical) for compiler information. So, data are categorized as per their type. These data type have size, as per this size they have allocated memory.



In computer science and computer programming, a data type or simply type is a classification identifying one of various types of data, such as real, integer or Boolean, that determines the possible values for that type, the operations that can be done on values of that type, the meaning of the data, and the way values of that type can be stored.

Common data types include:

Integers
Booleans
Characters
Floating-point numbers
The type void

The type specifier void indicates that no value is available.

Primitive Types:

A basic type is a data type provided by a programming language as a basic building block. Most languages allow more complicated composite types to be recursively constructed starting from basic types.

A built-in type is a data type for which the programming language provides built-in support.

Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295

Composite types

Composite types are derived from more than one primitive type. This can be done in a number of ways. The ways they are combined are called data structures. Composing a primitive type into a compound type generally results in a new type, e.g. array-of-integer is a different type to integer.

An array stores a number of elements of the same type in a specific order. They are accessed randomly using an integer to specify which element is required (although the elements may be of almost any type). Arrays may be fixed-length or expandable.

A list is similar to an array, but its contents are strung together by a series of references to the next element.

Record (also called tuple or struct) Records are among the simplest data structures. A record is a value that contains other values, typically in fixed number and sequence and typically indexed by names. The elements of records are usually called fields or members.

Union. A union type definition will specify which of a number of permitted primitive types may be stored in its instances, e.g. "float or long integer". Contrast with a record, which could be defined to contain a float and an integer; whereas, in a union, there is only one type allowed at a time.

A tagged union (also called a variant, variant record, discriminated union, or disjoint union) contains an additional field indicating its current type, for enhanced type safety.

A set is an abstract data structure that can store certain values, without any particular order, and no repeated values. Values themselves are not retrieved from sets, rather one tests a value for membership to obtain a Boolean "in" or "not in".

An object contains a number of data fields, like a record, and also a number of subroutines for accessing or modifying them, called methods.

If you like this post then please like and share this post.

Visit us @ tccicomputercoaching.com

Call us @ 98256 18292.

Wednesday, October 3, 2018

Stack vs. Heap

tccicomputercoaching.com

In programming language we need memory to store data. The data stores in Stack and Heap memory according to local and global data.

What is Stack and Heap? Let us see.

definition:

Stack:

It's a special region of your computer's memory that stores temporary variables created by each function (including the main() function).

When to use stack?


  • You would use the stack if you know exactly how much data you need to allocate before compile time and it is not too big.

Heap:

The heap is a region of your computer's memory that is not managed automatically.

When to use Heap?


  • You would use the heap if you don't know exactly how much data you will need at run time or if you need to allocate a lot of data.

which one faster?

The stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it (a pointer/integer is simply incremented or decremented), while the heap has much more complex bookkeeping involved in an allocation or free.

The Heap

The heap is a region of your computer's memory that is not managed automatically for you, and is not as tightly managed by the CPU. It is a more free-floating region of memory (and is larger). To allocate memory on the heap, you must use malloc() or calloc(), which are built-in C functions.
Once you have allocated memory on the heap, you are responsible for using free() to deallocate that memory once you don't need it any more. If you fail to do this, your program will have what is known as a memory leak. That is, memory on the heap will still be set aside (and won't be available to other processes). As we will see in the debugging section, there is a tool called valgrind that can help you detect memory leaks.

Unlike the stack, the heap does not have size restrictions on variable size (apart from the obvious physical limitations of your computer). Heap memory is slightly slower to be read from and written to, because one has to use pointers to access memory on the heap. We will talk about pointers shortly.

Unlike the stack, variables created on the heap are accessible by any function, anywhere in your program. Heap variables are essentially global in scope.

Examples

Here is a short program that creates its variables on the stack. It looks like the other programs we have seen so far.

#include <stdio.h>

double multiplyByTwo (double input) {
  double twice = input * 2.0;
  return twice;
}

int main (int argc, char *argv[])
{
  int age = 30;
  double salary = 12345.67;
  double myList[3] = {1.2, 2.3, 3.4};

  printf("double your salary is %.3f\n", multiplyByTwo(salary));

  return 0;
}
double your salary is 24691.340

Stack Heap
· It's a special region of your computer's memory that stores temporary variables created by each function (including the main() function). · The heap is a region of your computer's memory that is not managed automatically for you, and is not as tightly managed by the CPU.
  • Stack is used for static memory allocation
  • Heap is used for dynamic memory allocation
  • The stack is faster
  • Heap is slower
  • Stack stores local variables only
  • Heap stores global variable
  • space is managed efficiently by CPU, memory will not become fragmented
  • no guaranteed efficient use of space, memory may become fragmented over time as blocks of memory are allocated, then freed
  • limit on stack size (OS-dependent)
  • no limit on memory size
  • variables cannot be resized
  • variables can be resized using realloc()
  • when a function exits , all of its variables are popped off of the stack (and hence lost forever).
Heap variables are essentially global in scope.

So, both stack and heap have their own pros and cons. Which one be used in programming, it will depend on requirement of data and scope.

To learn more about Programming

Visit us @ tccicomputercoaching.com

Call us @ 98256 18292.

Monday, August 20, 2018

The postgraduate diploma in computer application ( PGDCA ) is a one-year course open to all students who have completed undergraduate studies and have an interest in computer applications and programming.

TCCI-Tririd Computer Coaching Institute is focused on providing Quality education with practical sessions. Satisfaction of our student is our priority.
Faculty of TCCI provides teaches to BCA college students offline and online both. Our experienced faculty teaches the following subjects :
Computer fundamental and Networking, Programming in C , C++, JAVA, MS Office, Web Design, HTML, CSS, PHP, JavaScript Oracle-VB, MS-Dos, Windows as Operating System - System Analysis & Data Processing .
To get admission at TCCI call us @ 98256 18292
Visit us @ http://tccicomputercoaching.com/contact/