View Single Post
  #1   Spotlight this post!  
Unread 03-16-2011, 08:57 PM
davidthefat davidthefat is offline
Alumni
AKA: David Yoon
FRC #0589 (Falkons)
Team Role: Alumni
 
Join Date: Jan 2011
Rookie Year: 2010
Location: California
Posts: 792
davidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud of
I Have A Question About Pointers

Now this is where I have not been able to wrap my head around: do pointers take up space in the memory heap? I know that pointers just reference to a specific memory "slot", but is it a "physical" object in the memory that takes up space? Or is it just what the compiler sees and uses to get the data that the pointer references to? Also does this mean an array is also like a pointer, just references to multiple objects? I am not sure if I am asking the questions right.

I have been using C++ for years and I have not thought about this until recently. I just assumed pointers were just an object that references to an objects location; are they like "#define"s where only the compiler uses value?
Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
/*
+---+--------------------+
| r |    Register(s)     |
+---+--------------------+
| a |   %eax, %ax, %al   |
| b |   %ebx, %bx, %bl   |
| c |   %ecx, %cx, %cl   |
| d |   %edx, %dx, %dl   |
| S |   %esi, %si        |
| D |   %edi, %di        |
+---+--------------------+
*/
uint16_t assemblyAddition(uint16_t input1, uint16_t input2);

int main()
{
    uint16_t *input1;//This should not take up any space in memory right?
    input1 = (uint16_t*)calloc(2, sizeof(uint16_t));//This allocates a piece of memory for 2 shorts (16 bits * 2 == 32 bits) and initializes it. 
    //Now the pointer points to that memory region
    if(input1 == NULL)
    {
        cout << "YOU DUN GOOFD!";
        return 1;
    }
    input1[0] = 0x0;//This sets the referenced object (that was allocated above) to 0
    input1[1] = 0x0;
    cout << "Number 1: ";
    cin >> input1[0];
    cout << endl << "Number 2: ";
    cin >> input1[1];
    cout << endl <<"Result : " << assemblyAddition(input1[0], input1[1]) << endl;
    free(input1);//This allows the allocated memory to be used again, but does it set everything to 0?
    return 0;
}

uint16_t assemblyAddition(uint16_t input1, uint16_t input2)
{
    uint16_t result = 0x0;
    asm("addw %%bx, %%ax;" : "=a"(result) : "a" (input1), "b" (input2));
    return result;
}

edit: since pointers have a datatype, does it take up as much space as the datatype (for my example, 32 bits? 16 * 2 == 32)
I thought they just stored the memory slot number, so I am just confusing myself.
__________________
Do not say what can or cannot be done, but, instead, say what must be done for the task at hand must be accomplished.

Last edited by davidthefat : 03-16-2011 at 09:04 PM.
Reply With Quote