![]() |
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. |
| All times are GMT -5. The time now is 09:30 AM. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi