How to manage many char array variables?












0















I am trying to learn how to send data properly. Currently the way I do it is by creating a new array for each command.



unsigned char data0{ 0x00, 0x00 };
CommandSEND(handle, 0x01, 0x31, 0, 0, 2, data0);
CommandSEND(handle, 0x01, 0x31, 0, 0, 2, data0);
CommandSEND(handle, 0x01, 0x31, 0, 0, 2, data0);

unsigned char data1{ 0x01 };
CommandSEND(handle, 0x01, 0x31, 0, 0, 1, data1);

unsigned char data2{ 0x00,0x00 };
CommandSEND(handle, 0x01, 0x45, 0, 0, 2, data2);

// and so on... (i have data# variable going to data20)


I was thinking of using vectors, but the library that I am using, only accepts unsigned char arrays. Also whenever you change the size of the vector, it deletes the previous memory and copies it to a new location (Is what I believe is happening behind the scenes). Which I feel like is no different than me having many variables.



I have also been thinking of making my own class that would accept a char array. If I give it a different char array it would delete the previous array and add a new one. In this case would I need to call delete before assigning a new array to that same variable each time? Or should I call delete after I am done using that variable all together? (My guess is that I would need to call delete every single time, because I believe it would find new pointer each time I call syntax new)



I hope to have my code to run as fast as possible, hence I feel like if I keep deleting and assigning this would take time and that there might be a better, a more optimized way of doing it.



I want to learn to be able to keep my code well optimized.



Thank you, any tips and criticism is welcome.
Also wasn't sure whats the best title for this question would be.










share|improve this question




















  • 7





    You are optimizing prematurely...you will probably be better off by getting the basic logic of the program working, and then fiddling with different memory management techniques, but only if performance testing shows that that's where the bottleneck is. The unsigned char arrays in your code fragment (data0, data1, data2) re not being allocated or deleted individually -- what actually happens (on most computers) is that when this function starts, the stack pointer is decremented to allocate space for those arrays. When the function returns, the arrays are released. It's not slow.

    – Dave M.
    Nov 21 '18 at 0:08











  • Hello, thank you for replying. I did finish my code and it works fine. I am looking to make changes to it and improve it as much as possible as a learning experience (this being one of the changes that Im currently making). I want to be able to squeeze out as much performance as i can out of it. I see, yea stack is faster than heap in this regard. I just feel like creating 20 variables for 20 commands feels a bit silly.

    – aapp
    Nov 21 '18 at 0:18








  • 1





    If CommandSEND doesn't need to change the command that you pass in, you can just pass them as strings: CommandSEND( handle, 0x01, 0x31, 0, 0, 2, "" ); (You may have to cast the string to an const unsigned char *). That would make the code look a little cleaner, although it wouldn't significantly change what is going on.

    – Dave M.
    Nov 21 '18 at 0:32











  • Thanks, that is something I have not thought of. However the library I am using does change the command I pass in so i am unable to use this method. I guess there no way to change it. I will keep it the way it is. Thank you. You can rewrite your comment as an answer and i will accept it. As it does answer my question. Thank you very much.

    – aapp
    Nov 21 '18 at 1:15











  • If using vectors, this may help: en.cppreference.com/w/cpp/container/vector/data

    – Kenny Ostrom
    Nov 21 '18 at 2:20


















0















I am trying to learn how to send data properly. Currently the way I do it is by creating a new array for each command.



unsigned char data0{ 0x00, 0x00 };
CommandSEND(handle, 0x01, 0x31, 0, 0, 2, data0);
CommandSEND(handle, 0x01, 0x31, 0, 0, 2, data0);
CommandSEND(handle, 0x01, 0x31, 0, 0, 2, data0);

unsigned char data1{ 0x01 };
CommandSEND(handle, 0x01, 0x31, 0, 0, 1, data1);

unsigned char data2{ 0x00,0x00 };
CommandSEND(handle, 0x01, 0x45, 0, 0, 2, data2);

