3
2

MISRA C++ 5-0-16 , C++(31)

Last updated at Posted at 2018-03-22

MISRA C++の規則の例題をコンパイル、実行して、何らかの出力をするプログラム。Clang++, G++の出力結果、コンパイルワーニング等は末尾に註釈(comment)

VisualC++の結果も掲載予定。

<この項は書きかけです。順次追記します。>
This article is not completed. I will add some words in order.

目次

1 filename: misracpp5-0-16.cpp
2 original examples and/or notes:
3 compile and output mechanism:
4 compile errors and/or warnings:
5. Hardware: MacBook Pro, (Retina, 13-inch, Mid 2014)
6. Special Thanks: Upper organizatios and
7. notes
8. code
9. Error, Warning and/or Output

misracpp5-0-16.cpp
// 1 filename: misracpp5-0-16.cpp
// ver 0.1 18(Mon), Aug, 2017

#include <iostream>
#include <string>
#include <cstdarg>
#include "misracpp.h"

// 2 original examples and/or notes:
// (c) Mira Limited, 2008
// MISRA C++2008, Guidelines for the use of the C++ language in critical systems. June 2008.
// https://www.misra.org.uk/forum/viewforum.php?f=180
std::string ru ("\"6.5 Expressions.\"");
std::string rus ("\"Rule 5-0-16 (Required) A pointer operand and any pointer resulting from pointer arithmetic using that operand shall both address elements of the same array.[Undefined 5.7(5)\"");

// 3 compile and output mechanism:
// (c) Dr. OGAWA Kiyoshi, kaizen at wh.commufa.jp,
//
// 4 compile errors and/or warnings:
// 4.1  Clang  http://www.llvm.org/
// Copyright (c) 2003-2017, LLVM Project
// clang++ --version
// (c) clang version 5.0.0 (tags/RELEASE_500/final)
// Target: x86_64-apple-darwin14.5.0
// Thread model: posix
// InstalledDir: /usr/local/opt/llvm/bin
// Base standard: https://clang.llvm.org/cxx_status.html
// Command/Options: clang++ -std=c++14 -stdlib=libc++ -Wall misracpp5-0-16.cpp -o misracpp5-0-16L
// Configuration: brew install --with-clang llvm
//
// 4.2. g++ (Homebrew GCC 6.4.0) 6.4.0
// Copyright (C) 2017 Free Software Foundation, Inc.
// This is free software; see the source for copying conditions.
// There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ln -s /usr/local/bin/gcc-6 /usr/local/bin/gcc
// ln -s /usr/local/bin/g++-6 /usr/local/bin/g++
// Base standard: http://gcc.gnu.org/onlinedocs/gcc/Standards.html
// Command/Options: g++  -std=c++14  -Wall misracpp5-0-16.cpp  -o misracpp5-0-16G
// Configuration:brew install gcc6
//
// 5. Hardware:  MacBook Pro,  (Retina, 13-inch, Mid 2014)
// OS X 10.10.5 Yosemite
//Core i5, 2.6GHz, 16GB, 1600MHz DDR3
//(c) Intel http://ark.intel.com/products/series/75024/4th-Generation-Intel-Core-i5-Processors
//
// 6. Special Thanks: Upper organizatios and
// ISO/IEC JTC1 SC22 WG21: http://www.open-std.org/jtc1/sc22/wg21/
// MISRA: https://www.misra.org.uk/forum/index.php
// ISO/IEC JTC1 SC22 WG14: http://www.open-std.org/jtc1/sc22/wg14/
// ITSCJ/IPSJ http://www.itscj.ipsj.or.jp/itscj_english/index.html
// Renesas Electronics Corporation.http://www.renesas.com/
// NPO SESSAME project, http://www.sessame.jp/workinggroup/WorkingGroup3/
// Toyo Corporation, http://www.toyo.co.jp/English/
// Japan Standard Association, http://bit.ly/1lzykg1
// NPO TOPPERS project, https://www.toppers.jp/asp-d-download.html
// Daido Universcity, http://www.daido-it.ac.jp/gakubugakka/computer/index.html
// WITZ Co.Ltd., http://www.witz-inc.co.jp/products/solution/solution.html
// SevenWise.Co., http://www.7ws.co.jp/index.html
// TOYOTA Motor Corporation, http://toyota.jp/
// DENSO Corporation, http://www.globaldenso.com/en/
// Aisin Seiki Co. Ltd., http://www.aisin.com/
// Cypress Semiconductor Corp., http://japan.cypress.com/
// Yazaki Corporation, http://www.yazaki-group.com/global/
// Pananosic Corporation, http://www.panasonic.net/
// Denso Create Inc.: http://www.denso-create.jp
// SWEST: Summer Workshop on Embedded System Technologies , http://swest.toppers.jp
// CEST: Consortium for Embedded System Technology, http://www.ertl.jp/CEST/
// JUSE: Union of Japanese Scientists and Engineers, http://www.juse.or.jp/e/
// OSC: Open Source Conference, http://www.ospn.jp/

// 7. notes
//{C} Cert C++ text
//{L} LLVM warnings and errors
//{G} Gcc warning and errors
//{S} C++ standard, ISO/IEC JTC1 SC22 WG14
// add 'n' to function and variable names of non compliant example if a same name exists. And sequence number also.
// add 'c' to  function and variable names of compliant examples if a same name exists. And sequence number also.
// 8. code

void f1( const int32_t * a1){
    int32_t a2[10]= {2,4,8,16,32,64,128,256,512,1024};
    const int32_t * p1 = &a1[1];
    int32_t * p2 = &a2[10];
    int32_t * p3 = &a2[11];
    std::cout << *p1 << std::endl << *p2 << std::endl << *p3 << std::endl;

}
    int32_t b=1;
    int32_t c[10]={3,5,7,9,11,13,15,17,19,21};

int main(int argc, char* argv[]){
    f1(&b);
    f1(c);
    std::cout << ru << std::endl << rus << std::endl;
    return argc;
}

// 9. Error, Warning and/or Output
// 9.1 clang
// clang++ -std=c++14 -stdlib=libc++ -Wall misracpp5-0-16.cpp -o misracpp5-0-16L
// 9.1.1 warnings
//misracpp5-0-16.cpp:81:18: warning: array index 11 is past the end of the array
//      (which contains 10 elements) [-Warray-bounds]
//        int32_t * p3 = &a2[11];
//                        ^  ~~
//misracpp5-0-16.cpp:78:2: note: array 'a2' declared here
//        int32_t a2[10]= {2,4,8,16,32,64,128,256,512,1024};
//        ^
//1 warning generated.
// 9.1.2 output
//$ ./misracpp5-0-16L
//0
//-570039104
//1912615492
//5
//-570039104
//1912615492
//"6.5 Expressions."
//"Rule 5-0-16 (Required) A pointer operand and any pointer resulting from pointer arithmetic using that operand shall both address elements of the same array.[Undefined 5.7(5)"
// 9.2 g++
//g++  -std=c++14  -Wall misracpp5-0-16.cpp  -o misracpp5-0-16G
// 9.2.1 no warning
// 9.2.2 output
//$ ./misracpp5-0-16G
//0
//1586133020
//32767
//5
//1586133020
//32767
//""
//"Rule 5-0-16 (Required) A pointer operand and any pointer resulting from pointer arithmetic using that operand shall both address elements of the same array.[Undefined 5.7(5)"

##Template版print使用

misra5-0-16.cpp
// 1 filename: misracpp5-0-16.cpp
// ver 0.1 18(Mon), Aug, 2017

#include <string>
#include "misracpp.h"

