Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *by Cooler_
- c00f3r[at]gmail[dot]com
- gcc -o code code.c; ./code
- professor de estrutura de dados
- falo que não tinha como usar ponteiro
- para reter uma string sem usar malloc()
- bom tem milhares de formas sem malloc()
- olhem algumas xD
- */
- #include <unistd.h>
- #include <string.h>
- #include <alloca.h>
- void main(void)
- {
- //1 memset()
- char *x;
- // \0 nullbyte para evitar bug :-X
- memset(&x,'\0',4);
- memset(&x,'o',3);
- memset(&x,'l',2);
- memset(&x,'a',1);
- puts(&x);
- //2 usando sbrk()
- // sizeof(char)*4 == 1byte X 4, cada byte para uma letra e sobra uma para null byte
- char *z=(char *)sbrk(sizeof(char)*4);
- z="omg";
- puts(z);
- // subtrair a memória para desalocar "man sbrk"
- z=(char *)sbrk(-sizeof(char)*4);
- //3 usando STACK inves da HEAP para alocar na memória
- char *y=(char *)alloca(sizeof(char)*4);
- y="yes";
- puts(y);
- // não precisa dar free() pois he STACK
- //4 apontando para um array de char
- char string[4];
- char *ptr=string;
- ptr="foo";
- puts(ptr);
- // tem mais formas como calloc()...
- }
Advertisement
Add Comment
Please, Sign In to add comment