SlideShare une entreprise Scribd logo
1  sur  21
Télécharger pour lire hors ligne
Article: CommonArticle: Common
Mistakes In CMistakes In C
ProgrammingProgramming
By Khanh Ngo-DuyBy Khanh Ngo-Duy
Khanhnd@elarion.comKhanhnd@elarion.com
SeminarSeminar
PurposePurpose
Common MistakesCommon Mistakes
struct and Memory Paddingstruct and Memory Padding
New line characterNew line character
Binary mode in fopen()Binary mode in fopen()
strncpy()strncpy()
memset()memset()
fgets()fgets()
Non-null-terminated stringNon-null-terminated string
#include guard#include guard
Get ID of a threadGet ID of a thread
Buffer overflow, Stack overwriteBuffer overflow, Stack overwrite
PurposePurpose
Introduce common mistakes programmersIntroduce common mistakes programmers
often gets into while writing C codeoften gets into while writing C code
Get experiences to write better codesGet experiences to write better codes
Common MistakesCommon Mistakes
1. struct and Memory Padding1. struct and Memory Padding (1 of 5)(1 of 5)
//sizeof() = 12
struct myStruct
{
short s;
int i;
char c;
};
Common MistakesCommon Mistakes
1. struct and Memory Padding1. struct and Memory Padding (2 of 5)(2 of 5)
//sizeof() = 8
struct myStruct
{
int i;
short s;
char c;
};
//sizeof() = 8
struct myStruct
{
char c;
short s;
int i;
};
Common MistakesCommon Mistakes
1. struct and Memory Padding1. struct and Memory Padding (3 of 5)(3 of 5)
Memory padding is done automatically byMemory padding is done automatically by
compilercompiler
Padding increases memoryPadding increases memory but makes app tobut makes app to
run fasterrun faster
Re-order variables in struct (ascending orRe-order variables in struct (ascending or
descending) → you can reduce paddingdescending) → you can reduce padding ← your← your
experienceexperience
Common MistakesCommon Mistakes
1. struct and Memory Padding1. struct and Memory Padding (4 of 5)(4 of 5)
Rules of padding:Rules of padding:
A variable of a specific type will be aligned at offset = multiple of size ofA variable of a specific type will be aligned at offset = multiple of size of
that variable. If it is not so, padding will be added before itthat variable. If it is not so, padding will be added before it
Total size of struct = multiple of size of largest variable in struct. If it is notTotal size of struct = multiple of size of largest variable in struct. If it is not
so, padding will be added at the end of struct.so, padding will be added at the end of struct.
Example:Example:
Variables of type int will be aligned at offset: 0, 4, 8, 12, 16 etc …Variables of type int will be aligned at offset: 0, 4, 8, 12, 16 etc …
Variables of type char will be aligned at offset: 0, 1, 2, 3, 4, 5 etc …Variables of type char will be aligned at offset: 0, 1, 2, 3, 4, 5 etc …
Variables of type pointer will be aligned at offset: 0, 8, 16, 24, 32 etc ...Variables of type pointer will be aligned at offset: 0, 8, 16, 24, 32 etc ...
Common MistakesCommon Mistakes
1. struct and Memory Padding1. struct and Memory Padding (5 of 5)(5 of 5)
Sometimes, you want to avoid memorySometimes, you want to avoid memory
padding, you can usepadding, you can use #pragma pack (1)#pragma pack (1)
directivedirective
It is useful in some specific situationIt is useful in some specific situation
Save memorySave memory but your app runs slowerbut your app runs slower
#pragma pack(1) /* set alignment to 1 byte boundary */
struct MyPackedData /* sizeof() = 10 → x64 architecture */
{
char Data1;
long Data2;
char Data3;
};
#pragma pack(0) /* Back to normal */
Common MistakesCommon Mistakes
2. New line character2. New line character
New line character in Windows is different fromNew line character in Windows is different from
Linux:Linux:
In Windows, newline is denoted by 2 bytes: a combination of CarriageIn Windows, newline is denoted by 2 bytes: a combination of Carriage
Return (ASCII value 13) and Line Feed (ASCII value 10)Return (ASCII value 13) and Line Feed (ASCII value 10)
In Linux, newline is denoted by only 1 byte: the Line Feed character (ASCIIIn Linux, newline is denoted by only 1 byte: the Line Feed character (ASCII
value 10)value 10)
Common MistakesCommon Mistakes
3. Binary mode in fopen()3. Binary mode in fopen()
FILE *FILE *fopen(fopen(const char *const char *path,path, const char *const char *mode);mode);
In Windows,In Windows, text-modetext-mode andand binary-modebinary-mode areare
differentiated. e.gdifferentiated. e.g “r”“r”,, “rb”“rb”,, “w”“w”,, “wb”“wb” ……
In Linux, there is no text-mode. fopen()In Linux, there is no text-mode. fopen() alw aysalw ays
open file inopen file in binary-modebinary-mode. So,. So, “r”“r” andand “rb”“rb” areare
the same. There is no error whether you passthe same. There is no error whether you pass
“b”“b” or notor not
– fopen(“myFile.txt”, “r”);fopen(“myFile.txt”, “r”); /* prefer to use this *//* prefer to use this */
– fopen(“myFile.txt”, “rb”);fopen(“myFile.txt”, “rb”); /* In Linux, both lines are same! *//* In Linux, both lines are same! */
Common MistakesCommon Mistakes
4. strncpy()4. strncpy() (1 of 2)(1 of 2)
char *char *strncpy(strncpy(char *char *dest,dest, const char *const char *src,src, size_tsize_t n);n);
strncpy() always tries to copystrncpy() always tries to copy nn character fromcharacter from
srcsrc intointo destdest. If. If (m<n)(m<n) chars are copied →chars are copied → (n-m)(n-m)
number of zeros will be filled intonumber of zeros will be filled into destdest →→
always copiesalways copies nn characters intocharacters into destdest
So, the following codes mightSo, the following codes might C R AS H!!!C R AS H!!!
charchar str[str[55];];
strncpy(str,strncpy(str, “abc”“abc”,, 1010);); /* Will copy “abc” and 7 zeros into str *//* Will copy “abc” and 7 zeros into str */
Common MistakesCommon Mistakes
4. strncpy()4. strncpy() (2 of 2)(2 of 2)
The following codes is redundantThe following codes is redundant
– charchar str[str[1010];];
– memset (str,memset (str, 00,, 1010);); /* ← No need, strncpy() will do the thing *//* ← No need, strncpy() will do the thing */
– strncpy(str,strncpy(str, “abc”“abc”,, 1010);); /* Will copy “abc” and 7 zeros into str *//* Will copy “abc” and 7 zeros into str */
Common MistakesCommon Mistakes
5. memset()5. memset()
OnlyOnly use memset() to initialize variables touse memset() to initialize variables to
ZEROZERO
N E V E RN E V E R use memset() to initialize variables touse memset() to initialize variables to
any values rather than zeroany values rather than zero
Since, memset() fills memory with units inSince, memset() fills memory with units in bytebyte
Common MistakesCommon Mistakes
6. fgets()6. fgets()
char *char *fgets(fgets(char *char *s,s, intint size,size, FILE *FILE *stream);stream);
fgets() only reads at mostfgets() only reads at most (size -1)(size -1) chars fromchars from
streamstream intointo ss and then addsand then adds '0''0' at the end ofat the end of ss
It reads only (size -1) charactersIt reads only (size -1) characters
Common MistakesCommon Mistakes
6. Non-null-terminated string6. Non-null-terminated string
When working withWhen working with non-null-terminatednon-null-terminated string,string,
do not usedo not use “%s”“%s”. Instead, use. Instead, use “%.*s”“%.*s”
– voidvoid display(display(charchar **msg)msg)
– {{
• printf(printf(“The msg: %.256s”“The msg: %.256s”, msg);, msg);
• printf(printf(“The msg: %.*s”“The msg: %.*s”,, 256256, msg);, msg); /* does the same thing *//* does the same thing */
– }}
Common MistakesCommon Mistakes
7. #include guard7. #include guard (1 of 3)(1 of 3)
Problem:Problem:
When compiling main.c: mylib.h is included twice → declarations areWhen compiling main.c: mylib.h is included twice → declarations are
overwrittenoverwritten
mylib.h is openedmylib.h is opened tw ic etw ic e → compiler time→ compiler time definitelydefinitely increasesincreases
main.c file1.h file2.h mylib.h
#include “file1.h”
#include “file2.h”
#include “mylib.h”
/* something belongs to file1 */
#include “mylib.h”
/* something belongs to file2 */
extern int i;
Common MistakesCommon Mistakes
7. #include guard7. #include guard (2 of 3)(2 of 3)
S olution:S olution: #inc lude g uard#inc lude g uard
When compiling main.c: mylib.h is included once!When compiling main.c: mylib.h is included once!
Depends on compiler (supports include guard optimisation or not): mylib.hDepends on compiler (supports include guard optimisation or not): mylib.h
is openedis opened onc eonc e oror tw ic etw ic e → compiler time may reduce or not→ compiler time may reduce or not
Most of compilers support “include guard optimisation feature”: the includeMost of compilers support “include guard optimisation feature”: the include
guard is cached at the first call, later the file (mylib.h) will not be opened →guard is cached at the first call, later the file (mylib.h) will not be opened →
compiler time is fastercompiler time is faster
main.c file1.h file2.h mylib.h
#include “file1.h”
#include “file2.h”
#include “mylib.h”
/* something belongs to file1 */
#include “mylib.h”
/* something belongs to file2 */
#ifndef MYLIB_H
#define MYLIB_H
extern int i;
#endif
Common MistakesCommon Mistakes
7. #include guard7. #include guard (3 of 3)(3 of 3)
S olution:S olution: #inc lude g uard (optimized)#inc lude g uard (optimized)
When compiling main.c: mylib.h is included once!When compiling main.c: mylib.h is included once!
mylib.h is openedmylib.h is opened onc eonc e → faster→ faster
Must insert #ifndef everywhere when calling #include → only use with veryMust insert #ifndef everywhere when calling #include → only use with very
large project to reduce the compiler timelarge project to reduce the compiler time
Us eles sUs eles s if the compiler supports “include guard optimisation feature”if the compiler supports “include guard optimisation feature”
main.c file1.h file2.h mylib.h
#include “file1.h”
#include “file2.h”
#ifndef MYLIB_H
#include “mylib.h”
#endif
/* something belongs to file1 */
#ifndef MYLIB_H
#include “mylib.h”
#endif
/* something belongs to file2 */
#ifndef MYLIB_H
#define MYLIB_H
extern int i;
#endif
Common MistakesCommon Mistakes
8. Get ID of a thread8. Get ID of a thread
Get ID of a process is easy (Get ID of a process is easy (pid_tpid_t getpid(getpid(voidvoid););). How). How
about thread?about thread?
#include#include <sys/syscall.h><sys/syscall.h>
#define#define gettid()gettid() syscall(__NR_gettid)syscall(__NR_gettid)
printf (printf ( "Thread ID: %dn""Thread ID: %dn", gettid() );, gettid() );
Common MistakesCommon Mistakes
9. Buffer Overflow, Stack Overwrite9. Buffer Overflow, Stack Overwrite
Don't write the following codes, it will overwriteDon't write the following codes, it will overwrite
some important data in your application:some important data in your application:
intint GlobalBuffer[GlobalBuffer[1010][][2020];];
voidvoid InitializeBuffer()InitializeBuffer()
{{
intint i, j;i, j;
forfor (i =(i = 00; I <; I < 100100; i++); i++) /* You go out off the boundary *//* You go out off the boundary */
forfor (j =(j = 00; j <; j < 2121; j++); j++) /* Again, go out off the boundary *//* Again, go out off the boundary */
GlobalBuffer[i][j] =GlobalBuffer[i][j] = 00;;
}}
Youthinkyouwillnever besilly likethis? Yep, u'r rite, but sometimesyoumake mistake like this!Youthinkyouwillnever besilly likethis? Yep, u'r rite, but sometimesyoumake mistake like this!
Thanks for watchingThanks for watchingIf you see it useful → clap your hands :-)If you see it useful → clap your hands :-)

Contenu connexe

Tendances

File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C ProgrammingRavindraSalunke3
 
06 -working_with_strings
06  -working_with_strings06  -working_with_strings
06 -working_with_stringsHector Garzo
 
Format string vunerability
Format string vunerabilityFormat string vunerability
Format string vunerabilitynuc13us
 
C format string vulnerability
C format string vulnerabilityC format string vulnerability
C format string vulnerabilitysluge
 
Static types on javascript?! Type checking approaches to ensure healthy appli...
Static types on javascript?! Type checking approaches to ensure healthy appli...Static types on javascript?! Type checking approaches to ensure healthy appli...
Static types on javascript?! Type checking approaches to ensure healthy appli...Arthur Puthin
 
The Next Best String
The Next Best StringThe Next Best String
The Next Best StringKevlin Henney
 
Acm aleppo cpc training eighth session
Acm aleppo cpc training eighth sessionAcm aleppo cpc training eighth session
Acm aleppo cpc training eighth sessionAhmad Bashar Eter
 
Acm aleppo cpc training ninth session
Acm aleppo cpc training ninth sessionAcm aleppo cpc training ninth session
Acm aleppo cpc training ninth sessionAhmad Bashar Eter
 
Grep - A powerful search utility
Grep - A powerful search utilityGrep - A powerful search utility
Grep - A powerful search utilityNirajan Pant
 
