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?










share|improve this question
























  • 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 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















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?










share|improve this question
























  • 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 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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


















  • 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 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
















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












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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 16:00









        Gerhardh

        3,7242625




        3,7242625






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo