![]() |
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>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. |
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.
|
Re: I Have A Question About Pointers
Quote:
|
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; |
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. |
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 |
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 |
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.
|
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.
|
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?
|
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.
|
Re: I Have A Question About Pointers
Quote:
Quote:
|
Re: I Have A Question About Pointers
Quote:
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). |
Re: I Have A Question About Pointers
Quote:
|
Re: I Have A Question About Pointers
Quote:
And I read that Intanium was the one that was NOT compatible with x86, that is why Intel uses AMD's x86-64 architecture. |
Re: I Have A Question About Pointers
Quote:
|
Re: I Have A Question About Pointers
Quote:
|
Re: I Have A Question About Pointers
Quote:
|
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.
|
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 |
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.
|
Re: I Have A Question About Pointers
Quote:
|
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... |
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
|
Re: I Have A Question About Pointers
Most likely for alignment. Usually in between functions.
|
Re: I Have A Question About Pointers
Quote:
HTH |
Re: I Have A Question About Pointers
Quote:
|
Re: I Have A Question About Pointers
Quote:
HTH |
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 |
Re: I Have A Question About Pointers
Quote:
|
Re: I Have A Question About Pointers
Quote:
HTH |
Re: I Have A Question About Pointers
Quote:
HTH |
Re: I Have A Question About Pointers
Quote:
|
Re: I Have A Question About Pointers
Quote:
|
Re: I Have A Question About Pointers
Quote:
HTH |
Re: I Have A Question About Pointers
Quote:
Quote:
Kernel Programmer's Guide v6.7 Application Programmer's Guide v6.7 BSP Developer's Guide v6.7 |
Re: I Have A Question About Pointers
Quote:
|
Re: I Have A Question About Pointers
Quote:
|
| 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