How to correctly add decimal integer to an address
up vote
-1
down vote
favorite
I am trying to add a decimal integer to an address. (I'm not very clear about address, I guess it works in hexadecimal) in the following way:
//node_t is a structure with an int and node_t* ptr, so i have the size 8
int move = sizeof(node_t) + size; //here node_t is 8, size is 10
//so actually i want to move it by 18bytes.
node_t* tmp = (current)+sizeof(node_t) / sizeof(node_t) + size/sizeof(int)
//current is the starting address, i try to get current moving positively with 18 bytes
But it turns out tmp
is only 16 bytes greater than current
.
I think the problem is that size is recognized as hexadecimal, how can I solve this?
c pointers
add a comment |
up vote
-1
down vote
favorite
I am trying to add a decimal integer to an address. (I'm not very clear about address, I guess it works in hexadecimal) in the following way:
//node_t is a structure with an int and node_t* ptr, so i have the size 8
int move = sizeof(node_t) + size; //here node_t is 8, size is 10
//so actually i want to move it by 18bytes.
node_t* tmp = (current)+sizeof(node_t) / sizeof(node_t) + size/sizeof(int)
//current is the starting address, i try to get current moving positively with 18 bytes
But it turns out tmp
is only 16 bytes greater than current
.
I think the problem is that size is recognized as hexadecimal, how can I solve this?
c pointers
Why would you wan't to do that?
– Quentin
Nov 22 at 15:50
Please provide a MCVE. You do not show any definition of your variable.
– Gerhardh
Nov 22 at 15:52
I have to simulate malloc() in simple way, and i trying to make a pointer pointing to header of next free space after assign the 10bytes(size).
– Kent Wong
Nov 22 at 15:53
You still do not show definitions ofa
andcurrent
– Gerhardh
Nov 22 at 16:01
"the addition is done in hexadecimal" hexadecimal is only a represenation of numbers. Unless you print or scan numbers that has no meaning. Thesizeof
operator "returns" a value. Such a value does not have decimal or hexadecimal representation until you print it.
– Gerhardh
Nov 22 at 16:11
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I am trying to add a decimal integer to an address. (I'm not very clear about address, I guess it works in hexadecimal) in the following way:
//node_t is a structure with an int and node_t* ptr, so i have the size 8
int move = sizeof(node_t) + size; //here node_t is 8, size is 10
//so actually i want to move it by 18bytes.
node_t* tmp = (current)+sizeof(node_t) / sizeof(node_t) + size/sizeof(int)
//current is the starting address, i try to get current moving positively with 18 bytes
But it turns out tmp
is only 16 bytes greater than current
.
I think the problem is that size is recognized as hexadecimal, how can I solve this?
c pointers
I am trying to add a decimal integer to an address. (I'm not very clear about address, I guess it works in hexadecimal) in the following way:
//node_t is a structure with an int and node_t* ptr, so i have the size 8
int move = sizeof(node_t) + size; //here node_t is 8, size is 10
//so actually i want to move it by 18bytes.
node_t* tmp = (current)+sizeof(node_t) / sizeof(node_t) + size/sizeof(int)
//current is the starting address, i try to get current moving positively with 18 bytes
But it turns out tmp
is only 16 bytes greater than current
.
I think the problem is that size is recognized as hexadecimal, how can I solve this?
c pointers
c pointers
edited Nov 22 at 16:09
asked Nov 22 at 15:47
Kent Wong
234
234
Why would you wan't to do that?
– Quentin
Nov 22 at 15:50
Please provide a MCVE. You do not show any definition of your variable.
– Gerhardh
Nov 22 at 15:52
I have to simulate malloc() in simple way, and i trying to make a pointer pointing to header of next free space after assign the 10bytes(size).
– Kent Wong
Nov 22 at 15:53
You still do not show definitions ofa
andcurrent
– Gerhardh
Nov 22 at 16:01
"the addition is done in hexadecimal" hexadecimal is only a represenation of numbers. Unless you print or scan numbers that has no meaning. Thesizeof
operator "returns" a value. Such a value does not have decimal or hexadecimal representation until you print it.
– Gerhardh
Nov 22 at 16:11
add a comment |
Why would you wan't to do that?
– Quentin
Nov 22 at 15:50
Please provide a MCVE. You do not show any definition of your variable.
– Gerhardh
Nov 22 at 15:52
I have to simulate malloc() in simple way, and i trying to make a pointer pointing to header of next free space after assign the 10bytes(size).
– Kent Wong
Nov 22 at 15:53
You still do not show definitions ofa
andcurrent
– Gerhardh
Nov 22 at 16:01
"the addition is done in hexadecimal" hexadecimal is only a represenation of numbers. Unless you print or scan numbers that has no meaning. Thesizeof
operator "returns" a value. Such a value does not have decimal or hexadecimal representation until you print it.
– Gerhardh
Nov 22 at 16:11
Why would you wan't to do that?
– Quentin
Nov 22 at 15:50
Why would you wan't to do that?
– Quentin
Nov 22 at 15:50
Please provide a MCVE. You do not show any definition of your variable.
– Gerhardh
Nov 22 at 15:52
Please provide a MCVE. You do not show any definition of your variable.
– Gerhardh
Nov 22 at 15:52
I have to simulate malloc() in simple way, and i trying to make a pointer pointing to header of next free space after assign the 10bytes(size).
– Kent Wong
Nov 22 at 15:53
I have to simulate malloc() in simple way, and i trying to make a pointer pointing to header of next free space after assign the 10bytes(size).
– Kent Wong
Nov 22 at 15:53
You still do not show definitions of
a
and current
– Gerhardh
Nov 22 at 16:01
You still do not show definitions of
a
and current
– Gerhardh
Nov 22 at 16:01
"the addition is done in hexadecimal" hexadecimal is only a represenation of numbers. Unless you print or scan numbers that has no meaning. The
sizeof
operator "returns" a value. Such a value does not have decimal or hexadecimal representation until you print it.– Gerhardh
Nov 22 at 16:11
"the addition is done in hexadecimal" hexadecimal is only a represenation of numbers. Unless you print or scan numbers that has no meaning. The
sizeof
operator "returns" a value. Such a value does not have decimal or hexadecimal representation until you print it.– Gerhardh
Nov 22 at 16:11
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
This does not make much sense:
node_t* tmp = (current)+sizeof(node_t) / sizeof(node_t) + size/sizeof(int)
Pointer arithmetics works by adding a number of elements, not bytes.
You seem to try to take care about this by dividing the sizes but this cannot work.
From the expression I assume current
is of type node_t*
.
Adding sizeof(node_t) / sizeof(node_t)
basically moves it behind current position. You could also just use 1
for this part.
Then you add size/sizeof(int)
.
Here you assume a different size of elements: int
instead of node_t
.
You mention size of node_t
is 8. How would you divide 10 by 8? 10/8
is 1 with integer division.
This means you always move by 2 elements, i.e. 16 bytes.
You could try this ugly hack instead:
node_t* tmp = (node_t *)(((char*)current) + sizeof(node_t) + size);
This can work if you do not have very strict alignment restrictions on your hardware.
Otherwise you need to round up to the next well aligned address.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
This does not make much sense:
node_t* tmp = (current)+sizeof(node_t) / sizeof(node_t) + size/sizeof(int)
Pointer arithmetics works by adding a number of elements, not bytes.
You seem to try to take care about this by dividing the sizes but this cannot work.
From the expression I assume current
is of type node_t*
.
Adding sizeof(node_t) / sizeof(node_t)
basically moves it behind current position. You could also just use 1
for this part.
Then you add size/sizeof(int)
.
Here you assume a different size of elements: int
instead of node_t
.
You mention size of node_t
is 8. How would you divide 10 by 8? 10/8
is 1 with integer division.
This means you always move by 2 elements, i.e. 16 bytes.
You could try this ugly hack instead:
node_t* tmp = (node_t *)(((char*)current) + sizeof(node_t) + size);
This can work if you do not have very strict alignment restrictions on your hardware.
Otherwise you need to round up to the next well aligned address.
add a comment |
up vote
2
down vote
This does not make much sense:
node_t* tmp = (current)+sizeof(node_t) / sizeof(node_t) + size/sizeof(int)
Pointer arithmetics works by adding a number of elements, not bytes.
You seem to try to take care about this by dividing the sizes but this cannot work.
From the expression I assume current
is of type node_t*
.
Adding sizeof(node_t) / sizeof(node_t)
basically moves it behind current position. You could also just use 1
for this part.
Then you add size/sizeof(int)
.
Here you assume a different size of elements: int
instead of node_t
.
You mention size of node_t
is 8. How would you divide 10 by 8? 10/8
is 1 with integer division.
This means you always move by 2 elements, i.e. 16 bytes.
You could try this ugly hack instead:
node_t* tmp = (node_t *)(((char*)current) + sizeof(node_t) + size);
This can work if you do not have very strict alignment restrictions on your hardware.
Otherwise you need to round up to the next well aligned address.
add a comment |
up vote
2
down vote
up vote
2
down vote
This does not make much sense:
node_t* tmp = (current)+sizeof(node_t) / sizeof(node_t) + size/sizeof(int)
Pointer arithmetics works by adding a number of elements, not bytes.
You seem to try to take care about this by dividing the sizes but this cannot work.
From the expression I assume current
is of type node_t*
.
Adding sizeof(node_t) / sizeof(node_t)
basically moves it behind current position. You could also just use 1
for this part.
Then you add size/sizeof(int)
.
Here you assume a different size of elements: int
instead of node_t
.
You mention size of node_t
is 8. How would you divide 10 by 8? 10/8
is 1 with integer division.
This means you always move by 2 elements, i.e. 16 bytes.
You could try this ugly hack instead:
node_t* tmp = (node_t *)(((char*)current) + sizeof(node_t) + size);
This can work if you do not have very strict alignment restrictions on your hardware.
Otherwise you need to round up to the next well aligned address.
This does not make much sense:
node_t* tmp = (current)+sizeof(node_t) / sizeof(node_t) + size/sizeof(int)
Pointer arithmetics works by adding a number of elements, not bytes.
You seem to try to take care about this by dividing the sizes but this cannot work.
From the expression I assume current
is of type node_t*
.
Adding sizeof(node_t) / sizeof(node_t)
basically moves it behind current position. You could also just use 1
for this part.
Then you add size/sizeof(int)
.
Here you assume a different size of elements: int
instead of node_t
.
You mention size of node_t
is 8. How would you divide 10 by 8? 10/8
is 1 with integer division.
This means you always move by 2 elements, i.e. 16 bytes.
You could try this ugly hack instead:
node_t* tmp = (node_t *)(((char*)current) + sizeof(node_t) + size);
This can work if you do not have very strict alignment restrictions on your hardware.
Otherwise you need to round up to the next well aligned address.
answered Nov 22 at 16:00
Gerhardh
3,7242625
3,7242625
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434427%2fhow-to-correctly-add-decimal-integer-to-an-address%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why would you wan't to do that?
– Quentin
Nov 22 at 15:50
Please provide a MCVE. You do not show any definition of your variable.
– Gerhardh
Nov 22 at 15:52
I have to simulate malloc() in simple way, and i trying to make a pointer pointing to header of next free space after assign the 10bytes(size).
– Kent Wong
Nov 22 at 15:53
You still do not show definitions of
a
andcurrent
– Gerhardh
Nov 22 at 16:01
"the addition is done in hexadecimal" hexadecimal is only a represenation of numbers. Unless you print or scan numbers that has no meaning. The
sizeof
operator "returns" a value. Such a value does not have decimal or hexadecimal representation until you print it.– Gerhardh
Nov 22 at 16:11