Dear All
I am porting code developed by using sun pc and cc compilers to gcc(2.95.2) and gpc(2.1)
Now I have landed in a preprocessor related problem,please see below code.
str_255 = PACKED ARRAY[1..256] OF CHAR; Str_ptr = ^Str_255;
strptr : str_ptr; p : str_ptr;
#define PAS_ADDTO_PTR(ptr,value) (Pointer(integer(ptr) + value)) #define PAS_TYPECAST(right, left_type) left_type(right)
p := PAS_TYPECAST(PAS_ADDTO_PTR(strptr,pos-1),str_ptr);
when I use above macros and assign the p pointer,it is pointing to junk values. But when I manually replace all the preprocessor definitions like below, it is working fine.
p := str_ptr(Pointer(integer(strptr) + (pos-1)));
Please suggest me as what to do and also let me know other bugs related to preprocessor.
Regards Hari
__________________________________ Yahoo! Mail - PC Magazine Editors' Choice 2005 http://mail.yahoo.com
vanam srihari kumar wrote:
I am porting code developed by using sun pc and cc compilers to gcc(2.95.2) and gpc(2.1)
Now I have landed in a preprocessor related problem,please see below code.
str_255 = PACKED ARRAY[1..256] OF CHAR; Str_ptr = ^Str_255;
strptr : str_ptr; p : str_ptr;
#define PAS_ADDTO_PTR(ptr,value) (Pointer(integer(ptr) + value)) #define PAS_TYPECAST(right, left_type) left_type(right)
p := PAS_TYPECAST(PAS_ADDTO_PTR(strptr,pos-1),str_ptr);
when I use above macros and assign the p pointer,it is pointing to junk values. But when I manually replace all the preprocessor definitions like below, it is working fine.
p := str_ptr(Pointer(integer(strptr) + (pos-1)));
Please suggest me as what to do and also let me know other bugs related to preprocessor.
Use gpc -E command to see output from preprocessor. Note that some macros are predefined, so preprocessor may be doing more expansion than you expect.
vanam srihari kumar wrote:
I am porting code developed by using sun pc and cc compilers to gcc(2.95.2) and gpc(2.1)
Now I have landed in a preprocessor related problem,please see below code.
str_255 = PACKED ARRAY[1..256] OF CHAR; Str_ptr = ^Str_255;
strptr : str_ptr; p : str_ptr;
#define PAS_ADDTO_PTR(ptr,value) (Pointer(integer(ptr) + value)) #define PAS_TYPECAST(right, left_type) left_type(right)
p := PAS_TYPECAST(PAS_ADDTO_PTR(strptr,pos-1),str_ptr);
when I use above macros and assign the p pointer,it is pointing to junk values. But when I manually replace all the preprocessor definitions like below, it is working fine.
p := str_ptr(Pointer(integer(strptr) + (pos-1)));
At first glance, only a few parentheses are different. You might want to try if this makes a difference.
Apart from that, it is not safe to use `Integer' for pointer artihmetics. `Integer' may be smaller than pointer types. This may already explain the junk (in this case, both variants are wrong, but one happens to work anyway, or seems to work ...). Also `Integer' is signed, which would yield errors in the addition when you happen to hit the center of the address space (depending on the OS, this is more or less likely). Use `PtrCard' instead to avoid both problems.
And finally, depending on what you *really* mean with this code, it may be safer and more readable to use array indexing instead of pointer arithmetic (still followed by a type cast, if you need the result as a str_ptr):
p := str_ptr (@strptr[pos]);
Frank