// and so on... (i have data# variable going to data20)


I was thinking of using vectors, but the library that I am using, only accepts unsigned char arrays. Also whenever you change the size of the vector, it deletes the previous memory and copies it to a new location (Is what I believe is happening behind the scenes). Which I feel like is no different than me having many variables.



I have also been thinking of making my own class that would accept a char array. If I give it a different char array it would delete the previous array and add a new one. In this case would I need to call delete before assigning a new array to that same variable each time? Or should I call delete after I am done using that variable all together? (My guess is that I would need to call delete every single time, because I believe it would find new pointer each time I call syntax new)



I hope to have my code to run as fast as possible, hence I feel like if I keep deleting and assigning this would take time and that there might be a better, a more optimized way of doing it.



I want to learn to be able to keep my code well optimized.



Thank you, any tips and criticism is welcome.
Also wasn't sure whats the best title for this question would be.










share|improve this question




















  • 7





    You are optimizing prematurely...you will probably be better off by getting the basic logic of the program working, and then fiddling with different memory management techniques, but only if performance testing shows that that's where the bottleneck is. The unsigned char arrays in your code fragment (data0, data1, data2) re not being allocated or deleted individually -- what actually happens (on most computers) is that when this function starts, the stack pointer is decremented to allocate space for those arrays. When the function returns, the arrays are released. It's not slow.

    – Dave M.
    Nov 21 '18 at 0:08











  • Hello, thank you for replying. I did finish my code and it works fine. I am looking to make changes to it and improve it as much as possible as a learning experience (this being one of the changes that Im currently making). I want to be able to squeeze out as much performance as i can out of it. I see, yea stack is faster than heap in this regard. I just feel like creating 20 variables for 20 commands feels a bit silly.

    – aapp
    Nov 21 '18 at 0:18








  • 1





    If CommandSEND doesn't need to change the command that you pass in, you can just pass them as strings: CommandSEND( handle, 0x01, 0x31, 0, 0, 2, "" ); (You may have to cast the string to an const unsigned char *). That would make the code look a little cleaner, although it wouldn't significantly change what is going on.

    – Dave M.
    Nov 21 '18 at 0:32











  • Thanks, that is something I have not thought of. However the library I am using does change the command I pass in so i am unable to use this method. I guess there no way to change it. I will keep it the way it is. Thank you. You can rewrite your comment as an answer and i will accept it. As it does answer my question. Thank you very much.

    – aapp
    Nov 21 '18 at 1:15











  • If using vectors, this may help: en.cppreference.com/w/cpp/container/vector/data

    – Kenny Ostrom
    Nov 21 '18 at 2:20
















0












0








0








I am trying to learn how to send data properly. Currently the way I do it is by creating a new array for each command.



unsigned char data0{ 0x00, 0x00 };
CommandSEND(handle, 0x01, 0x31, 0, 0, 2, data0);
CommandSEND(handle, 0x01, 0x31, 0, 0, 2, data0);
CommandSEND(handle, 0x01, 0x31, 0, 0, 2, data0);

unsigned char data1{ 0x01 };
CommandSEND(handle, 0x01, 0x31, 0, 0, 1, data1);

unsigned char data2{ 0x00,0x00 };
CommandSEND(handle, 0x01, 0x45, 0, 0, 2, data2);

// and so on... (i have data# variable going to data20)


I was thinking of using vectors, but the library that I am using, only accepts unsigned char arrays. Also whenever you change the size of the vector, it deletes the previous memory and copies it to a new location (Is what I believe is happening behind the scenes). Which I feel like is no different than me having many variables.



I have also been thinking of making my own class that would accept a char array. If I give it a different char array it would delete the previous array and add a new one. In this case would I need to call delete before assigning a new array to that same variable each time? Or should I call delete after I am done using that variable all together? (My guess is that I would need to call delete every single time, because I believe it would find new pointer each time I call syntax new)



I hope to have my code to run as fast as possible, hence I feel like if I keep deleting and assigning this would take time and that there might be a better, a more optimized way of doing it.



I want to learn to be able to keep my code well optimized.



Thank you, any tips and criticism is welcome.
Also wasn't sure whats the best title for this question would be.










share|improve this question
















I am trying to learn how to send data properly. Currently the way I do it is by creating a new array for each command.



unsigned char data0{ 0x00, 0x00 };
CommandSEND(handle, 0x01, 0x31, 0, 0, 2, data0);
CommandSEND(handle, 0x01, 0x31, 0, 0, 2, data0);
CommandSEND(handle, 0x01, 0x31, 0, 0, 2, data0);

unsigned char data1{ 0x01 };
CommandSEND(handle, 0x01, 0x31, 0, 0, 1, data1);

unsigned char data2{ 0x00,0x00 };
CommandSEND(handle, 0x01, 0x45, 0, 0, 2, data2);

// and so on... (i have data# variable going to data20)


I was thinking of using vectors, but the library that I am using, only accepts unsigned char arrays. Also whenever you change the size of the vector, it deletes the previous memory and copies it to a new location (Is what I believe is happening behind the scenes). Which I feel like is no different than me having many variables.



I have also been thinking of making my own class that would accept a char array. If I give it a different char array it would delete the previous array and add a new one. In this case would I need to call delete before assigning a new array to that same variable each time? Or should I call delete after I am done using that variable all together? (My guess is that I would need to call delete every single time, because I believe it would find new pointer each time I call syntax new)



I hope to have my code to run as fast as possible, hence I feel like if I keep deleting and assigning this would take time and that there might be a better, a more optimized way of doing it.



I want to learn to be able to keep my code well optimized.



Thank you, any tips and criticism is welcome.
Also wasn't sure whats the best title for this question would be.







c++ arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 1:47







aapp

















asked Nov 20 '18 at 23:56









aappaapp

206




206








  • 7





    You are optimizing prematurely...you will probably be better off by getting the basic logic of the program working, and then fiddling with different memory management techniques, but only if performance testing shows that that's where the bottleneck is. The unsigned char arrays in your code fragment (data0, data1, data2) re not being allocated or deleted individually -- what actually happens (on most computers) is that when this function starts, the stack pointer is decremented to allocate space for those arrays. When the function returns, the arrays are released. It's not slow.

    – Dave M.
    Nov 21 '18 at 0:08











  • Hello, thank you for replying. I did finish my code and it works fine. I am looking to make changes to it and improve it as much as possible as a learning experience (this being one of the changes that Im currently making). I want to be able to squeeze out as much performance as i can out of it. I see, yea stack is faster than heap in this regard. I just feel like creating 20 variables for 20 commands feels a bit silly.

    – aapp
    Nov 21 '18 at 0:18








  • 1





    If CommandSEND doesn't need to change the command that you pass in, you can just pass them as strings: CommandSEND( handle, 0x01, 0x31, 0, 0, 2, "" ); (You may have to cast the string to an const unsigned char *). That would make the code look a little cleaner, although it wouldn't significantly change what is going on.

    – Dave M.
    Nov 21 '18 at 0:32











  • Thanks, that is something I have not thought of. However the library I am using does change the command I pass in so i am unable to use this method. I guess there no way to change it. I will keep it the way it is. Thank you. You can rewrite your comment as an answer and i will accept it. As it does answer my question. Thank you very much.

    – aapp
    Nov 21 '18 at 1:15











  • If using vectors, this may help: en.cppreference.com/w/cpp/container/vector/data

    – Kenny Ostrom
    Nov 21 '18 at 2:20
















  • 7





    You are optimizing prematurely...you will probably be better off by getting the basic logic of the program working, and then fiddling with different memory management techniques, but only if performance testing shows that that's where the bottleneck is. The unsigned char arrays in your code fragment (data0, data1, data2) re not being allocated or deleted individually -- what actually happens (on most computers) is that when this function starts, the stack pointer is decremented to allocate space for those arrays. When the function returns, the arrays are released. It's not slow.

    – Dave M.
    Nov 21 '18 at 0:08











  • Hello, thank you for replying. I did finish my code and it works fine. I am looking to make changes to it and improve it as much as possible as a learning experience (this being one of the changes that Im currently making). I want to be able to squeeze out as much performance as i can out of it. I see, yea stack is faster than heap in this regard. I just feel like creating 20 variables for 20 commands feels a bit silly.

    – aapp
    Nov 21 '18 at 0:18








  • 1





    If CommandSEND doesn't need to change the command that you pass in, you can just pass them as strings: CommandSEND( handle, 0x01, 0x31, 0, 0, 2, "" ); (You may have to cast the string to an const unsigned char *). That would make the code look a little cleaner, although it wouldn't significantly change what is going on.

    – Dave M.
    Nov 21 '18 at 0:32











  • Thanks, that is something I have not thought of. However the library I am using does change the command I pass in so i am unable to use this method. I guess there no way to change it. I will keep it the way it is. Thank you. You can rewrite your comment as an answer and i will accept it. As it does answer my question. Thank you very much.

    – aapp
    Nov 21 '18 at 1:15











  • If using vectors, this may help: en.cppreference.com/w/cpp/container/vector/data

    – Kenny Ostrom
    Nov 21 '18 at 2:20










7




7





You are optimizing prematurely...you will probably be better off by getting the basic logic of the program working, and then fiddling with different memory management techniques, but only if performance testing shows that that's where the bottleneck is. The unsigned char arrays in your code fragment (data0, data1, data2) re not being allocated or deleted individually -- what actually happens (on most computers) is that when this function starts, the stack pointer is decremented to allocate space for those arrays. When the function returns, the arrays are released. It's not slow.

– Dave M.
Nov 21 '18 at 0:08





You are optimizing prematurely...you will probably be better off by getting the basic logic of the program working, and then fiddling with different memory management techniques, but only if performance testing shows that that's where the bottleneck is. The unsigned char arrays in your code fragment (data0, data1, data2) re not being allocated or deleted individually -- what actually happens (on most computers) is that when this function starts, the stack pointer is decremented to allocate space for those arrays. When the function returns, the arrays are released. It's not slow.

– Dave M.
Nov 21 '18 at 0:08













Hello, thank you for replying. I did finish my code and it works fine. I am looking to make changes to it and improve it as much as possible as a learning experience (this being one of the changes that Im currently making). I want to be able to squeeze out as much performance as i can out of it. I see, yea stack is faster than heap in this regard. I just feel like creating 20 variables for 20 commands feels a bit silly.

– aapp
Nov 21 '18 at 0:18







Hello, thank you for replying. I did finish my code and it works fine. I am looking to make changes to it and improve it as much as possible as a learning experience (this being one of the changes that Im currently making). I want to be able to squeeze out as much performance as i can out of it. I see, yea stack is faster than heap in this regard. I just feel like creating 20 variables for 20 commands feels a bit silly.

– aapp
Nov 21 '18 at 0:18






1




1





If CommandSEND doesn't need to change the command that you pass in, you can just pass them as strings: CommandSEND( handle, 0x01, 0x31, 0, 0, 2, "" ); (You may have to cast the string to an const unsigned char *). That would make the code look a little cleaner, although it wouldn't significantly change what is going on.

– Dave M.
Nov 21 '18 at 0:32





If CommandSEND doesn't need to change the command that you pass in, you can just pass them as strings: CommandSEND( handle, 0x01, 0x31, 0, 0, 2, "" ); (You may have to cast the string to an const unsigned char *). That would make the code look a little cleaner, although it wouldn't significantly change what is going on.

– Dave M.
Nov 21 '18 at 0:32













Thanks, that is something I have not thought of. However the library I am using does change the command I pass in so i am unable to use this method. I guess there no way to change it. I will keep it the way it is. Thank you. You can rewrite your comment as an answer and i will accept it. As it does answer my question. Thank you very much.