// 2 original examples and/or notes:
// (c) Mira Limited, 2008
// MISRA C++2008, Guidelines for the use of the C++ language in critical systems. June 2008.
// https://www.misra.org.uk/forum/viewforum.php?f=180
string ru ("\"6.5 Expressions.\"");
string rus ("\"Rule 5-0-16 (Required) A pointer operand and any pointer resulting from pointer arithmetic using that operand shall both address elements of the same array.[Undefined 5.7(5)\"");

// 3 compile and output mechanism:
// (c) Dr. OGAWA Kiyoshi, kaizen at wh.commufa.jp,
//
// 4 compile errors and/or warnings:
// 4.1  Clang  http://www.llvm.org/
// Copyright (c) 2003-2017, LLVM Project
// clang++ --version
// (c) clang version 5.0.0 (tags/RELEASE_500/final)
// Target: x86_64-apple-darwin14.5.0
// Thread model: posix
// InstalledDir: /usr/local/opt/llvm/bin
// Base standard: https://clang.llvm.org/cxx_status.html
// Command/Options: clang++ -std=c++14 -stdlib=libc++ -Wall misracpp5-0-16.cpp -o misracpp5-0-16L
// Configuration: brew install --with-clang llvm
//
// 4.2. g++ (Homebrew GCC 6.4.0) 6.4.0
// Copyright (C) 2017 Free Software Foundation, Inc.
// This is free software; see the source for copying conditions.
// There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ln -s /usr/local/bin/gcc-6 /usr/local/bin/gcc
// ln -s /usr/local/bin/g++-6 /usr/local/bin/g++
// Base standard: http://gcc.gnu.org/onlinedocs/gcc/Standards.html
// Command/Options: g++  -std=c++14  -Wall misracpp5-0-16.cpp  -o misracpp5-0-16G
// Configuration:brew install gcc6
//
// 5. Hardware:  MacBook Pro,  (Retina, 13-inch, Mid 2014)
// OS X 10.10.5 Yosemite
//Core i5, 2.6GHz, 16GB, 1600MHz DDR3
//(c) Intel http://ark.intel.com/products/series/75024/4th-Generation-Intel-Core-i5-Processors
//
// 6. Special Thanks: Upper organizatios and
// ISO/IEC JTC1 SC22 WG21: http://www.open-std.org/jtc1/sc22/wg21/
// MISRA: https://www.misra.org.uk/forum/index.php
// ISO/IEC JTC1 SC22 WG14: http://www.open-std.org/jtc1/sc22/wg14/
// ITSCJ/IPSJ http://www.itscj.ipsj.or.jp/itscj_english/index.html
// Renesas Electronics Corporation.http://www.renesas.com/
// NPO SESSAME project, http://www.sessame.jp/workinggroup/WorkingGroup3/
// Toyo Corporation, http://www.toyo.co.jp/English/
// Japan Standard Association, http://bit.ly/1lzykg1
// NPO TOPPERS project, https://www.toppers.jp/asp-d-download.html
// Daido Universcity, http://www.daido-it.ac.jp/gakubugakka/computer/index.html
// WITZ Co.Ltd., http://www.witz-inc.co.jp/products/solution/solution.html
// SevenWise.Co., http://www.7ws.co.jp/index.html
// TOYOTA Motor Corporation, http://toyota.jp/
// DENSO Corporation, http://www.globaldenso.com/en/
// Aisin Seiki Co. Ltd., http://www.aisin.com/
// Cypress Semiconductor Corp., http://japan.cypress.com/
// Yazaki Corporation, http://www.yazaki-group.com/global/
// Pananosic Corporation, http://www.panasonic.net/
// Denso Create Inc.: http://www.denso-create.jp
// SWEST: Summer Workshop on Embedded System Technologies , http://swest.toppers.jp
// CEST: Consortium for Embedded System Technology, http://www.ertl.jp/CEST/
// JUSE: Union of Japanese Scientists and Engineers, http://www.juse.or.jp/e/
// OSC: Open Source Conference, http://www.ospn.jp/

// 7. notes
//{C} Cert C++ text
//{L} LLVM warnings and errors
//{G} Gcc warning and errors
//{S} C++ standard, ISO/IEC JTC1 SC22 WG14
// add 'n' to function and variable names of non compliant example if a same name exists. And sequence number also.
// add 'c' to  function and variable names of compliant examples if a same name exists. And sequence number also.
// 8. code

void f1( const int32_t * a1){
    int32_t a2[10]= {2,4,8,16,32,64,128,256,512,1024};
    const int32_t * p1 = &a1[1];
    int32_t * p2 = &a2[10];
    int32_t * p3 = &a2[11];
    print(*p1, *p2, *p3);
}
    int32_t b=1;
    int32_t c[10]={3,5,7,9,11,13,15,17,19,21};

int main(){
    f1(&b);
    f1(c);
    print(ru, rus);
    return EXIT_SUCCESS;
}

// 9. Error, Warning and/or Output
// 9.1 clang
// clang++ -std=c++14 -stdlib=libc++ -Wall misracpp5-0-16.cpp -o misracpp5-0-16L
// 9.1.1 warnings
//misracpp5-0-16.cpp:81:18: warning: array index 11 is past the end of the array
//      (which contains 10 elements) [-Warray-bounds]
//        int32_t * p3 = &a2[11];
//                        ^  ~~
//misracpp5-0-16.cpp:78:2: note: array 'a2' declared here
//        int32_t a2[10]= {2,4,8,16,32,64,128,256,512,1024};
//        ^
//1 warning generated.
// 9.1.2 output
//$ ./misracpp5-0-16L
//0
//-570039104
//1912615492
//5
//-570039104
//1912615492
//"6.5 Expressions."
//"Rule 5-0-16 (Required) A pointer operand and any pointer resulting from pointer arithmetic using that operand shall both address elements of the same array.[Undefined 5.7(5)"
// 9.2 g++
//g++  -std=c++14  -Wall misracpp5-0-16.cpp  -o misracpp5-0-16G
// 9.2.1 no warning
// 9.2.2 output
//$ ./misracpp5-0-16G
//0
//1586133020
//32767
//5
//1586133020
//32767
//""
//"Rule 5-0-16 (Required) A pointer operand and any pointer resulting from pointer arithmetic using that operand shall both address elements of the same array.[Undefined 5.7(5)"

#misracpp.h

misracpp.h
#ifndef MISRA
#define MISRA


#include <iostream>
#include <utility>
#include <cstdlib>

using namespace std;

// パラメータパックが空になったら終了
void print() {}

// ひとつ以上のパラメータを受け取るようにし、
// 可変引数を先頭とそれ以外に分割する
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail)
{
  cout << head << endl;

  // パラメータパックtailをさらにheadとtailに分割する
  print(std::move(tail)...);
}
#endif

関連文献

どうやって MISRA Example Suiteをコンパイルするか
https://qiita.com/kaizen_nagoya/items/fbdbff5ff696e2ca7f00

MISRA C.2.1 Type widening in integer promotion,(wicm3.c )
https://qiita.com/kaizen_nagoya/items/6a24db5d51efae358cfb

MISRA-C 2012 Referenceに掲載している文献の入手可能性を確認
https://qiita.com/kaizen_nagoya/items/96dc8b125e462d5575bb

MISRA C まとめ #include
https://qiita.com/kaizen_nagoya/items/f1a79a7cbd281607c7c9

MISRA C++ 5-0-16
https://qiita.com/kaizen_nagoya/items/7df2d4e05db724752a74

物理記事 上位100
https://qiita.com/kaizen_nagoya/items/66e90fe31fbe3facc6ff

数学関連記事100
https://qiita.com/kaizen_nagoya/items/d8dadb49a6397e854c6d

言語・文学記事 100
https://qiita.com/kaizen_nagoya/items/42d58d5ef7fb53c407d6