Checking the Open-Source Multi Theft Auto Game
Checking the Open-Source Multi Theft Auto GameChecking the Open-Source Multi Theft Auto Game
Checking the Open-Source Multi Theft Auto GameAndrey Karpov
 
Brogramming - Python, Bash for Data Processing, and Git
Brogramming - Python, Bash for Data Processing, and GitBrogramming - Python, Bash for Data Processing, and Git
Brogramming - Python, Bash for Data Processing, and GitRon Reiter
 
Regular Expression (RegExp)
Regular Expression (RegExp)Regular Expression (RegExp)
Regular Expression (RegExp)Davide Dell'Erba
 

Tendances (19)

C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
Python programming language
Python programming languagePython programming language
Python programming language
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
 
2 debugging-c
2 debugging-c2 debugging-c
2 debugging-c
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
06 -working_with_strings
06  -working_with_strings06  -working_with_strings
06 -working_with_strings
 
Format string vunerability
Format string vunerabilityFormat string vunerability
Format string vunerability
 
C format string vulnerability
C format string vulnerabilityC format string vulnerability
C format string vulnerability
 
Static types on javascript?! Type checking approaches to ensure healthy appli...
Static types on javascript?! Type checking approaches to ensure healthy appli...Static types on javascript?! Type checking approaches to ensure healthy appli...
Static types on javascript?! Type checking approaches to ensure healthy appli...
 
The Next Best String
The Next Best StringThe Next Best String
The Next Best String
 
Acm aleppo cpc training eighth session
Acm aleppo cpc training eighth sessionAcm aleppo cpc training eighth session
Acm aleppo cpc training eighth session
 
Acm aleppo cpc training ninth session
Acm aleppo cpc training ninth sessionAcm aleppo cpc training ninth session
Acm aleppo cpc training ninth session
 
Grep - A powerful search utility
Grep - A powerful search utilityGrep - A powerful search utility
Grep - A powerful search utility
 
Checking the Open-Source Multi Theft Auto Game
Checking the Open-Source Multi Theft Auto GameChecking the Open-Source Multi Theft Auto Game
Checking the Open-Source Multi Theft Auto Game
 
Brogramming - Python, Bash for Data Processing, and Git
Brogramming - Python, Bash for Data Processing, and GitBrogramming - Python, Bash for Data Processing, and Git
Brogramming - Python, Bash for Data Processing, and Git
 
C language updated
C language updatedC language updated
C language updated
 
Regular Expression (RegExp)
Regular Expression (RegExp)Regular Expression (RegExp)
Regular Expression (RegExp)
 

Similaire à Common mistakes in C programming

2.Format Strings
2.Format Strings2.Format Strings
2.Format Stringsphanleson
 