– aapp
Nov 21 '18 at 1:15





Thanks, that is something I have not thought of. However the library I am using does change the command I pass in so i am unable to use this method. I guess there no way to change it. I will keep it the way it is. Thank you. You can rewrite your comment as an answer and i will accept it. As it does answer my question. Thank you very much.

– aapp
Nov 21 '18 at 1:15













If using vectors, this may help: en.cppreference.com/w/cpp/container/vector/data

– Kenny Ostrom
Nov 21 '18 at 2:20







If using vectors, this may help: en.cppreference.com/w/cpp/container/vector/data

– Kenny Ostrom
Nov 21 '18 at 2:20














1 Answer
1






active

oldest

votes


















1














Assuming that your code fragment is inside a function, you're already doing things almost quickly as possible. Your unsigned char array variables data0, data1 and data2 are automatic, which means they're "allocated" (and "freed") simply by adjusting the stack pointer on entry to (and exit from) the function -- there's no heap action happening.



Since the arrays have initializers, the compiler must have arranged for the arrays' initial contents to be stored in static memory somewhere, and on entry to the function, those initializers are copied to the appropriate areas on the stack where the automatic variables live. This copy is the only thing you might possibly optimize (somehow), but I don't see how you'd do it unless the last argument to CommandSEND was a const unsigned char *. If CommandSEND modifies that argument, then you'll have to re-initialize the arrays every time you enter the function.



Actually, although your comment says that CommandSEND modifies its last argument, you're calling it three times in the first stanza without re-initializing data0. So either you intend to send three (possibly) different commands in those three CommandSEND calls, or CommandSEND doesn't really change its last argument. Perhaps the library (and its header file) should be improved to offer better constness guarantees on its function prototypes. This is an example of where that might be important -- if the library could promise that the last argument to CommandSEND was const, then you could pass a static const unsigned char data0 as the argument. That would eliminate the initializing copy, and if there are no other automatic variables, then it's possible that the function entry and exit would be slightly faster. If all the commands you send are just a few bytes long, you could even write them as (const unsigned char *)"x12x34x56x78". Or, for clearer expression and better maintainability, you could define constants or preprocessor macros for the commands, and then say:



CommandSEND( handle, 0x01, 0x31, 0, 0, 2, FIRST_COMMAND );
CommandSEND( handle, 0x01, 0x31, 0, 0, 2, SECOND_COMMAND );
CommandSEND( handle, 0x01, 0x31, 0, 0, 2, THIRD_COMMAND );