医工連携関連記事一覧
https://qiita.com/kaizen_nagoya/items/6ab51c12ba51bc260a82

通信記事100
https://qiita.com/kaizen_nagoya/items/1d67de5e1cd207b05ef7

自動車 記事 100
https://qiita.com/kaizen_nagoya/items/f7f0b9ab36569ad409c5

Qiita(0)Qiita関連記事一覧(自分)
https://qiita.com/kaizen_nagoya/items/58db5fbf036b28e9dfa6

鉄道(0)鉄道のシステム考察はてっちゃんがてつだってくれる
https://qiita.com/kaizen_nagoya/items/26bda595f341a27901a0

日本語(0)一欄
https://qiita.com/kaizen_nagoya/items/7498dcfa3a9ba7fd1e68

英語(0) 一覧
https://qiita.com/kaizen_nagoya/items/680e3f5cbf9430486c7d

転職(0)一覧
https://qiita.com/kaizen_nagoya/items/f77520d378d33451d6fe

仮説(0)一覧(目標100現在40)
https://qiita.com/kaizen_nagoya/items/f000506fe1837b3590df

安全(0)安全工学シンポジウムに向けて: 21
https://qiita.com/kaizen_nagoya/items/c5d78f3def8195cb2409

Error一覧 error(0)
https://qiita.com/kaizen_nagoya/items/48b6cbc8d68eae2c42b8

Ethernet 記事一覧 Ethernet(0)
https://qiita.com/kaizen_nagoya/items/88d35e99f74aefc98794

Wireshark 一覧 wireshark(0)、Ethernet(48)
https://qiita.com/kaizen_nagoya/items/fbed841f61875c4731d0

線網(Wi-Fi)空中線(antenna)(0) 記事一覧(118/300目標)
https://qiita.com/kaizen_nagoya/items/5e5464ac2b24bd4cd001

OSEK OS設計の基礎 OSEK(100)
https://qiita.com/kaizen_nagoya/items/7528a22a14242d2d58a3

官公庁・学校・公的団体(NPOを含む)システムの課題、官(0)
https://qiita.com/kaizen_nagoya/items/04ee6eaf7ec13d3af4c3

Error一覧(C/C++, python, bash...) Error(0)
https://qiita.com/kaizen_nagoya/items/48b6cbc8d68eae2c42b8

なぜdockerで機械学習するか 書籍・ソース一覧作成中 (目標100)
https://qiita.com/kaizen_nagoya/items/ddd12477544bf5ba85e2

言語処理100本ノックをdockerで。python覚えるのに最適。:10+12
https://qiita.com/kaizen_nagoya/items/7e7eb7c543e0c18438c4

プログラムちょい替え(0)一覧:4件
https://qiita.com/kaizen_nagoya/items/296d87ef4bfd516bc394

TOPPERSまとめ #名古屋のIoTは名古屋のOSで
https://qiita.com/kaizen_nagoya/items/9026c049cb0309b9d451

官公庁・学校・公的団体(NPOを含む)システムの課題、官(0)
https://qiita.com/kaizen_nagoya/items/04ee6eaf7ec13d3af4c3

自動制御、制御工学一覧(0)
https://qiita.com/kaizen_nagoya/items/7767a4e19a6ae1479e6b

一覧の一覧( The directory of directories of mine.) Qiita(100)
https://qiita.com/kaizen_nagoya/items/7eb0e006543886138f39

<この記事は個人の過去の経験に基づく個人の感想です。現在所属する組織、業務とは関係がありません。>
This article is an individual impression based on the individual's experience. It has nothing to do with the organization or business to which I currently belong.

文書履歴(document history)

ver. 0.10 初稿 20180202
ver. 0.11 template版print 20180807
ver. 0.12 ありがとう追記 20230312

最後までおよみいただきありがとうございました。

いいね 💚、フォローをお願いします。

Thank you very much for reading to the last sentence.

Please press the like icon 💚 and follow me for your happy life.

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2