Skip to content

Instantly share code, notes, and snippets.

@ecylmz
Created November 19, 2011 20:01
Show Gist options
  • Save ecylmz/1379285 to your computer and use it in GitHub Desktop.
Save ecylmz/1379285 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *
tersle(char *dizgi)
{
int i, uzunluk;
char *ters_dizgi;
ters_dizgi = (char *) malloc(sizeof(char));
uzunluk = strlen(dizgi);
for (i = uzunluk; i >= 0; --i)
ters_dizgi[uzunluk - i - 1] = dizgi[i];
return ters_dizgi;
}
int
main(void)
{
char *dizgi;
dizgi = (char *) malloc(sizeof(char));
printf("Dizgiyi Girin: ");
scanf("%s",dizgi);
printf("%s\n", tersle(dizgi));
free(dizgi);
}
@roktas
Copy link

roktas commented Nov 19, 2011

bellek yönetiminde sorunlar var:

  • malloc(sizeof(char)) ile sadece tek karakterlik bir yer açıyorsun, bu yetmez değil mi? verilen dizgiyi çoğaltmak zorundasın.
  • sizeof(char) daima 1 bayt, malloc da bayt cinsinden yer ayırdığına göre malloc(sizeof(char)) yerine basitçe malloc(1) diyebilirsin (sadece not olarak geçiyorum).
  • gözün daima malloc sayısınca free arasın. bak burada bellek sızıntısı var (terslenmiş dizgi için ayrılan yer kaybolmuş).

şunu incele: https://gist.github.com/1379491

@ecylmz
Copy link
Author

ecylmz commented Nov 20, 2011

teşekkürler hocam :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment