Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   I Have A Question About Pointers (http://www.chiefdelphi.com/forums/showthread.php?t=93687)

davidthefat 16-03-2011 20:57

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.

EricVanWyk 16-03-2011 21:04

Re: I Have A Question About Pointers
 
A pointer takes up space in its own right, usually 32 or 64 bits depending on the memory map. It isn't just a compiler construct - it gets used at run time. This lets you do pointer arithmetic.

davidthefat 16-03-2011 21:08

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by EricVanWyk (Post 1040926)
A pointer takes up space in its own right, usually 32 or 64 bits depending on the memory map. It isn't just a compiler construct - it gets used at run time. This lets you do pointer arithmetic.

So my example uses 64 bits of memory from just the variables? 32 from the pointer it self, 16 from short, and another 16 from the other short. I don't know how much the streams use up.

mikets 16-03-2011 21:09

Re: I Have A Question About Pointers
 
If it is a variable, it takes up space in memory. A char variable takes 1 byte, a short variable takes 2 bytes, a long variable takes 4 bytes, an int on a 32-bit machine takes 4 bytes. A pointer variable on a 32-bit machine also takes 4 bytes.
Code:

int A = 40;
long B = 50;
int *ptrToA = &A;
long *ptrToB = &B;
 
addr          value      variable name
====          =====      =============
        +----------------+
1000    |      40      |  A
        +----------------+
1004    |      50      |  B
        +----------------+
1008    |      1000      |  ptrToA
        +----------------+
1012    |      1004      |  ptrToB
        +----------------+
        |      ....      |


davidthefat 16-03-2011 21:20

Re: I Have A Question About Pointers
 
So the cRio has 32 General Purpose Registers and 32 Floating Point registers? Since cRio apparently has a PPC core.

On the other hand, I am impressed that the mars rovers used VxWorks as their OS.

Does the cRio have a 64 bit structure?

So if a pointer does take up that much space, it seems like a structure would be more efficient for variables under 32 bits than using singletons.

Greg McKaskle 16-03-2011 22:12

Re: I Have A Question About Pointers
 
Predicting how much memory is used for a given bit of code requires quite a bit of knowledge about the machine architecture. One of the most common tradeoffs in electronics and computer is "space for speed". This is true in your code, but often true in the computer architecture itself.

As an example of this, how much space is taken by
struct { int8 a; int16 b; }[1000]?

The naive answer is (1+2)*1000, but on many architectures, alignment rules force the int16 to be on a two byte alignment and for the struct to actually take four bytes. Additionally, when you malloc you'll find both internal and external fragmentation. You will often find memory managers making all sorts of tricks to avoid locks, to speed up contention for the free list, etc.

Additionally, while a pointer represents space, if they are in a local scope they may actually be just a register and use no memory. This is also true of ints, floats, and any local of sufficiently small scope.

For future reference, if you use sizeof(int*) or any other type, that is a good estimate for memory usage, but all of the factors listed above and many more make an impact.

Greg McKaskle

wireties 17-03-2011 08:04

Re: I Have A Question About Pointers
 
In your example input1 DOES take up space in memory (whether or not you call the calloc), 4 bytes on the stack of the vxWorks task in which it executes. After the calloc call, input1 contains an address that points to a location in the heap. The space used in the heap is probably 4 bytes but could be more. For example vxWorks has an option to insert boundaries around heap allocations (where it inserts a certain pattern) to assist in memory analysis. Greg's observations about alignment also play a role.

The data type referred to by the pointer does NOT changed the amount of memory the pointer itself consumes. But it does determine how pointer arithmetic increments/decrements. For your example, input1++ increases the address pointed to by 4 bytes. If input1 were declared to pointer to a double, it would increase by 8 bytes and so on.

I think our version of the PowerPC is a 32-bit machine thus integers and pointers are 4-bytes.

HTH

davidthefat 04-04-2011 04:59

Re: I Have A Question About Pointers
 
Oh man, I have been reading this assembly book. EVERYTHING makes so much more sense to me now.

davidthefat 05-04-2011 18:50

Re: I Have A Question About Pointers
 
Is it safe to assume that it runs in protected mode? Sorry for the double post, but I can't edit my latest post. Now is anyone else thinking of writing the code for next year in assembly? Obviously not the whole thing, but some of the calculations and stuff like that would not be too bad. Well, if you are already doing pointer arithmetics, it can be replaced with assembly code.

mikets 05-04-2011 18:53

Re: I Have A Question About Pointers
 
Are you talking about the PowerPC processor in the cRIO or are you talking about Intel processor? Your initial post listed all the Intel processor registers. So I am not sure which one you are asking?

demosthenes2k8 05-04-2011 19:00

Re: I Have A Question About Pointers
 
I'm sorry, but you appear to have a need to overcomplicate everything. If there's really no need to write the code in assembly, it would probably be better to write it in C++, because the compiler can probably write better assembly than a human.

davidthefat 05-04-2011 19:24

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by mikets (Post 1049943)
Are you talking about the PowerPC processor in the cRIO or are you talking about Intel processor? Your initial post listed all the Intel processor registers. So I am not sure which one you are asking?

That is true, that was written for my computer, the thread started out as a general question and is becoming more specific. I know the PPC syntax is different. I don't think that its much of a big deal to transition from AT&T syntax to PPC syntax. I know in the IA 32 systems, it runs in protected mode. But I am not sure about PPCs

Quote:

Originally Posted by demosthenes2k8 (Post 1049948)
I'm sorry, but you appear to have a need to overcomplicate everything. If there's really no need to write the code in assembly, it would probably be better to write it in C++, because the compiler can probably write better assembly than a human.

There is no need, but I have the mentality that I would learn so much more from it. I mean there is no NEED for me to even be on the team. You see where I am going with this?

mikets 05-04-2011 19:44

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by davidthefat (Post 1049940)
Is it safe to assume that it runs in protected mode?

In Intel processor architecture, there is the concept of real mode versus protected mode. But there is no such thing in PPC. In PPC, you probably mean kernel mode versus user mode. But that's entirely different thing. For historic reason, Intel processor boots up into real mode where it uses real mode address space seg : offset to form a 20-bit physical address. That will allow you to access only the low 1 Mbyte of memory. PC BIOS runs in this environment and loads the OS loader into memory (Actually much more complicated than that, but this is the simplified view). The OS loader will then set things up and switch the CPU to go into protected mode. The setup includes setting up the virtual memory address tables GDT, LDT and the interrupt table IDT. So once the OS is booted, the CPU is already in protected mode. Everything including both the OS kernel and applications are run in protected mode. However, in protected mode, there are different privileges. Again, for historic reason, Intel processor supports 4 different privileges (rings). But most of the operating systems support only 2 privileges and they are usuually called kernel mode or superviser mode and user mode. Applications are run in user mode and OS kernel, device drivers are usually run in superviser mode.
I am not very familiar with vxWorks but I believe your program is actually compiled as a downloadable kernel module. So I assume it must be running in kernel mode as well which means your program can misbehave and kill the entire system if you are not careful (e.g. run away pointers). BTW, you can do pointer arithmetics in C/C++. There is no need to do that in assembly. But if you are not careful, you can trash memory. For vxWork, you can probably trash kernel mode memory as well and cause a system crash. For Windows application, for example, a run away pointer can only trash application memory. Kernel mode memory is protected from applications. So your application may crash but the OS will be fine. You can just kill the app and move on. However, for Windows kernel mode drivers, they have privilege to access kernel mode memroy. If they have a run away pointer, they could trash kernel mode memory and bring the system down (Blue-Screen aka BugCheck).

mikets 05-04-2011 20:05

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by davidthefat (Post 1049969)
That is true, that was written for my computer, the thread started out as a general question and is becoming more specific. I know the PPC syntax is different. I don't think that its much of a big deal to transition from AT&T syntax to PPC syntax. I know in the IA 32 systems, it runs in protected mode. But I am not sure about PPCs

BTW, there is no AT&T syntax, you probably meant Intel syntax. Also. there is no IA32 system. There is IA64 but 32-bit system is called x86. IA64 is an entirely different 64-bit architecture from Intel. The curent 64-bit Intel processors x64 is different from IA64 (Itanium). All new Intel processors are now 64-bit processors but they are backward compatible with 32-bit (x86) systems.

davidthefat 05-04-2011 20:49

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by mikets (Post 1050002)
BTW, there is no AT&T syntax, you probably meant Intel syntax. Also. there is no IA32 system. There is IA64 but 32-bit system is called x86. IA64 is an entirely different 64-bit architecture from Intel. The curent 64-bit Intel processors x64 is different from IA64 (Itanium). All new Intel processors are now 64-bit processors but they are backward compatible with 32-bit (x86) systems.

According to Intel there is http://www.intel.com/products/processor/manuals/
And I read that Intanium was the one that was NOT compatible with x86, that is why Intel uses AMD's x86-64 architecture.

Ether 05-04-2011 22:21

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by demosthenes2k8 (Post 1049948)
the compiler can probably write better assembly than a human.

Although I agree that it is seldom necessary (or desirable) to write assembly code, an experienced assembly programmer can in many cases implement optimizations that a compiler won't use. The downside is that the code can be a brittle maintenance nightmare.



mikets 06-04-2011 01:24

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by davidthefat (Post 1050056)
According to Intel there is http://www.intel.com/products/processor/manuals/
And I read that Intanium was the one that was NOT compatible with x86, that is why Intel uses AMD's x86-64 architecture.

Well, I guess I did not follow the Intel terminology as close as I thought. In any case, Intel has three architectures: Itanium 64 (IA64) is not compatible with anything, x64 which is compatible with amd64 and the old 32-bit whatever it is called (x86).

davidthefat 06-04-2011 01:58

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by mikets (Post 1049986)
In Intel processor architecture, there is the concept of real mode versus protected mode. But there is no such thing in PPC. In PPC, you probably mean kernel mode versus user mode. But that's entirely different thing. For historic reason, Intel processor boots up into real mode where it uses real mode address space seg : offset to form a 20-bit physical address. That will allow you to access only the low 1 Mbyte of memory. PC BIOS runs in this environment and loads the OS loader into memory (Actually much more complicated than that, but this is the simplified view). The OS loader will then set things up and switch the CPU to go into protected mode. The setup includes setting up the virtual memory address tables GDT, LDT and the interrupt table IDT. So once the OS is booted, the CPU is already in protected mode. Everything including both the OS kernel and applications are run in protected mode. However, in protected mode, there are different privileges. Again, for historic reason, Intel processor supports 4 different privileges (rings). But most of the operating systems support only 2 privileges and they are usuually called kernel mode or superviser mode and user mode. Applications are run in user mode and OS kernel, device drivers are usually run in superviser mode.
I am not very familiar with vxWorks but I believe your program is actually compiled as a downloadable kernel module. So I assume it must be running in kernel mode as well which means your program can misbehave and kill the entire system if you are not careful (e.g. run away pointers). BTW, you can do pointer arithmetics in C/C++. There is no need to do that in assembly. But if you are not careful, you can trash memory. For vxWork, you can probably trash kernel mode memory as well and cause a system crash. For Windows application, for example, a run away pointer can only trash application memory. Kernel mode memory is protected from applications. So your application may crash but the OS will be fine. You can just kill the app and move on. However, for Windows kernel mode drivers, they have privilege to access kernel mode memroy. If they have a run away pointer, they could trash kernel mode memory and bring the system down (Blue-Screen aka BugCheck).

Actually it might be in kernel mode. That explains why vXWorks does not have a system call system. You just go do it for yourself, there is no asking for the OS to do it for you.

Techhexium 06-04-2011 03:11

Re: I Have A Question About Pointers
 
Itanium or IA-64 is in a way pure 64-bit architecture. Because there is little to no compatibility with other architectures, Microsoft is finally abandoning it so there is no more future IA-64 Windows. x64 is actually an extension of x86, I think that's why some call it x86-64.

Greg McKaskle 06-04-2011 08:34

Re: I Have A Question About Pointers
 
Learning assembly and low-level computer architecture are good things. Putting your experiments on your teams robots -- maybe not.

Assembly programming is a very unforgiving task, and is best done with a net. My CS degree was a few years back, but they still required an assembly course. It was done completely virtual, on a mainframe, because your program would crash about 90% of the time. Since it was virtual, the "processor" was frozen so that you could examine the remains and determine where you went wrong, modify code, and run the experiment again. Even with the help of good tools such as this, it was a painful reminder of just how low-level and complex computers are.

The good thing about that experience is that from time to time, I need to look under the covers and try to understand a bug, an optimization, etc. and being able to grok the assembly and low-level parts of the computer is useful. The rest of the time it would only slow me down by about 100x.

Once you have a good PPC assembly reference and understand the basics of the architecture, I'm pretty sure that you can run your C++ code in vxWorks, hit a breakpoint, show the registers, the stack, and the disassembly. Even better, some environments show you the mixed view of your code -- a line of C and the assembly for that line. This will allow you to see how the compiler produces assembly, learn its tricks, and do it much faster than starting from scratch. It is informative to look at array indexing code, function calls, loops, and other high-level structures that make you productive. It is also informative to intentionally put some common bugs into your code and step through them in assembly to better understand what they cause the computer to do.

Greg McKaskle

mikets 06-04-2011 12:38

Re: I Have A Question About Pointers
 
Are you trying to learn an assembly language or it must be PPC assembly? If learning x86 assembly is good enough, you can use your existing tools. Since your first post seems to be C++/C#, you probaby have visual studio. If so it supports the syntax of _asm {<some assembly code>} mixed in with C code. So you can progressively add more assembly code to your project. Another useful thing to do is to configure your compiler to generate assembly listing so that you can see how each C line is converted into assembly.

Ether 06-04-2011 12:45

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by mikets (Post 1050311)
Another useful thing to do is to configure your compiler to generate assembly listing so that you can see how each C line is converted into assembly.

Yes. And you may be appalled at what you find.



demosthenes2k8 07-04-2011 22:21

Re: I Have A Question About Pointers
 
I'm pretty sure you add a -S to the command line options to do that...I'm gonna try that out now.

After trying that out, HUGE MISTAKE. I used to think my code was elegant...

davidthefat 07-04-2011 22:29

Re: I Have A Question About Pointers
 
Yes, I used the disassembly tool in the debugger, I found that a lot of the code was filled with nop (no operation) but why? Is it to compensate for the data transfer from the ram to the cache? That seems unlikely

mikets 07-04-2011 22:34

Re: I Have A Question About Pointers
 
Most likely for alignment. Usually in between functions.

wireties 07-04-2011 23:24

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by davidthefat (Post 1050191)
Actually it might be in kernel mode. That explains why vXWorks does not have a system call system. You just go do it for yourself, there is no asking for the OS to do it for you.

The 6.X version of VxWorks does have a system call interface (and normal block, character and network drivers) and processes (which they call RTPs or real-time processes) but the robot code does not use them. But RTP support is not included in the cRio BSP (board support package) provided to FIRST. All the robot code runs in the kernel context or mode.

HTH

Ether 08-04-2011 08:53

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by wireties (Post 1050649)
...the robot code does not use them ... RTP support is not included in the cRio BSP (board support package) provided to FIRST. All the robot code runs in the kernel context or mode.

Keith, I'm not disagreeing with you, but how do you know all this? Is there a document somewhere which discusses this ?



wireties 11-04-2011 15:14

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by Ether (Post 1050704)
Keith, I'm not disagreeing with you, but how do you know all this? Is there a document somewhere which discusses this ?

NP Ether - I taught engineers how to use VxWorks (and embedded Linux) for 15+ years. Check Help->Help Contents->Wind River Documentation from the Workbench IDE. Under Guides->Operating System there is a Application Programmers Guide (which is about programming in a RTP) and a Kernel Programmers Guide (about programming in kernel space/mode) and a BSP Developers Guide (which is about building a board support package - which NI gives us). In our case, NI and/or WPI chose to not use RTPs and stick everything in kernel space/mode (probably because that is how LabView's code generator works). The C/C++ Developer User Guides and Getting Started->Tutorials are also most helpful.

HTH

Greg McKaskle 12-04-2011 06:32

Re: I Have A Question About Pointers
 
I'm not on the RT team, but the reason I was given was that when they did the board support, the RTP introduced too much overhead for the I/O drivers. Until FRC, the only C programmers for the cRIO were the NI developers, so they kept it in kernel mode. At that time, all other users of the cRIO used LV.

Exposing the C/C++ interfaces and tools, going to RTP was evaluated, but there would be a lot of work and testing involved, and it would mess with the I/O rates of various drivers.

Greg McKaskle

Ether 12-04-2011 09:23

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by wireties (Post 1051762)
Check Help->Help Contents->Wind River Documentation from the Workbench IDE. Under Guides->Operating System there is a Application Programmers Guide (which is about programming in a RTP) and a Kernel Programmers Guide (about programming in kernel space/mode) and a BSP Developers Guide (which is about building a board support package - which NI gives us). In our case, NI and/or WPI chose to not use RTPs and stick everything in kernel space/mode (probably because that is how LabView's code generator works).

Are these three Guides freely available online somewhere, for those who do not have the Workbench installed ?



wireties 12-04-2011 16:47

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by Greg McKaskle (Post 1051835)
I'm not on the RT team, but the reason I was given was that when they did the board support, the RTP introduced too much overhead for the I/O drivers. Until FRC, the only C programmers for the cRIO were the NI developers, so they kept it in kernel mode. At that time, all other users of the cRIO used LV.

Exposing the C/C++ interfaces and tools, going to RTP was evaluated, but there would be a lot of work and testing involved, and it would mess with the I/O rates of various drivers.

There are good reasons for staying in kernel space. I wasn't knocking NI's choices. But hey - if the students are running Java, how can isolating (just the students code) in an RTP slow things down too much? For example, the PID controller tasks could run in kernel context and share a memory region where the students code updated the operating parameters. This specific example would result in no performance penalty at all and errant student code code not crash the kernel or the NI infrastructure. I'm not a strong or gun-ho advocate but, strictly speaking, it is possible.

HTH

wireties 12-04-2011 16:50

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by Ether (Post 1051885)
Are these three Guides freely available online somewhere, for those who do not have the Workbench installed ?

Not that I know of - WindRiver does not open source VxWorks (or its documentation) yet. But you don't need Workbench to view them. I think they exist in html and/or pdf format. If someone on your team has Workbench installed they should be able to find them and give them to you. I doubt that would violate the WindRiver-FIRST licensing agreement.

HTH

Ether 12-04-2011 16:51

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by wireties (Post 1052044)
There are good reasons for staying in kernel space. I wasn't knocking NI's choices. But hey - if the students are running Java, how can isolating (just the students code) in an RTP slow things down too much?

Greg said the IO was the issue.



Ether 12-04-2011 16:56

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by wireties (Post 1052046)
Not that I know of - WindRiver does not open source VxWorks (or its documentation) yet. But you don't need Workbench to view them. I think they exist in html and/or pdf format. If someone on your team has Workbench installed they should be able to find them and give them to you. I doubt that would violate the WindRiver-FIRST licensing agreement.

Would these VxWorks documents be on a machine that has the 2011 FRC LabVIEW Framework installed? Or only if C++ is installed?



wireties 12-04-2011 16:58

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by Ether (Post 1052050)
Would these VxWorks documents be on a machine that has the 2011 FRC LabVIEW Framework installed? Or only if C++ is installed?

Only if Wind River's Workbench is installed, which is most of the C++ install I think.

HTH

Ether 14-04-2011 10:27

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by wireties
Check Help->Help Contents->Wind River Documentation from the Workbench IDE. Under Guides->Operating System there is a Application Programmers Guide (which is about programming in a RTP) and a Kernel Programmers Guide (about programming in kernel space/mode) and a BSP Developers Guide (which is about building a board support package - which NI gives us). In our case, NI and/or WPI chose to not use RTPs and stick everything in kernel space/mode (probably because that is how LabView's code generator works).
Quote:

Originally Posted by Ether
Are these three Guides freely available online somewhere, for those who do not have the Workbench installed ?
Found them on-line, if anyone else if interested:

Kernel Programmer's Guide v6.7

Application Programmer's Guide v6.7

BSP Developer's Guide v6.7




wireties 15-04-2011 11:25

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by Ether (Post 1052713)

FIRST is running 6.3 (I think) but not too much has changed.

davidthefat 16-04-2011 21:27

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by Ether (Post 1052713)

Thank you sir.


All times are GMT -5. The time now is 12:30.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi