Created
November 20, 2017 12:46
-
-
Save Et7f3/cd3acd49c239ea90926420bb0115f685 to your computer and use it in GitHub Desktop.
parser arg one malloc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
//#include <string.h> | |
struct s_argument | |
{ | |
int valc; //# de valeurs | |
char *opt; //Nom de l'option sans tiret | |
char **val; //Tableau d'argument de taille `valc` | |
}; | |
struct s_arg_parse | |
{ | |
unsigned int argc; //# d'arguments | |
struct s_arg_parse *suivant; //Vaut NULL si on est en bout de liste chainé. Permet de conserver les arguments et de nettoyer | |
struct s_argument *arg; //Tableau d'arguments de taille `argc` | |
char *cmd_line; //premier caractère de la Ligne de commande dans le TAS -=-> free(); | |
}; | |
struct a { | |
int b; | |
int pad; | |
struct e* c; | |
char *d; | |
}; | |
struct e{ | |
int f; | |
}; | |
struct s_arg_parse *gl_liste=NULL; | |
int main() | |
{ | |
int argc=2; | |
char ligne_commande[]="coucou je suis la ligne"; | |
int cmd_len=sizeof(ligne_commande)/sizeof(char); | |
gl_liste=(struct s_arg_parse *)malloc(sizeof(struct s_arg_parse)+argc*sizeof(struct s_argument)+sizeof(char)*cmd_len); | |
gl_liste->argc=argc; | |
gl_liste->suivant=NULL; | |
gl_liste->arg=sizeof(struct s_arg_parse); | |
gl_liste->cmd_line=sizeof(struct s_arg_parse)+argc*sizeof(struct s_argument); | |
free(gl_liste); | |
struct a* p=NULL; | |
struct e se={88}; | |
int size =4; | |
p=(struct a*)malloc(sizeof(struct a)+sizeof(struct e)*size+25*sizeof(char)); | |
p->c=(struct e*)(p+sizeof(struct a)); | |
p->d=(char*)(p+sizeof(struct a)+sizeof(struct e*)*size); | |
p->b=size; | |
p->c[0].f=88; | |
while(size--)printf("%d\n",p->c[size].f); | |
p->d="coucou"; | |
printf("%s,%d,%d,%d",p->d,sizeof(struct a),sizeof(struct e),sizeof(struct e*)); | |
free(p); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment