If you don't mind helping me a little, I have a few questions for you:

1) Why is it possible different data types(which should be different sizes) be the same size in memory?
Take the sizeOf() function. I read where it's possible that you can pass it a long and print out the result, pass it a integer and print out the result, and you can come up with the same results. Like an int might be 4 bytes(which it should on a 32bit proc.), and a long might also show up as 4 bytes...But why? Why can this happen, and why is it allowed?

2) This question is concerning memory pointers. Is it really necessary to create a variable to hold a memory address? Take this code for example:

unsigned short shortVar = 6;
unsigned short * memAdd = shortVar;

Is it really important to create a "special" variable to hold the memory address?

Why not this to get the address:
int x = &shortVar;

and if you want to get the value at that address you already have the value stored in it's own variable. I'm jut a bit confused with this "design". Is the memory pointer "function" there just more bells and whistles, or is it actually very useful?


I hope all that make sense, because I just confused the crap out of myself. Sorry if it don't.

Recommended Answers

All 2 Replies

1) Why is it possible different data types(which should be different sizes) be the same size in memory?
Take the sizeOf() function. I read where it's possible that you can pass it a long and print out the result, pass it a integer and print out the result, and you can come up with the same results. Like an int might be 4 bytes(which it should on a 32bit proc.), and a long might also show up as 4 bytes...But why? Why can this happen, and why is it allowed?

Why not? :p

All hardware is not created equal, so imposing such restrictions on the language would make the language less usable. And the standard doesn't really get into specifying sizes for such things. This is the great failure by many language tutors. C, and I believe C++ via incorporation, are more about values than the sizes of the objects that contain them.

If an int must be at least able to hold a value between -32767 and 32767, there is nothing keeping it from holding a larger value. So if an int and a long happen to be the same size, no big deal. The programmer should really try to quit looking up the language's skirt anyways.

2) This question is concerning memory pointers. Is it really necessary to create a variable to hold a memory address? Take this code for example:

unsigned short shortVar = 6;
unsigned short * memAdd = shortVar;

This is generally not correct.

Is it really important to create a "special" variable to hold the memory address?

Yes, when you need to.

Why not this to get the address:
int x = &shortVar;

That's how it's supposed to be.

and if you want to get the value at that address you already have the value stored in it's own variable. I'm jut a bit confused with this "design". Is the memory pointer "function" there just more bells and whistles, or is it actually very useful?

Take for example a function in C in which you want to change the value of an object from within a called function. Using a pointer to simulate pass-by-reference is the only way to do so. I'd say that's useful.

I hope all that make sense, because I just confused the crap out of myself. Sorry if it don't.

Visit Narue's tutorial.
http://eternallyconfuzzled.com/tuts/pointers.html

Thanks Dave. I appreciate the detailed reply. This book I have really don't answer a lot of the question I have, so I thank you for assisting me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.