1

I am not a software engineer which means I am not really good at organizing my code, so this task might look very simple for you.

I have a qt c++ application for 32-bit ARM device (with lubuntu). The application contains a window which is used to debug the external custom instruction memory (64 rows of 32-bit instructions) of a different embedded device. I have created a function:

bool memoRead(uint32_t *memoRowData, uint8_t memoRowNumber); 

This function reads data stored in a given row.

I want to create a different function which uses the given function to read and display content of 8 rows from the memory. Example: When I call

memoDebug(0);

The output should be data stored in rows 0 to 7, and when I call

memoDebug(1);

The output should be data stored in rows 8 to 15

The obvious way is to create 8 different uint32_t variables, read each row separately and assign their values to the created 8 different variables and display/print them (which I have already done and succeeded). But I want to use something like array or struct instead and everything inside for loop to improve the code. In the future the memory size might increase significantly (65536 rows, and read 64 rows or more each time instead of 8).

What is the best and most efficient way of organizing my data and how should I use that?

EDIT 1: Obviously I will change the data type of the variable memoRowNumber of my memoRead function to uint32_t when my memory size increases

Adhamzhon Shukurov
  • 681
  • 1
  • 8
  • 21
  • I am not sure what you mean when you say you want to avoid a loop if you wish to print the content from each row? Using a struct you could assign each row a value and whenever you want to print that value use a row variable. For example if you read row 1, 4 and 7 in that order your row array will look like [1,4,7] and your value array will look like [row1value, row4value, row7value]. And when you wish to read value from row x you print structname.value[structname.row[x]]; where X is the row you want to read? – darclander Aug 29 '19 at 12:20
  • 1
    @darclander Actually for loop is what I want to use. I am encouraging to use for loop to decrease number of lines of code used. The function should call the memoRead() function 8 times. Ex: when I call memoDebug(2), it should print row15value, row16value, ... row23value – Adhamzhon Shukurov Aug 29 '19 at 12:44
  • @darclander In order to be able to print that data, the function should obviously call the memoRead() function 8 times with memoRowNumber values of 15, 16, ... 23 – Adhamzhon Shukurov Aug 29 '19 at 12:47
  • @Adhamzon Shukurov so when you call memoDebug(2), you save each value from memoRead() into an uint32_t such as uint32_t row1 = memoRead(); uint32_t row2 = memoRead(); ... ? Instead of doing a loop and saving each row to the array index? `for(int i = 0; i < 8; i++) {array[i] = memoRead();}`? (Formatting is weird in comments) – darclander Aug 29 '19 at 12:51
  • @darclander Yes, exactly. I am doing this to be sure for now. But I want to do it with for loop, as you suggested. – Adhamzhon Shukurov Aug 29 '19 at 12:53
  • @darclander I am just afraid of using wrong method (which can cause memory problem) – Adhamzhon Shukurov Aug 29 '19 at 12:58
  • do you mean the ARM device might run out of memory or that you might get memory leaks? – darclander Aug 29 '19 at 13:06
  • @darclander I mean memory leaks – Adhamzhon Shukurov Aug 29 '19 at 13:07

1 Answers1

1

If I understood your question correctly by the comments and the information you have provided in the question a solution would be to create a loop of choice and store the return value of memoRead() in an array.

Instead of doing:

uint32_t row1 = memoRead();
uint32_t row2 = memoRead();

You can use a loop as suggested in the comment:

//read the memo values in a loop
    uint32_t array[8]; //specify size of array or make it dynamic instead of "8"
    for(int i = 0; i < x; i++) {
        array[i] = memoRead();
    }

//print the row values in a loop
    for(int i = 0; i < x; i++) {
        std::cout << array[i] << "\n";
    }

Where X is the value you would like to read to for example if your array size is 100 instead of 8 x would be larger.

Regarding the question about memory leaks we are not allocating or deallocating any memory which should not cause any memory leaks see: reason for memory leakage in C C++, Risk of damaging your computer by altering memory in C++. If you are unsure what can cause memory leaks I would suggest you look into some articles before you do anything you do not know.

As far as I know this is the easiest way to store data, although this does not keep track of which rows you store where (which can be optimized).

Edit:

For doing everything in one loop you can simply:

uint32_t array[8];
for (int i = 0; i < x; i++) {
    array[i] = memoRead();
    std::cout << array[i] << "\n";
}
darclander
  • 1,526
  • 1
  • 13
  • 35
  • Nice answer, but how should I call memoRead? memoRead(&array[i], i) or memoRead(array[i], i)? – Adhamzhon Shukurov Aug 29 '19 at 13:49
  • Also is it ok if I merge those for loops? – Adhamzhon Shukurov Aug 29 '19 at 13:50
  • @AdhamzhonShukurov yes of course you can merge those loops! You do not need to store the return value aswell if you do not need it. How do you mean that you would call memoRead? Isn't the return value of memoRead() the value of the row you are reading? If you read all 8 rows in the memoRead method I would suggest you read about how passing arrays work in c++. https://www.geeksforgeeks.org/pointer-array-array-pointer/. – darclander Aug 29 '19 at 14:09
  • memoRead() returns bool, but you provided the idea i needed. For me this answer is more than enough to solve the problem, you answered my question, but it would be nice if you could edit your answer accordingly. – Adhamzhon Shukurov Aug 29 '19 at 14:19
  • @AdhamzhonShukurov you mean adding how to pass an array to memoRead()? I am not quite sure what you would like me to edit could you give me a suggestion? – darclander Aug 29 '19 at 14:26
  • Yes, and merging for loops – Adhamzhon Shukurov Aug 29 '19 at 14:30
  • @AdhamzhonShukurov I am not quite sure how memoRead() reads the data. Do you send memoRead() a value such as 1 and then it reads row 1, then 2 and it reads row 2? I do not think we should be commenting as much would you rather join a chatroom to discuss? – darclander Aug 29 '19 at 14:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/198668/discussion-between-adhamzhon-shukurov-and-darclander). – Adhamzhon Shukurov Aug 29 '19 at 14:37