help with data type sizes


 
Thread Tools Search this Thread
Top Forums Programming help with data type sizes
# 8  
Old 01-14-2011
Quote:
Originally Posted by DGPickett
I am not sure that is portable for all CPUs!
I think it's reasonably portable among 32-bit and 64-bit varieties of UNIX, and 32-bit varieties of Windows. Perhaps not 64-bit windows, in which long inexplicably doesn't change size even though the integer width of the processor doubles...
Quote:
I think there is a printf option for printing pointers that is portable.
The printf "%p" option is portable, but what it actually ends up printing is entirely platform and library-specific. Sometimes you get hex, sometimes you don't. Sometimes you get a leading 0x, sometimes you don't. On an old DOS compiler I got ????:???? for segment and offset -- making pointers portable on that was a whole new ball of wax, it had at least two different kinds!

Last edited by Corona688; 01-14-2011 at 06:33 PM..
# 9  
Old 01-14-2011
so when i do the print (&a-&b) which gets me a 1, that means that the values were in the heap, and when i do the print(c-d), c and d were in the stack.
but why does values in the heap have a different arithmetic then the values in the stack?
# 10  
Old 01-14-2011
Where the variable is stored has nothing to do with how it does pointer math. I can't see your code from here, but I'd venture they were all on the stack, with nothing in the heap. What type it's stored in does matter. Math on pointers is done in multiples of the base size, math on integers is just plain math.

So &a-&b is arithmetic on pointers themselves, and happens in multiples of the base size.

But if you convert them to integers first -- integers, not pointers to integers -- the compiler is no longer aware of any base size and does just plain arithmetic.

Try this:

Code:
#include <stdio.h>

int main(void)
{
        int a;
        int *b=&a;
        int *c=b+1;
        int d=b;
        int e=c;

        printf("diff between pointers is: %d\n", (unsigned long)(c-b));
        printf("diff between ints is: %d\n", e-d);
}

This makes it more obvious what's going on.

When you do math on a pointer, it puts it in terms of it's base size. An integer pointer plus one, actually gets sizeof(int) added to it, in this case 4. For plain integers, it makes no adjustment.

Last edited by Corona688; 01-14-2011 at 06:44 PM..
This User Gave Thanks to Corona688 For This Post:
# 11  
Old 01-14-2011
ok, can you explain more about
"When you do pointer arithmetic directly, the compiler does a little more legwork for you, delivering the result in multiples of the base size."
# 12  
Old 01-14-2011
I think I did, and we crossposted. But to simplify a bit:

Code:
int a;
int n=&a;
n++; // n is an integer.  it adds only one.
int *o=&a;
o++; // o is a pointer.  it adds sizeof(int).

Pointer math happens relative to the base size. integer math is just plain math.
# 13  
Old 01-14-2011
for chars if i do a="a" then get the address (&a) i get some large negative number, but i know the size of a char is 1 byte or 8 bits, so shouldn't the value be between 0 and 255 ?

also if converting an address of an int to hex and printing it why is it never negative?

Last edited by omega666; 01-14-2011 at 09:14 PM..
# 14  
Old 01-15-2011
The reason pointer arithmetic is scaled for the base type is simply to support array operations.

Given

int x[2];

... we access the second element of the array by writing x[1] or equivalently *(x + 1). You cannot fully separate arrays and pointers because the value of an array is a pointer to its first element, such that most array operations will involve pointers to their elements. It would be quite inconvenient and error-prone if we had to multiply the element number with the base type size to access the element.

Quote:
for chars if i do a="a" then get the address (&a) i get some large negative number, but i know the size of a char is 1 byte or 8 bits, so shouldn't the value be between 0 and 255 ?

also if converting an address of an int to hex and printing it why is it never negative?
The address of your char has a different type - it's char *, not char - and will probably (but not necessarily) have the same size as any other pointer, and typically amounts to 32 or 64 bits on most systems.

Signedness is a matter of how the data is interpreted. The %x format specifier expects an unsigned argument and will therefore treat all bits as data, whereas if you're using %d, the most significant bit of your value will be treated as sign bit - if it's set, the number is negative, otherwise it isn't.

