- Bits & Bytes
- Command-line Arguments
- Dynamischer Speicher
- sizeof
- Strukturen
- File I/O
- Pipes
- Ausbesserungsbeispiel
output = output & 0x80;
output &= (1 << 7);
output = output | 0x10;
output |= (1 << 4);
output = output ^ 0x01;
output ^= (1 << 0);
int main (int argc, char *argv[])
{
int count;
printf ("This program was called with \"%s\".\n",argv[0]);
if (argc > 1)
{
for (count = 1; count < argc; count++)
{
printf("argv[%d] = %s\n", count, argv[count]);
}
}
else
{
printf("The command had no other arguments.\n");
}
return 0;
}
size an (in Bytes angegeben)|
1int main()
{
2 int* mem = (int*)malloc(sizeof(int) * 2);
3 if(mem == NULL) return 0;
4 *mem = 1;
5 *(mem + 1) = 2;
6 mem[0] = 3;
7 mem[1] = 4;
8 free(mem);
return 0;
}
|
|
NULL initialisierenfree auf NULL setzenmalloc/realloc auf NULL überprüfenrealloc Hilfspointer verwenden:
char* text = (char*)malloc(8 * sizeof(char));
char* new_text = (char*)realloc(text, 16 * sizeof(char));
if(new_text != NULL)
{
text = new_text;
}
else
{
free(text);
printf("Out of memory!\n");
}
|
1int* x = (int*)malloc(3 * sizeof(int));
2x = (int*)realloc(x, 16);
3if(x == NULL)
{
4 free(x);
5 printf("Out of memory!");
}
|
|
|
1int* x = (int*)malloc(3 * sizeof(int));
2int* new_x = (int*)realloc(x, 16);
3if(new_x != NULL)
{
x = new_x;
}
4else
{
5 free(x);
6 printf("Out of memory!\n");
}
|
|
int charsize = sizeof(char);
int ptrsize = sizeof(int*);
int size = sizeof("Hallo");int x[20]; int len = sizeof(x) / sizeof(x[0]);
int* x = (int*)malloc(100); int len = sizeof(x);
int x[20]; int* y = x; int len = sizeof(y) / sizeof(y[0]);
char* hallo = "Hallo"; int size = sizeof(hallo);
int* x = (int*)malloc(10);
int* x = (int*)malloc(10 * sizeof(int));
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char* text = (char*)malloc(6 * sizeof(char));
strcpy(text, "Hallo!");
printf("%s\n", text);
return 0;
}
Kompilieren:
clang -Wall -g -o valgrind_test valgrind_test.cValgrind:
valgrind --leak-check=full ./valgrind_test
#include <stdio.h>
struct _Person_
{
char* name_;
int age_;
};
int main()
{
struct _Person_ petra;
petra.name_ = "Petra";
petra.age_ = 17;
return 0;
}
typedef:
[...]
typedef struct _Person_
{
char* name_;
int age_;
} Person;
[...]
Person petra;
petra.name_ = "Petra";
petra.age_ = 17;
[...]
[...]
typedef struct _Person_
{
char* name_;
int age_;
} Person;
[...]
Person* petra = (Person*)malloc(sizeof(Person));
petra->name_ = "Petra";
petra->age_ = 17;
[...]
free(petra);
struct _Person_
{
char* name_;
int age_;
struct _Person_* father_;
struct _Person_* mother_;
};
#include <stdio.h>
typedef struct _FileParserReturn_
{
char file_present_ : 1;
char file_open_failed_ : 1;
char ignored_ : 6;
} FileParserReturn;
FileParserReturn parseConfigFile( ... )
{
FileParserReturn return_value;
...
return_value.file_present_ = 1;
return return_value;
}
#defines"
#include <stdio.h>
typedef enum _FileParserReturn_
{
FILE_PRESENT,
FILE_OPEN_FAILED
} FileParserReturn;
FileParserReturn parseConfigFile( ... )
{
...
return FILE_PRESENT;
}
fopen(const char * filename, const char * mode);
fread(void * dst, size_t size, size_t items, FILE * file);
fwrite(const void * data, size_t size, size_t items, FILE * file);
struct in Datei speichernstructs können in Binärdateien gespeichert werden
[...]
struct _Phone_
{
int country_code_;
int number_;
}__attribute__((packed));
[...]
struct _Phone_ number;
number.number_ = 1234567;
number.country_code_ = 43;
FILE* file = fopen("phone_numbers", "wb"); // wb = write binary
fwrite(&number, sizeof(struct _Phone_), 1, file);
fclose(file);
struct aus Datei lesenstructs können aus Binärdateien geladen werden
[...]
struct _Phone_
{
int country_code_;
int number_;
}__attribute__((packed));
[...]
struct _Phone_ number;
FILE* file = fopen("phone_numbers", "rb"); // rb = read binary
fread(&number, sizeof(struct _Phone_), 1, file);
fclose(file);
struct in Datei gespeichert werden soll
|
|
||||||||||||||||||||||||
long size; fseek(file, 0, SEEK_END); size = ftell(file); fseek(file, 0, SEEK_SET);
Möglichkeit, die Ausgabe der einzelnen Befehle umzuleiten bzw. an andere Befehle weiterzuleiten.
./programm > ausgabe.txt
./programm < eingabe.txt
./programm 2> error.txt
./programm < input.txt 1> output.txt 2> error.txt
./programm | tail
echo $?