Altogether, I don't think there's much opportunity for code-size or execution-speed optimization in this part of your program. You could certainly do some human-directed optimization, to make it easier to read or understand, but there's not much else you can do to help the compiler make it run faster. (And you shouldn't try to do that anyway until you've profiled the code and determined that this really is where your speed bottleneck is -- premature optimization is the square root of all evil!)






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',
    autoActivateHeartbeat: false,
    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%2f53403379%2fhow-to-manage-many-char-array-variables%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









    1














    Assuming that your code fragment is inside a function, you're already doing things almost quickly as possible. Your unsigned char array variables data0, data1 and data2 are automatic, which means they're "allocated" (and "freed") simply by adjusting the stack pointer on entry to (and exit from) the function -- there's no heap action happening.



    Since the arrays have initializers, the compiler must have arranged for the arrays' initial contents to be stored in static memory somewhere, and on entry to the function, those initializers are copied to the appropriate areas on the stack where the automatic variables live. This copy is the only thing you might possibly optimize (somehow), but I don't see how you'd do it unless the last argument to CommandSEND was a const unsigned char *. If CommandSEND modifies that argument, then you'll have to re-initialize the arrays every time you enter the function.



    Actually, although your comment says that CommandSEND modifies its last argument, you're calling it three times in the first stanza without re-initializing data0. So either you intend to send three (possibly) different commands in those three CommandSEND calls, or CommandSEND doesn't really change its last argument. Perhaps the library (and its header file) should be improved to offer better constness guarantees on its function prototypes. This is an example of where that might be important -- if the library could promise that the last argument to CommandSEND was const, then you could pass a static const unsigned char data0 as the argument. That would eliminate the initializing copy, and if there are no other automatic variables, then it's possible that the function entry and exit would be slightly faster. If all the commands you send are just a few bytes long, you could even write them as (const unsigned char *)"x12x34x56x78". Or, for clearer expression and better maintainability, you could define constants or preprocessor macros for the commands, and then say:



    CommandSEND( handle, 0x01, 0x31, 0, 0, 2, FIRST_COMMAND );
    CommandSEND( handle, 0x01, 0x31, 0, 0, 2, SECOND_COMMAND );
    CommandSEND( handle, 0x01, 0x31, 0, 0, 2, THIRD_COMMAND );


    Altogether, I don't think there's much opportunity for code-size or execution-speed optimization in this part of your program. You could certainly do some human-directed optimization, to make it easier to read or understand, but there's not much else you can do to help the compiler make it run faster. (And you shouldn't try to do that anyway until you've profiled the code and determined that this really is where your speed bottleneck is -- premature optimization is the square root of all evil!)






    share|improve this answer




























      1














      Assuming that your code fragment is inside a function, you're already doing things almost quickly as possible. Your unsigned char array variables data0, data1 and data2 are automatic, which means they're "allocated" (and "freed") simply by adjusting the stack pointer on entry to (and exit from) the function -- there's no heap action happening.



      Since the arrays have initializers, the compiler must have arranged for the arrays' initial contents to be stored in static memory somewhere, and on entry to the function, those initializers are copied to the appropriate areas on the stack where the automatic variables live. This copy is the only thing you might possibly optimize (somehow), but I don't see how you'd do it unless the last argument to CommandSEND was a const unsigned char *. If CommandSEND modifies that argument, then you'll have to re-initialize the arrays every time you enter the function.



      Actually, although your comment says that CommandSEND modifies its last argument, you're calling it three times in the first stanza without re-initializing data0. So either you intend to send three (possibly) different commands in those three CommandSEND calls, or CommandSEND doesn't really change its last argument. Perhaps the library (and its header file) should be improved to offer better constness guarantees on its function prototypes. This is an example of where that might be important -- if the library could promise that the last argument to CommandSEND was const, then you could pass a static const unsigned char data0 as the argument. That would eliminate the initializing copy, and if there are no other automatic variables, then it's possible that the function entry and exit would be slightly faster. If all the commands you send are just a few bytes long, you could even write them as (const unsigned char *)"x12x34x56x78". Or, for clearer expression and better maintainability, you could define constants or preprocessor macros for the commands, and then say:



      CommandSEND( handle, 0x01, 0x31, 0, 0, 2, FIRST_COMMAND );
      CommandSEND( handle, 0x01, 0x31, 0, 0, 2, SECOND_COMMAND );
      CommandSEND( handle, 0x01, 0x31, 0, 0, 2, THIRD_COMMAND );


      Altogether, I don't think there's much opportunity for code-size or execution-speed optimization in this part of your program. You could certainly do some human-directed optimization, to make it easier to read or understand, but there's not much else you can do to help the compiler make it run faster. (And you shouldn't try to do that anyway until you've profiled the code and determined that this really is where your speed bottleneck is -- premature optimization is the square root of all evil!)






      share|improve this answer


























        1












        1








        1







        Assuming that your code fragment is inside a function, you're already doing things almost quickly as possible. Your unsigned char array variables data0, data1 and data2 are automatic, which means they're "allocated" (and "freed") simply by adjusting the stack pointer on entry to (and exit from) the function -- there's no heap action happening.



        Since the arrays have initializers, the compiler must have arranged for the arrays' initial contents to be stored in static memory somewhere, and on entry to the function, those initializers are copied to the appropriate areas on the stack where the automatic variables live. This copy is the only thing you might possibly optimize (somehow), but I don't see how you'd do it unless the last argument to CommandSEND was a const unsigned char *. If CommandSEND modifies that argument, then you'll have to re-initialize the arrays every time you enter the function.



        Actually, although your comment says that CommandSEND modifies its last argument, you're calling it three times in the first stanza without re-initializing data0. So either you intend to send three (possibly) different commands in those three CommandSEND calls, or CommandSEND doesn't really change its last argument. Perhaps the library (and its header file) should be improved to offer better constness guarantees on its function prototypes. This is an example of where that might be important -- if the library could promise that the last argument to CommandSEND was const, then you could pass a static const unsigned char data0 as the argument. That would eliminate the initializing copy, and if there are no other automatic variables, then it's possible that the function entry and exit would be slightly faster. If all the commands you send are just a few bytes long, you could even write them as (const unsigned char *)"x12x34x56x78". Or, for clearer expression and better maintainability, you could define constants or preprocessor macros for the commands, and then say:



        CommandSEND( handle, 0x01, 0x31, 0, 0, 2, FIRST_COMMAND );
        CommandSEND( handle, 0x01, 0x31, 0, 0, 2, SECOND_COMMAND );
        CommandSEND( handle, 0x01, 0x31, 0, 0, 2, THIRD_COMMAND );


        Altogether, I don't think there's much opportunity for code-size or execution-speed optimization in this part of your program. You could certainly do some human-directed optimization, to make it easier to read or understand, but there's not much else you can do to help the compiler make it run faster. (And you shouldn't try to do that anyway until you've profiled the code and determined that this really is where your speed bottleneck is -- premature optimization is the square root of all evil!)






        share|improve this answer













        Assuming that your code fragment is inside a function, you're already doing things almost quickly as possible. Your unsigned char array variables data0, data1 and data2 are automatic, which means they're "allocated" (and "freed") simply by adjusting the stack pointer on entry to (and exit from) the function -- there's no heap action happening.



        Since the arrays have initializers, the compiler must have arranged for the arrays' initial contents to be stored in static memory somewhere, and on entry to the function, those initializers are copied to the appropriate areas on the stack where the automatic variables live. This copy is the only thing you might possibly optimize (somehow), but I don't see how you'd do it unless the last argument to CommandSEND was a const unsigned char *. If CommandSEND modifies that argument, then you'll have to re-initialize the arrays every time you enter the function.



        Actually, although your comment says that CommandSEND modifies its last argument, you're calling it three times in the first stanza without re-initializing data0. So either you intend to send three (possibly) different commands in those three CommandSEND calls, or CommandSEND doesn't really change its last argument. Perhaps the library (and its header file) should be improved to offer better constness guarantees on its function prototypes. This is an example of where that might be important -- if the library could promise that the last argument to CommandSEND was const, then you could pass a static const unsigned char data0 as the argument. That would eliminate the initializing copy, and if there are no other automatic variables, then it's possible that the function entry and exit would be slightly faster. If all the commands you send are just a few bytes long, you could even write them as (const unsigned char *)"x12x34x56x78". Or, for clearer expression and better maintainability, you could define constants or preprocessor macros for the commands, and then say:



        CommandSEND( handle, 0x01, 0x31, 0, 0, 2, FIRST_COMMAND );
        CommandSEND( handle, 0x01, 0x31, 0, 0, 2, SECOND_COMMAND );
        CommandSEND( handle, 0x01, 0x31, 0, 0, 2, THIRD_COMMAND );


        Altogether, I don't think there's much opportunity for code-size or execution-speed optimization in this part of your program. You could certainly do some human-directed optimization, to make it easier to read or understand, but there's not much else you can do to help the compiler make it run faster. (And you shouldn't try to do that anyway until you've profiled the code and determined that this really is where your speed bottleneck is -- premature optimization is the square root of all evil!)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 3:43









        Dave M.Dave M.

        775624




        775624
































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403379%2fhow-to-manage-many-char-array-variables%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

            鏡平學校

            ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

            Why https connections are so slow when debugging (stepping over) in Java?