Introduction to c
Introduction to cIntroduction to c
Introduction to camol_chavan
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 
COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docx
COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docxCOMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docx
COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docxdonnajames55
 
1.Buffer Overflows
1.Buffer Overflows1.Buffer Overflows
1.Buffer Overflowsphanleson
 
Getting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::CGetting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::Cdaoswald
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)Saifur Rahman
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparationKushaal Singla
 
100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projectsPVS-Studio
 
Complete c programming presentation
Complete c programming presentationComplete c programming presentation
Complete c programming presentationnadim akber
 
Error correction-and-type-of-error-in-c
Error correction-and-type-of-error-in-cError correction-and-type-of-error-in-c
Error correction-and-type-of-error-in-cMd Nazmul Hossain Mir
 
OSCON2014 : Quick Introduction to System Tools Programming with Go
OSCON2014 : Quick Introduction to System Tools Programming with GoOSCON2014 : Quick Introduction to System Tools Programming with Go
OSCON2014 : Quick Introduction to System Tools Programming with GoChris McEniry
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string libraryMomenMostafa
 

Similaire à Common mistakes in C programming (20)

2.Format Strings
2.Format Strings2.Format Strings
2.Format Strings
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docx
COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docxCOMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docx
COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docx
 
1.Buffer Overflows
1.Buffer Overflows1.Buffer Overflows
1.Buffer Overflows
 
Getting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::CGetting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::C
 
CInputOutput.ppt
CInputOutput.pptCInputOutput.ppt
CInputOutput.ppt
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
 
C
CC
C
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparation
 
100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects
 
Complete c programming presentation
Complete c programming presentationComplete c programming presentation
Complete c programming presentation
 
C++ How to program
C++ How to programC++ How to program
C++ How to program
 
Error correction-and-type-of-error-in-c
Error correction-and-type-of-error-in-cError correction-and-type-of-error-in-c
Error correction-and-type-of-error-in-c
 
OSCON2014 : Quick Introduction to System Tools Programming with Go
OSCON2014 : Quick Introduction to System Tools Programming with GoOSCON2014 : Quick Introduction to System Tools Programming with Go
OSCON2014 : Quick Introduction to System Tools Programming with Go
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
 
printf tricks
printf tricksprintf tricks
printf tricks
 
C programming day#1
C programming day#1C programming day#1
C programming day#1
 

Dernier

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 

Dernier (20)

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 