Pointer encoding is system-specific, but it is generally not signed, so you should not print it as a signed value.

Quote:
I think it's reasonably portable among 32-bit and 64-bit varieties of UNIX, and 32-bit varieties of Windows. Perhaps not 64-bit windows, in which long inexplicably doesn't change size even though the integer width of the processor doubles...
On Windows (and arguably other systems as well, though I tend to use unsigned long) it is best to convert your pointer to size_t, because that will give you an __int64.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Incompatible data type fpos_t in C

This is from a program I wrote over in 1998 that I am trying to compile on a linux machine: void write_line (FILE *fp, int rec_no, line_rec *arec) { fpos_t woffset; woffset = (rec_no - 1) * sizeof(line_rec); fsetpos(fp,&woffset); fwrite(arec,sizeof(line_rec),1,fp); }On the line... (2 Replies)
Discussion started by: wbport
2 Replies

2. Web Development

Data type to use for prices with commas

Hi everybody, I`m very new with PHP and Databases and I having the follow issue with prices data.. The original information is in CSV files. The prices have formatted with commas and dots as follow: 12,300.99 -->(thousands separated by commas) 3,500.25 -->(thousands separated... (10 Replies)
Discussion started by: cgkmal
10 Replies

3. Shell Programming and Scripting

Can I split a 10GB file into 1 GB sizes using my repeating data pattern

I'm not a unix guy so excuses my ignorance... I'm the database ETL guy. I'm trying to be proactive and devise a plan B for a ETL process where I expect a file 10X larger than what I process daily for a recast job. The ETL may handle it but I just don't know. This file may need to be split... (3 Replies)
Discussion started by: john091
3 Replies

4. UNIX for Advanced & Expert Users

Memory allocation for float data type

Dear All, How internally memory allocated when we declare the float data type. how many bytes allocated for decimal and how many bytes for fraction. kindly help me in this regards. (2 Replies)
Discussion started by: rajamohan
2 Replies

5. Shell Programming and Scripting

Perl data type checking

I am using perl 5.8.0. I need to check some values to see it they are floats. Our system does not have Data::Types so I can't use is_float. Is there something else that I can use? The only thing in Data is Dump.pm. I am not allowed to download anything to our system so I have to use what I have.... (3 Replies)
Discussion started by: ajgwin
3 Replies

6. UNIX for Dummies Questions & Answers

Directorie listing in Human form for data sizes

I have seen it done at my job before, there is a command that will make a notepad and show the directorie path, subfolders, and size of the subfolders? But i dont want it to go lower than 2 levels for example: folder_01 10 GB subfolder_02 10 GB subfolder_03 10 GB... (4 Replies)
Discussion started by: JUSSAN007
4 Replies

7. AIX

Value too large to be stored in data type???

Hello, I get this message : "Value too large to be stored in data type" when I try to open a 3Gb file. Can someone helps me to resolve the problem. Thank you very much (5 Replies)
Discussion started by: limame
5 Replies

8. Programming

data type limitation

I am writing some code to do analysis on the file system (HP-UX 11.11). I am using stat(..) to get file information. My problem is that the file-size may exceed the data types defined in 'sys/stat.h' & 'sys/types.h' respectively. Thus file-sizes in the Giga-byte range are not read correctly.... (2 Replies)
Discussion started by: ALTRUNVRSOFLN
2 Replies

9. Programming

time_t data type-- what does start +1 mean?

Hi, I am trying to understand an very old C program. .... time_t start, end; ptr = localtime(&start); ... fprintf(out, "%-35s 01 %5s %2s %10d 1 5 /tty/M%d/%02d %24s", buffer3, job, ver, start, mach_num,atoi(buffer), asctime(ptr)); fprintf(out, "%-35s 03 %5s %2s %10d 1 5... (9 Replies)
Discussion started by: whatisthis
9 Replies

10. Programming

FILE data type

Hi all, Can anyone tell me a little about the datatype FILE, which represents stream. What does its structure look like, and in which header file is it defined and so on... Ex : FILE *fp ; fp = fopen("filename", "w") ; (6 Replies)
Discussion started by: milhan
6 Replies
Login or Register to Ask a Question