454,599 Members | 1,304 Online
Need help? Post your question and get tips & solutions from a community of 454,599 IT Pros & Developers. It's quick & easy.

 P: n/a Quick question about pointers. Wrote this "trying to understand this better" code: int *ptr, x = 565; ptr= &x; printf("\n\n\nThe value of x is %d\n", x); printf("Now: ptr= &x\n So:"); printf("The value of *ptr is %d\n", *ptr); printf("And, straight ptr should give an address of \"x\"\n "); printf("Ptr points at x whose address is is %d\n", ptr); /*<<<<<<<< returns -1073743224<<<*/ return 0; } All except the last line work...or is the memory location indeed "-1073743224" Thanks in advance. Nov 13 '06 #1
10 Replies

 P: n/a mdh wrote: > Quick question about pointers. Wrote this "trying to understand this better" code: int *ptr, x = 565; ptr= &x; [...] printf("Ptr points at x whose address is is %d\n", ptr); /*<<<<<<<< returns -1073743224<<<*/ [...] All except the last line work...or is the memory location indeed "-1073743224" It could be. First, "%d" expects an int, and you have pased an int*, so all bets are off. However, given that your post says you're on an Intel Mac, and most of those systems probably have sizeof(int)==sizeof(int*)==4, it's probably sort-of okay on your platform. Next, "%d" interprets the value as a signed integer, which makes for strange interpretations for half of the address space. Finally, -1073743224 decimal is 0xBFFFFA88 hex (assuming a 32-bit signed int), which is certainly a reasonable address, especially if the particular implementation starts the stack growing down from 0xC0000000. -- +-------------------------+--------------------+-----------------------+ | Kenneth J. Brody | www.hvcomputer.com | #include | | kenbrody/at\spamcop.net | www.fptech.com | Nov 13 '06 #3

 P: n/a mdh: int *ptr, x = 565; ptr= &x; While we're saving space, we may aswell write: int x = 565, *ptr = &x; This also saves us mixing declarations with statements. -- Frederick Gotham Nov 13 '06 #4

 P: n/a mdh wrote: Quick question about pointers. Wrote this "trying to understand this better" code: int *ptr, x = 565; ptr= &x; [...] printf("Ptr points at x whose address is is %d\n", ptr); /*<<<<<<<< returns -1073743224<<<*/ The specifier for a pointer is %p and its corresponding argument is a void *: printf("Ptr points at x whose address is %p\n", (void *) ptr); Nov 13 '06 #5

 P: n/a Kenneth Brody San Diego Supercomputer Center <* We must do something. This is something. Therefore, we must do this. Nov 13 '06 #6

 P: n/a In article , Keith Thompson Right. On some platforms, though, integer and pointer arguments canbe passed using different mechanisms, so the location that printf()looks at in response to a "%d" format might have nothing at all to dowith the pointer value that you passed to it. (For example, the 68khas separate integer and pointer CPU registers; I don't know whetherparameter passing mechanisms typically take advantage of this.) That would depend upon the ABI for that implementation. Some do, some don't. -- Is there any thing whereof it may be said, See, this is new? It hath been already of old time, which was before us. -- Ecclesiastes Nov 13 '06 #7

 P: n/a Keith Thompson wrote: > Kenneth Brody Nov 15 '06 #8

 P: n/a 2006-11-15 <45***************@spamcop.net>, Kenneth Brody wrote: Now, this particular platform used the "normal" stack method of passing parameters. However, I have seen platforms that pass the first few parameters in registers (though there is only one set of registers on those platforms), so it's certainly possible that there is a platform which has more than one set of registers, and that uses the registers to pass parameters, which would use different registers depending on whether the parameter is a pointer or not. Even for platforms that do this, it seems unlikely they'd do it for variadic functions. There's a reason they need a prototype in scope. Nov 15 '06 #9

 P: n/a Random832 wrote: > 2006-11-15 <45***************@spamcop.net>, Kenneth Brody wrote: Now, this particular platform used the "normal" stack method of passing parameters. However, I have seen platforms that pass the first few parameters in registers (though there is only one set of registers on those platforms), so it's certainly possible that there is a platform which has more than one set of registers, and that uses the registers to pass parameters, which would use different registers depending on whether the parameter is a pointer or not. Even for platforms that do this, it seems unlikely they'd do it for variadic functions. There's a reason they need a prototype in scope. True. Yet another "real world" example of how calling a varadic function without a prototype in scope is UB. (In this case, the compiler will generate code to put the first several [or maybe even all] of the parameters into registers, but the function itself assumes that all of the parameters are on the stack.) -- +-------------------------+--------------------+-----------------------+ | Kenneth J. Brody | www.hvcomputer.com | #include | | kenbrody/at\spamcop.net | www.fptech.com | Nov 15 '06 #10

 P: n/a In article <45***************@spamcop.net>, Kenneth Brody Now, this particular platform used the "normal" stack method ofpassing parameters. However, I have seen platforms that pass thefirst few parameters in registers (though there is only one set ofregisters on those platforms), so it's certainly possible that thereis a platform which has more than one set of registers, and thatuses the registers to pass parameters, which would use differentregisters depending on whether the parameter is a pointer or not. The SGI "n32" and "n64" ABIs use the same registers for integers and pointers, but different registers for float/double. http://techpubs.sgi.com/library/tpl/...html/ch02.html As an approximation: the Nth parameter (N from 1 to 8) is at float register \$f(11+N) if it is float or double, and at general register \$(3+N) otherwise. (So half of the registers will go unused, but you always know the proper register by knowing the type and the parameter number.) In the above ABIs, varargs are always in the general registers even if they are float/double. In the above ABIs, floats, or structs that are one or two floats (including Complex) are returned in floating point registers; any other return type (including integers, pointers, small arrays, structs and unions) that total 128 bits or smaller are returned in general registers; any other larger return type is dealt with by passing in a save area to receive the result with nothing returned in registers. This is an example of an ABI in which not having a prototype in scope can definitely retrieve an unexpected value; it does not, though, happen to be an ABI in which not having a prototype for malloc() would be a problem. -- Prototypes are supertypes of their clones. -- maplesoft Nov 16 '06 #11

### This discussion thread is closed

Replies have been disabled for this discussion.