473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Pointer subtraction

udt_list *g_list = calloc(1, LIST_SIZE);
udt_entry *entry = get_entry(); // returns a pointer to some address in
g_list
int numbytes = (char*)entry - (char*)g_list; // supposed to be offset in
bytes

But numbytes is -240. Can someone explain why?
Nov 14 '05 #1
5 4605
Kevin C. wrote:
udt_list *g_list = calloc(1, LIST_SIZE);
udt_entry *entry = get_entry(); // returns a pointer to some address in
g_list
int numbytes = (char*)entry - (char*)g_list;
// supposed to be offset in bytes

Can't get dangerous than this line. You are subtracting char * and
assigning to an integer.
If you can let us know the precise intentions before doing this, this
n.g. can probably help. Letz know the bigger picture.

But numbytes is -240. Can someone explain why?

--
Karthik.
Humans please 'removeme_' for my real email.
Nov 14 '05 #2
Mac
On Fri, 07 May 2004 02:37:12 +0000, Kevin C. wrote:
udt_list *g_list = calloc(1, LIST_SIZE);
udt_entry *entry = get_entry(); // returns a pointer to some address in
g_list
int numbytes = (char*)entry - (char*)g_list; // supposed to be offset in
bytes

But numbytes is -240. Can someone explain why?


You should declare numbytes to be a ptrdiff_t, a type which is defined in
stddef.h.

Personally, I would subtract like types without a cast, then multiply by
the sizeof the type to get the byte offset.

I doubt that the conversion to plain int is causing you a problem, though.
More likely, your assumption about g_list is wrong. Since you don't
provide any definitions of types and so on, it is impossible to
troubleshoot any further than that, AFAICT.

--Mac

Nov 14 '05 #3
"Kevin C." <no****@fake.com> wrote in message
news:sD*******************@newssvr29.news.prodigy. com
udt_list *g_list = calloc(1, LIST_SIZE);
udt_entry *entry = get_entry(); // returns a pointer to some address
in g_list
int numbytes = (char*)entry - (char*)g_list; // supposed to be offset
in bytes

But numbytes is -240. Can someone explain why?


The fact that get_entry() doesn't take an argument is a clue that it doesn't
do what you think it does.

The following shows an example that works. It retrieves the offset of the
style field in a CREATESTRUCT struct. Compare it to what you are doing and
you should be able to figure out where you are going wrong.

LONG *GetStyleAddress(CREATESTRUCT * pcs)
{
return &pcs->style;
}

int main()
{
CREATESTRUCT *pcs = (CREATESTRUCT*)calloc(1, sizeofCREATESTRUCT));
LONG *pstyle = GetStyleAddress(pcs);
int numbytes = (char*)pstyle - (char*)pcs;
return 0;
}

If you were working with a linked list, then there is no necessary reason
for later list elements to have a higher address than earlier elements, but
the code you have supplied isn't consistent with a linked list.

--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Nov 14 '05 #4
"Mac" <fo*@bar.net> wrote:
On Fri, 07 May 2004 02:37:12 +0000, Kevin C. wrote:
udt_list *g_list = calloc(1, LIST_SIZE);
udt_entry *entry = get_entry(); // returns a pointer to some address in
g_list
I seriously doubt that. Hint: how does get_entry know about g_list?
int numbytes = (char*)entry - (char*)g_list; // supposed to be offset in
bytes

But numbytes is -240. Can someone explain why?

Obviously entry is _not_ a pointer into the same array. You invoked
UB, the result could've been anything.
You should declare numbytes to be a ptrdiff_t, a type which is defined in
stddef.h.
Indeed.
Personally, I would subtract like types without a cast, then multiply by
the sizeof the type to get the byte offset.


Bad idea: subtracting pointers of incompatible object type is a
constraint violation, even if both pointers happen to point into
(or one past) the same array object.

<snip>

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #5
Mac
On Fri, 07 May 2004 09:59:27 +0200, Irrwahn Grausewitz wrote:
"Mac" <fo*@bar.net> wrote: [snip]
Personally, I would subtract like types without a cast, then multiply by
the sizeof the type to get the byte offset.


Bad idea: subtracting pointers of incompatible object type is a
constraint violation, even if both pointers happen to point into
(or one past) the same array object.


When I wrote "like types" I meant to convey the idea that I would subtract
only if the types were the same. I didn't word it very clearly.

--Mac
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: 2boxers | last post by:
Can somebody tell me why the following program fails to compile? I am using gcc-3.4.0. #include <iostream> using namespace std; int addition (int a, int b) { return (a+b); } int...
67
by: Ike Naar | last post by:
Hi, Asking your advice on the following subject: Suppose I want to find out whether a given pointer (say, p) of type *T points to an element of a given array (say, a) of type T. A way to...
11
by: Sushil | last post by:
Hi Gurus I've tried to come up with a small logical example of my problem. The problem is platform specific (MIPS) which I understand should not be discussed here. So here goes my example: ...
11
by: junky_fellow | last post by:
Can I subtract two pointers of same type that are pointing to the two different location of memory allocated by malloc. eg. #include <stdlib.h> int main(void) { unsigned char *c_ptr;...
3
by: randomtalk | last post by:
hello everyone! Well, recently i've been trying to pick up c and see what is pointer all about (been programming in lisp/python for the better part of my last two years).. mmm.. I'm currently...
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
5
by: Angel Tsankov | last post by:
Does the standard define what happens when a NULL pointer is dereferenced? If so, where?
1
by: Michael | last post by:
Hi, My understanding of int op(int x, int y, int (*func)(int,int)). when n = op (20, m, minus); minus is a function pointer, which matches parameter. when m = op (7, 5, addition);there...
20
by: junky_fellow | last post by:
Hi, In my previous post I asked if sizeof may be implemented as a function. Somebody pointed a link that says that sizeof may calculated as follows with a warning that it is not guaranteed to...
5
by: junky_fellow | last post by:
Guys, I want to find out an offset of a member inside a structure. Although, there is a macro (offsetof) available to do this. Still, I have one question. Can I subtract the address of the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.