{"id":2750,"date":"2024-05-10T11:22:49","date_gmt":"2024-05-10T11:22:49","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2750"},"modified":"2024-05-10T11:22:49","modified_gmt":"2024-05-10T11:22:49","slug":"near-far-and-huge-pointers-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/near-far-and-huge-pointers-in-c\/","title":{"rendered":"Near, Far And Huge Pointers In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#near-far-and-huge-pointers-in-c\">Near, Far And Huge Pointers In C<\/a><\/li><li ><a href=\"#1-near-pointer\">1. Near Pointer<\/a><\/li><li ><a href=\"#2-far-pointer\">2. Far Pointer<\/a><\/li><li ><a href=\"#3-huge-pointer\">3. Huge Pointer<\/a><\/li><li ><a href=\"#difference-between-far-pointer-and-near-pointer\">Difference between Far Pointer and Near Pointer<\/a><\/li><li ><a href=\"#the-difference-between-a-far-pointer-and-a-huge-pointer\">The difference between a Far Pointer and a Huge Pointer<\/a><\/li><li ><a href=\"#faq-near-far-and-huge-pointers-in-c\">FAQ- Near, Far, And Huge Pointers In C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"near-far-and-huge-pointers-in-c\">Near, Far And Huge Pointers In C<\/h2>\n\n\n\n<p>In the past, older Intel processors had 16-bit registers, which couldn&#8217;t hold the full memory address because the address bus was wider. To work around this limitation, memory was divided into 64-kilobyte segments. Concepts like near pointers, far pointers, and huge pointers were used in the C programming language to handle these segmented memory models. However, these concepts are mostly outdated and not relevant in modern computing environments.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-near-pointer\">1. Near Pointer<\/h2>\n\n\n\n<p>A &#8220;Near Pointer&#8221; on a 16-bit machine can only store 16-bit addresses, limiting it to accessing memory within the current 64-kilobyte segment. This means it can only reach the first 64 kilobytes of data in the memory.<\/p>\n\n\n\n<p>The size of the near pointer is 2 bites.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pointer_type near * pointer_name;\n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\/\/ C Program to demonstrate the use of near pointer\n#include &lt;stdio.h&gt;\n \nint main()\n{\n    \/\/ declaring a near pointer\n    int near* ptr;\n \n    \/\/ size of the near pointer\n    printf(\"Size of Near Pointer: %d bytes\", sizeof(ptr));\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Size of Near Pointer: 2 bytes\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-far-pointer\">2. Far Pointer<\/h2>\n\n\n\n<p>A &#8220;far pointer&#8221; uses two 16-bit registers to store memory addresses, allowing it to access memory outside the current segment. The compiler allocates one register to store the segment address and another for the offset within the current segment. To get the actual address, the offset is added to the shifted segment address.<\/p>\n\n\n\n<p>However, in a far pointer, you can&#8217;t change the segment part by incrementing or decrementing it; this only affects the offset. The size of a far pointer is 4 bytes.<\/p>\n\n\n\n<p>A challenge with far pointers is that different pointer values may actually point to the same address, making pointer comparisons less reliable.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pointer_type far * pointer_name;\n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ C Program to find the size of far pointer\n#include &lt;stdio.h&gt;\n \nint main()\n{\n    \/\/ declaring far pointer\n    int far* ptr;\n \n    \/\/ Size of far pointer\n    printf(\"Size of Far Pointer: %d bytes\", sizeof(ptr));\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Size of far pointer: 4 bytes<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-huge-pointer\">3. Huge Pointer<\/h2>\n\n\n\n<p>A &#8220;huge pointer&#8221; also uses two registers to store addresses, like a far pointer. However, there are key differences:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>In a huge pointer, both the offset and the segment address can be changed, allowing you to jump from one memory segment to another.<\/li>\n\n\n\n<li>Huge pointers always compare the absolute addresses, which means you can perform relational operations on them with confidence.<\/li>\n\n\n\n<li>The size of a huge pointer is 4 bytes, just like a far pointer.<\/li>\n<\/ol>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pointer_type huge * pointer_name;\n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ C Program to find the size of the huge pointer\n#include &lt;stdio.h&gt;\n \nint main()\n{\n    \/\/ declaring the huge pointer\n    int huge* ptr;\n \n    \/\/ size of huge pointer\n    printf(\"Size of the Huge Pointer: %d bytes\",\n           sizeof(ptr));\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Size of the Huge Pointer: 4 bytes\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"difference-between-far-pointer-and-near-pointer\">Difference between Far Pointer and Near Pointer<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A &#8220;far pointer&#8221; can store addresses for any location in RAM, while a &#8220;near pointer&#8221; is limited to the first 64 kilobytes of memory.<\/li>\n\n\n\n<li>A far pointer uses two registers to hold segment and offset addresses separately, while a near pointer uses just one register.<\/li>\n\n\n\n<li>The size of a far pointer is 4 bytes, whereas a near pointer is 2 bytes in size.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-difference-between-a-far-pointer-and-a-huge-pointer\">The difference between a Far Pointer and a Huge Pointer<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A &#8220;far pointer&#8221; can&#8217;t move between different memory segments; it&#8217;s limited to a single segment.<\/li>\n\n\n\n<li>&#8220;Huge pointers&#8221; can move between multiple memory segments.<\/li>\n\n\n\n<li>Two different far pointer values can point to the same memory location, while this is not possible with huge pointers<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-near-far-and-huge-pointers-in-c\">FAQ- Near, Far, And Huge Pointers In C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1697543524742\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What are near-far and huge pointers in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.<br \/>A &#8220;near pointer&#8221; doesn&#8217;t have a separate selector.<br \/>&#8220;Huge pointers&#8221; have a selector. When you do pointer arithmetic with a far pointer, the selector isn&#8217;t changed. However, with huge pointers, the selector can be modified.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697543535708\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. How many bytes are occupied by near far and huge pointers?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <strong>Near Pointers<\/strong>: 2 bytes.<br \/><strong>Far Pointers<\/strong>: 4 bytes (16-bit segment + 16-bit offset).<br \/><strong>Huge Pointers<\/strong>: 4 bytes (used for extended memory access).<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697543545794\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. Are pointers 4 or 8 bytes?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In modern computing environments, the size of pointers does indeed depend on the target architecture.<br \/>On 64-bit architectures, pointers are typically 8 bytes.<br \/>On 32-bit architectures, pointers are usually 4 bytes<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Near, Far And Huge Pointers In C In the past, older Intel processors had 16-bit registers, which couldn&#8217;t hold the full memory address because the address bus was wider. To work around this limitation, memory was divided into 64-kilobyte segments. Concepts like near pointers, far pointers, and huge pointers were used in the C programming &#8230; <a title=\"Near, Far And Huge Pointers In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/near-far-and-huge-pointers-in-c\/\" aria-label=\"More on Near, Far And Huge Pointers In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5381,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[473,472],"class_list":["post-2750","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-far-and-huge-pointers-in-c","tag-near","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2750","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2750"}],"version-history":[{"count":9,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2750\/revisions"}],"predecessor-version":[{"id":10730,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2750\/revisions\/10730"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5381"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}