return a pointer, always get 0000 as its segment
Author |
Message |
Jaso #1 / 8
|
 return a pointer, always get 0000 as its segment
We met a question in a project, the compiler enviroment is microsoft c700. Funcion A call B, A and B are in different files in two directories. A{ char *addr = B(); Quote: }
void * B{ char *buf; ... buf = getbuf(); //allocate some memory to buf return((char far *)buf) Quote: }
After calling B in A, the offset of addr is correct, but the SEGMENT of addr is always set to ZERO. The project will be due soon, any suggestion will be greatly appreciated!!! Jun Ge
|
Thu, 06 Oct 2005 12:51:12 GMT |
|
 |
Ben Pfaf #2 / 8
|
 return a pointer, always get 0000 as its segment
Quote:
> After calling B in A, the offset of addr is correct, but the SEGMENT > of addr is always set to ZERO.
Segmentation is a "feature" of x86 processors that is not part of ISO C. If you're truly having trouble with it, consult a newsgroup specific to your compiler vendor. Before you do that, make sure you're properly prototyping all the functions that you call. -- char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[] ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x1f6},*p= b,x,i=24;for(;p+=!*p;*p/=4)switch(x=*p&3)case 0:{return 0;for(p--;i--;i--)case 2:{i++;if(1)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
|
Thu, 06 Oct 2005 12:55:34 GMT |
|
 |
Tom St Den #3 / 8
|
 return a pointer, always get 0000 as its segment
Quote:
> void * B{
This Quote: > return((char far *)buf)
And this do not agree :-) [hint read compiler warnings] Tom
|
Thu, 06 Oct 2005 21:36:18 GMT |
|
 |
Gordon Burdi #4 / 8
|
 return a pointer, always get 0000 as its segment
Quote: >We met a question in a project, the compiler enviroment is microsoft >c700. > return((char far *)buf)
This is comp.lang.c. ANSI C doesn't use the f-word. Quote: >Funcion A call B, A and B are in different files in two directories. >A{ > char *addr = B(); >} >void * B{ > char *buf; > ... > buf = getbuf(); //allocate some memory to buf > return((char far *)buf) >}
If you take a value, put it in a type that is Too Small(tm), and then cast it back to the larger type, expect part of the larger type to be filled with a value that is Wrong(tm). What type does getbuf() return? Is there a prototype in scope for getbuf()? Is the variable buf large enough to hold what getbuf() returns? Is the return type of B (void *) large enough to hold what getbuf() returns? I doubt it - otherwise why the cast? Is the variable addr large enough to hold what getbuf() returns? Quote: >After calling B in A, the offset of addr is correct, but the SEGMENT >of addr is always set to ZERO.
If you cram a value into a smaller type where it won't fit, then cast it back to the larger type, expect the result to be Wrong(tm). Quote: >The project will be due soon, any suggestion will be greatly >appreciated!!!
If you absolutely insist on using the f-word, use it on every pointer variable unless you have a clear reason not to. Better, compile in a mode (sometimes called "memory model") where the f-word is not required but matches the compiler default. Gordon L. Burditt
|
Fri, 07 Oct 2005 07:25:52 GMT |
|
 |
Jaso #5 / 8
|
 return a pointer, always get 0000 as its segment
Thank you all! I use cv, the de{*filter*} in c700, to track the running of my program. Just before B return, buf hold right segment and offset value. But when it return to A, addr only get offset value, its offset will be set to 0000. For example, before the assignment in function A, addr is 4456:2344, after the assignment, it becomes 0000:1365. 1365 is right, but 0000 is not right. I don't know how to compile it in "memory mode", can anyone give me some hint? Thank you very much!!! Jun Ge Quote:
> >We met a question in a project, the compiler enviroment is microsoft > >c700. > > return((char far *)buf) > This is comp.lang.c. ANSI C doesn't use the f-word. > >Funcion A call B, A and B are in different files in two directories. > >A{ > > char *addr = B(); > >} > >void * B{ > > char *buf; > > ... > > buf = getbuf(); //allocate some memory to buf > > return((char far *)buf) > >} > If you take a value, put it in a type that is Too Small(tm), > and then cast it back to the larger type, expect part of > the larger type to be filled with a value that is Wrong(tm). > What type does getbuf() return? > Is there a prototype in scope for getbuf()? > Is the variable buf large enough to hold what getbuf() returns? > Is the return type of B (void *) large enough to hold what getbuf() > returns? I doubt it - otherwise why the cast? > Is the variable addr large enough to hold what getbuf() returns? > >After calling B in A, the offset of addr is correct, but the SEGMENT > >of addr is always set to ZERO. > If you cram a value into a smaller type where it won't fit, then > cast it back to the larger type, expect the result to be Wrong(tm). > >The project will be due soon, any suggestion will be greatly > >appreciated!!! > If you absolutely insist on using the f-word, use it on every pointer > variable unless you have a clear reason not to. Better, compile > in a mode (sometimes called "memory model") where the f-word is not > required but matches the compiler default. > Gordon L. Burditt
|
Sat, 08 Oct 2005 04:59:35 GMT |
|
 |
Emmanuel Delahay #6 / 8
|
 return a pointer, always get 0000 as its segment
Quote: > Thank you all! I use cv, the de{*filter*} in c700, to track the running > of my program. Just before B return, buf hold right segment and > offset value. But when it return to A, addr only get offset value, > its offset will be set to 0000. For example, before the assignment in > function A, addr is 4456:2344, after the assignment, it becomes > 0000:1365. 1365 is right, but 0000 is not right. > I don't know how to compile it in "memory mode", can anyone give me > some hint? Thank you very much!!!
<OT> Try the 'large' memory model for the whole projet and forget the f-word. </> -- -ed- emdel at noos.fr The C-language FAQ: http://www.*-*-*.com/ ~scs/C-faq/top.html C-library: http://www.*-*-*.com/ FAQ de f.c.l.c : http://www.*-*-*.com/ ~rumeau/fclc/
|
Sat, 08 Oct 2005 05:18:01 GMT |
|
 |
Joel Re #7 / 8
|
 return a pointer, always get 0000 as its segment
Quote:
> Thank you all! I use cv, the de{*filter*} in c700,
Looking up "cv c700 compiler" on google, I got a hit on a Zaurus model and on a page about Microsfot Clodeview or something. Quote: > to track the running > of my program. Just before B return, buf hold right segment and > offset value. But when it return to A, addr only get offset value, > its offset will be set to 0000.
Yes, the standard does not guarantee that when you do that you will get good results. Don't do that. Quote: > For example, before the assignment in > function A, addr is 4456:2344, after the assignment, it becomes > 0000:1365. 1365 is right, but 0000 is not right.
And that is another thing the standard doesn't guarantee good results for. Don't do that, either. The f-word which has been much maligned (and rightly so) is "far". The ability to specify far pointers is not standard C, and quite rightly so. Quote: > I don't know how to compile it in "memory mode", can anyone give me > some hint? Thank you very much!!!
That also is not standard C, and I am not going to speculate on it. Things which are not standard C here aren't really on topic. You probably want to find a newsgroup that supports the compiler/system you are working on. Quote: > Jun Ge > Funcion A call B, A and B are in different files
Irrelevant. Quote: > in two directories.
Irrelevant. Quote: > A{ > char *addr = B();
This is a character pointer. Depending on the memory model you use (whatever that is) it might have 32 bits or it might have more. Based on what you wrote above, we might guess it only has 32 bits. But we might guess wrong. Quote: This is a void pointer. Depending on the memory model you use (whatever that is) it might have 32 bits or it might have more. See above about the character pointer. Quote: > char *buf;
This is a character pointer. See above. Quote: > ... > buf = getbuf(); //allocate some memory to buf
What does getbuff() return? We don't know. We might guess, because of what you wrote above, that it returns a far pointer, but we might guess wrong. If we guessed right, getbuf() returns more than 32 bits, but buf only has 32 bits. What happens to the extra bits? If we guess right, they disappear into the bit bucket, headed out into outer space as electromagnetic radiation, never to return to earth again. Or, at any rate, they disappear. You know that segment thing you mention that is not standard C? That's where the extra bits were until you put the far pointer into a (probably not far) pointer. So your segment register probably sent its bits into outer space because you tried to squeeze a far pointer into a pointer that was probably not far. Quote: > return((char far *)buf)
This makes a far pointer. Given what you wrote above, we should probably guess it is more than 32 bits. We might also guess that it picked up some extra bits from somewhere. But the bits that getbuf() probably returned as a segment value have probably gone to outer space, because you tried to squeeze them into a pointer that was probably too small. So the extra bits are full of zero, we might guess. Quote: > }
Just to add wind to wind, I should point out that those extra bits of zero probably followed the segment value into outer space on the return, since B is not declared to return a far pointer. Quote: > After calling B in A, the offset of addr is correct, but the SEGMENT > of addr is always set to ZERO.
And we aren't very surprised. Quote: > The project will be due soon, any suggestion will be greatly > appreciated!!!
Read the manuals. If you don't have manuals, get them. Assume that anything that comes from Microsoft or iNTEL follows a standard of its own. In the meantime, declare every pointer far. (Since we do not know how to set your memory model, which is also not standard C.)
|
Sat, 08 Oct 2005 14:53:37 GMT |
|
 |
Jaso #8 / 8
|
 return a pointer, always get 0000 as its segment
We solved it at last! But still don't understand it fully. Thank you all! The problem is that we didn't declare the subroutines where we call it. So the solution is very simple, just declare it anytime and anywhere we call it. I can't yet explain why there no problem with the calling of these functions without the declaration, but the return value is not complete. It is weird. Jun Ge Quote:
> We met a question in a project, the compiler enviroment is microsoft > c700. > Funcion A call B, A and B are in different files in two directories. > A{ > char *addr = B(); > } > void * B{ > char *buf; > ... > buf = getbuf(); //allocate some memory to buf > return((char far *)buf) > } > After calling B in A, the offset of addr is correct, but the SEGMENT > of addr is always set to ZERO. > The project will be due soon, any suggestion will be greatly > appreciated!!! > Jun Ge
|
Mon, 10 Oct 2005 06:19:13 GMT |
|
|
|