Common mistakes in C programming

  • 1. Article: CommonArticle: Common Mistakes In CMistakes In C ProgrammingProgramming By Khanh Ngo-DuyBy Khanh Ngo-Duy Khanhnd@elarion.comKhanhnd@elarion.com
  • 2. SeminarSeminar PurposePurpose Common MistakesCommon Mistakes struct and Memory Paddingstruct and Memory Padding New line characterNew line character Binary mode in fopen()Binary mode in fopen() strncpy()strncpy() memset()memset() fgets()fgets() Non-null-terminated stringNon-null-terminated string #include guard#include guard Get ID of a threadGet ID of a thread Buffer overflow, Stack overwriteBuffer overflow, Stack overwrite
  • 3. PurposePurpose Introduce common mistakes programmersIntroduce common mistakes programmers often gets into while writing C codeoften gets into while writing C code Get experiences to write better codesGet experiences to write better codes
  • 4. Common MistakesCommon Mistakes 1. struct and Memory Padding1. struct and Memory Padding (1 of 5)(1 of 5) //sizeof() = 12 struct myStruct { short s; int i; char c; };
  • 5. Common MistakesCommon Mistakes 1. struct and Memory Padding1. struct and Memory Padding (2 of 5)(2 of 5) //sizeof() = 8 struct myStruct { int i; short s; char c; }; //sizeof() = 8 struct myStruct { char c; short s; int i; };
  • 6. Common MistakesCommon Mistakes 1. struct and Memory Padding1. struct and Memory Padding (3 of 5)(3 of 5) Memory padding is done automatically byMemory padding is done automatically by compilercompiler Padding increases memoryPadding increases memory but makes app tobut makes app to run fasterrun faster Re-order variables in struct (ascending orRe-order variables in struct (ascending or descending) → you can reduce paddingdescending) → you can reduce padding ← your← your experienceexperience
  • 7. Common MistakesCommon Mistakes 1. struct and Memory Padding1. struct and Memory Padding (4 of 5)(4 of 5) Rules of padding:Rules of padding: A variable of a specific type will be aligned at offset = multiple of size ofA variable of a specific type will be aligned at offset = multiple of size of that variable. If it is not so, padding will be added before itthat variable. If it is not so, padding will be added before it Total size of struct = multiple of size of largest variable in struct. If it is notTotal size of struct = multiple of size of largest variable in struct. If it is not so, padding will be added at the end of struct.so, padding will be added at the end of struct. Example:Example: Variables of type int will be aligned at offset: 0, 4, 8, 12, 16 etc …Variables of type int will be aligned at offset: 0, 4, 8, 12, 16 etc … Variables of type char will be aligned at offset: 0, 1, 2, 3, 4, 5 etc …Variables of type char will be aligned at offset: 0, 1, 2, 3, 4, 5 etc … Variables of type pointer will be aligned at offset: 0, 8, 16, 24, 32 etc ...Variables of type pointer will be aligned at offset: 0, 8, 16, 24, 32 etc ...
  • 8. Common MistakesCommon Mistakes 1. struct and Memory Padding1. struct and Memory Padding (5 of 5)(5 of 5) Sometimes, you want to avoid memorySometimes, you want to avoid memory padding, you can usepadding, you can use #pragma pack (1)#pragma pack (1) directivedirective It is useful in some specific situationIt is useful in some specific situation Save memorySave memory but your app runs slowerbut your app runs slower #pragma pack(1) /* set alignment to 1 byte boundary */ struct MyPackedData /* sizeof() = 10 → x64 architecture */ { char Data1; long Data2; char Data3; }; #pragma pack(0) /* Back to normal */
  • 9. Common MistakesCommon Mistakes 2. New line character2. New line character New line character in Windows is different fromNew line character in Windows is different from Linux:Linux: In Windows, newline is denoted by 2 bytes: a combination of CarriageIn Windows, newline is denoted by 2 bytes: a combination of Carriage Return (ASCII value 13) and Line Feed (ASCII value 10)Return (ASCII value 13) and Line Feed (ASCII value 10) In Linux, newline is denoted by only 1 byte: the Line Feed character (ASCIIIn Linux, newline is denoted by only 1 byte: the Line Feed character (ASCII value 10)value 10)
  • 10. Common MistakesCommon Mistakes 3. Binary mode in fopen()3. Binary mode in fopen() FILE *FILE *fopen(fopen(const char *const char *path,path, const char *const char *mode);mode); In Windows,In Windows, text-modetext-mode andand binary-modebinary-mode areare differentiated. e.gdifferentiated. e.g “r”“r”,, “rb”“rb”,, “w”“w”,, “wb”“wb” …… In Linux, there is no text-mode. fopen()In Linux, there is no text-mode. fopen() alw aysalw ays open file inopen file in binary-modebinary-mode. So,. So, “r”“r” andand “rb”“rb” areare the same. There is no error whether you passthe same. There is no error whether you pass “b”“b” or notor not – fopen(“myFile.txt”, “r”);fopen(“myFile.txt”, “r”); /* prefer to use this *//* prefer to use this */ – fopen(“myFile.txt”, “rb”);fopen(“myFile.txt”, “rb”); /* In Linux, both lines are same! *//* In Linux, both lines are same! */
  • 11. Common MistakesCommon Mistakes 4. strncpy()4. strncpy() (1 of 2)(1 of 2) char *char *strncpy(strncpy(char *char *dest,dest, const char *const char *src,src, size_tsize_t n);n); strncpy() always tries to copystrncpy() always tries to copy nn character fromcharacter from srcsrc intointo destdest. If. If (m<n)(m<n) chars are copied →chars are copied → (n-m)(n-m) number of zeros will be filled intonumber of zeros will be filled into destdest →→ always copiesalways copies nn characters intocharacters into destdest So, the following codes mightSo, the following codes might C R AS H!!!C R AS H!!! charchar str[str[55];]; strncpy(str,strncpy(str, “abc”“abc”,, 1010);); /* Will copy “abc” and 7 zeros into str *//* Will copy “abc” and 7 zeros into str */
  • 12. Common MistakesCommon Mistakes 4. strncpy()4. strncpy() (2 of 2)(2 of 2) The following codes is redundantThe following codes is redundant – charchar str[str[1010];]; – memset (str,memset (str, 00,, 1010);); /* ← No need, strncpy() will do the thing *//* ← No need, strncpy() will do the thing */ – strncpy(str,strncpy(str, “abc”“abc”,, 1010);); /* Will copy “abc” and 7 zeros into str *//* Will copy “abc” and 7 zeros into str */
  • 13. Common MistakesCommon Mistakes 5. memset()5. memset() OnlyOnly use memset() to initialize variables touse memset() to initialize variables to ZEROZERO N E V E RN E V E R use memset() to initialize variables touse memset() to initialize variables to any values rather than zeroany values rather than zero Since, memset() fills memory with units inSince, memset() fills memory with units in bytebyte
  • 14. Common MistakesCommon Mistakes 6. fgets()6. fgets() char *char *fgets(fgets(char *char *s,s, intint size,size, FILE *FILE *stream);stream); fgets() only reads at mostfgets() only reads at most (size -1)(size -1) chars fromchars from streamstream intointo ss and then addsand then adds '0''0' at the end ofat the end of ss It reads only (size -1) charactersIt reads only (size -1) characters
  • 15. Common MistakesCommon Mistakes 6. Non-null-terminated string6. Non-null-terminated string When working withWhen working with non-null-terminatednon-null-terminated string,string, do not usedo not use “%s”“%s”. Instead, use. Instead, use “%.*s”“%.*s” – voidvoid display(display(charchar **msg)msg) – {{ • printf(printf(“The msg: %.256s”“The msg: %.256s”, msg);, msg); • printf(printf(“The msg: %.*s”“The msg: %.*s”,, 256256, msg);, msg); /* does the same thing *//* does the same thing */ – }}
  • 16. Common MistakesCommon Mistakes 7. #include guard7. #include guard (1 of 3)(1 of 3) Problem:Problem: When compiling main.c: mylib.h is included twice → declarations areWhen compiling main.c: mylib.h is included twice → declarations are overwrittenoverwritten mylib.h is openedmylib.h is opened tw ic etw ic e → compiler time→ compiler time definitelydefinitely increasesincreases main.c file1.h file2.h mylib.h #include “file1.h” #include “file2.h” #include “mylib.h” /* something belongs to file1 */ #include “mylib.h” /* something belongs to file2 */ extern int i;
  • 17. Common MistakesCommon Mistakes 7. #include guard7. #include guard (2 of 3)(2 of 3) S olution:S olution: #inc lude g uard#inc lude g uard When compiling main.c: mylib.h is included once!When compiling main.c: mylib.h is included once! Depends on compiler (supports include guard optimisation or not): mylib.hDepends on compiler (supports include guard optimisation or not): mylib.h is openedis opened onc eonc e oror tw ic etw ic e → compiler time may reduce or not→ compiler time may reduce or not Most of compilers support “include guard optimisation feature”: the includeMost of compilers support “include guard optimisation feature”: the include guard is cached at the first call, later the file (mylib.h) will not be opened →guard is cached at the first call, later the file (mylib.h) will not be opened → compiler time is fastercompiler time is faster main.c file1.h file2.h mylib.h #include “file1.h” #include “file2.h” #include “mylib.h” /* something belongs to file1 */ #include “mylib.h” /* something belongs to file2 */ #ifndef MYLIB_H #define MYLIB_H extern int i; #endif
  • 18. Common MistakesCommon Mistakes 7. #include guard7. #include guard (3 of 3)(3 of 3) S olution:S olution: #inc lude g uard (optimized)#inc lude g uard (optimized) When compiling main.c: mylib.h is included once!When compiling main.c: mylib.h is included once! mylib.h is openedmylib.h is opened onc eonc e → faster→ faster Must insert #ifndef everywhere when calling #include → only use with veryMust insert #ifndef everywhere when calling #include → only use with very large project to reduce the compiler timelarge project to reduce the compiler time Us eles sUs eles s if the compiler supports “include guard optimisation feature”if the compiler supports “include guard optimisation feature” main.c file1.h file2.h mylib.h #include “file1.h” #include “file2.h” #ifndef MYLIB_H #include “mylib.h” #endif /* something belongs to file1 */ #ifndef MYLIB_H #include “mylib.h” #endif /* something belongs to file2 */ #ifndef MYLIB_H #define MYLIB_H extern int i; #endif
  • 19. Common MistakesCommon Mistakes 8. Get ID of a thread8. Get ID of a thread Get ID of a process is easy (Get ID of a process is easy (pid_tpid_t getpid(getpid(voidvoid););). How). How about thread?about thread? #include#include <sys/syscall.h><sys/syscall.h> #define#define gettid()gettid() syscall(__NR_gettid)syscall(__NR_gettid) printf (printf ( "Thread ID: %dn""Thread ID: %dn", gettid() );, gettid() );
  • 20. Common MistakesCommon Mistakes 9. Buffer Overflow, Stack Overwrite9. Buffer Overflow, Stack Overwrite Don't write the following codes, it will overwriteDon't write the following codes, it will overwrite some important data in your application:some important data in your application: intint GlobalBuffer[GlobalBuffer[1010][][2020];]; voidvoid InitializeBuffer()InitializeBuffer() {{ intint i, j;i, j; forfor (i =(i = 00; I <; I < 100100; i++); i++) /* You go out off the boundary *//* You go out off the boundary */ forfor (j =(j = 00; j <; j < 2121; j++); j++) /* Again, go out off the boundary *//* Again, go out off the boundary */ GlobalBuffer[i][j] =GlobalBuffer[i][j] = 00;; }} Youthinkyouwillnever besilly likethis? Yep, u'r rite, but sometimesyoumake mistake like this!Youthinkyouwillnever besilly likethis? Yep, u'r rite, but sometimesyoumake mistake like this!
  • 21. Thanks for watchingThanks for watchingIf you see it useful → clap your hands :-)If you see it useful → clap your hands :-)