Skip to main content

[C++] One way to convert pointer of structure to vector

Of course, you can choose one method of C++ likes reinterpret which is described in below topic:
https://www.geeksforgeeks.org/reinterpret_cast-in-c-type-casting-operators/

However, it is easily to find out one big problem in here is:
reinterpret_cast is a very special and dangerous type of casting operator

In embedded system, this problem is not allowed. Then, we should convert it by manually.
The idea is, you push each structure to vector by passing number of structure to function which is used for converting.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <list>
#include <vector>
#include <string.h>

using namespace std;

#define STR_NUM   3       //Number of structures
typedef struct
{
    char    text_id[16];
    char    name[16];
}st_A;

/* Show vector data after convert */
void vector_show( std::vector<st_A> pvIn)
{
    int size = pvIn.size();
    cout << "Number of structure in vector:" <<size << endl;
    
    for(int i = 0; i<size; i++)
    {
        cout << pvIn[i].text_id;
        cout << pvIn[i].name << endl;
    }
    
}

/* Convert from pointer of structure to vector with given number of structure */
void convert_func( st_A *psIn, int st_no)
{
    std::vector<st_A>    stTemp;
    
    for(int i = 0; i<st_no; i++)
    {
        stTemp.push_back(psIn[i]);
    }
    
    vector_show(stTemp);
}

/* It is main */
int main()
{
    //Declare structure with 10 members   
    st_A    stA_in[STR_NUM];
 
    //Set value for all member as example 
    strcpy(stA_in[0].text_id,"MrPres 0:");
    strcpy(stA_in[0].name,"Trumpy");
    
    strcpy(stA_in[1].text_id,"MrPres 1:");
    strcpy(stA_in[1].name,"Putty");
    
    strcpy(stA_in[2].text_id,"MrPres 2:");
    strcpy(stA_in[2].name,"Xiy");
    
 
 //Convert structure to vector
 convert_func(stA_in,STR_NUM);
 
 while (true);
}

Comments

Popular posts from this blog

How to test frame buffer in linux

The intended goal of this article is to provide the user with enough information so they can display an image onto a screen of their choosing. How to re-build frame buffer testing application. Base knowledge http://trac.gateworks.com/wiki/OpenEmbedded/Video_Out Frame buffer test application - rebuild fb-test-app step 1: Download soure code ( git installed on your machine and account in git-hub is required) https://github.com/prpplague/fb-test-app Command in git: git init git clone https://github.com/prpplague/fb-test-app.git step 2: Change makefile vi Makefile //open Makefile by vim editor :set nu //show line number replace from line 6 to line 12 by folow code: CC=arm-linux-gnueabi-gcc CFLAGS=-02 -Wall Save change command :wq step 3: Rebuid Type “make” command step 4: Copy execute file to target system by filezilla or something like that step 5: Remote target by root account, change mode of execute file (what copied from host)

The best 12 AI Tools in 2023

The new life challenges require new tools.  For decades, AI technology has been a fuzzy field for unprofessional users. However, it is not rocket science anymore at this time.  There are the best 12 AI tools in 2023 that will keep you up and take some advantages in your business. Here, I put everything on your table. Enjoy Solves anything - ChatGPT ( https://chat.openai.com/auth/login ) Write anything - Writesonic ( https://writesonic.com/ ) Generates Art - Midjourney ( https://www.midjourney.com/explore ) Generates Code - Replit ( https://replit.com /) Generates Video - Synthesia ( https://www.synthesia.io/ ) Generates Music - Soundraw ( https://soundraw.io/ ) Generates Tiktoks - Fliki ( https://fliki.ai/ ) Generates Avatars - Starrytars ( https://starryai.com/en/starrytars ) Generates PPTs - SlidesAI ( https://www.slidesai.io/ ) Edit Pictures - Remini ( https://remini.ai/ ) Edit Video - Pictory ( https://pictory.ai/ ) Summarize Notes - Wordtune( https://www.wordtune.com /)

Một số khái niệm hay dùng khi làm việc với RTOS

Brief Góp nhặt một số kiến thức hay dùng khi làm việc với các hệ điều hành thời gian thực. 1. Mutex Mutex là gì? Mutual exclusion: Trên một hệ điều hành thường có nhiều chương trình (hay tác vụ - task) chạy đồng thời. Mutex giúp ngăn chặn việc hai task cùng truy cập vào 1 tài nguyên (memory, register gọi chung là critical section) của hệ thống trong cùng một thời điểm. Ví dụ, ta có 1 cái FIFO, task 1 đang read FIFO, task 2 lại write FIFO, nếu task 1 chưa lấy xong phần dữ liệu cần thiết mà task 2 lại write đè vào đúng vị trí task 1 đang read, nó sẽ làm sai lệch dữ liệu mà task 1 đang đọc. Lưu ý: Cơ chế mutex được dùng trong cả thiết kế hardware và software. Dùng trong chương trình như thế nào? Hiểu đơn giản, mutex giống như cái khóa tủ dùng chung khi vào hiệu sách hay siêu thị. Để mở được tủ thì phải có khóa. Dùng xong tủ thì phải trả lại chìa khóa lại cho thằng khác dùng ví dụ 1: void task1() {        //Đợi lấy chìa khóa để mở tủ        osMutexWait();         //Cất