Core’u geniş Tutki Patlamasın :) (Segmentation Fault Core Dumped C++)
Ödev, Belçika, C++, Core Dumped, English, Linux, Memory Allocating 4 Yorum »İngilizce bilen kardeşler için bir açıklama yapayım
( maksat sitenin gavur ziyaretçiside olsun
)
For English Press 9:
I was coding a small program on C++ then I have the error;
master@slave:~/project/oop2$ g++ MyString.cc MyString.h -o trial
master@slave:~/project/oop2$ ./trial
Segmentation fault (core dumped)
It was because using a pointer without specifying its borders. It grows and grows then it violates the memory addresses belonged by other variables, methods or what ever. The solution is simple just reserving the needed memory. So how to do that? The answer is also simple. You should allocate the memory using malloc() function which is both available in C and C++.
for example you need a pointer to put inside 100 int.
int somenumber = 100; // the number of integer you will put in pointer
int *intPointer;
intPointer = (int *)malloc(somenumber * sizeof(int));
// malloc function needs the byte value of the needed area in memory
// that’s why you should multiply it with the byte value of an integer
// it is generally 4 in 32-bit and 64-bit processor aimed compilers
Don’t press 8 it is for Dutch
, my Dutch is still poor,
Press 7 for Turkish
Evet Türkçesine gelince olayın, Tercüme edin Allah Allah, beni uğraştırmayın
.
Neyse ben biraz bahsedeyim yine
;
Yazdığım programda bir adet int pointer kullanıyordum (çok adet te bir tanesi patlıyordu
) ve bu memoryde sınırlarını zorluyordu, hatta altüst edip programımı öldürüyordu. Aslında çok basit bir problem ama yeni başlayanların ve benim gibi unutup tekrar hatırlayanların sık karşılaştıkları ve harşılaşacakları bir problem bu.
Tek yapılması gereken ise işinize yarayacak kadar memory’i o değişken için ayırmanız. Bu işlem için örnek kod yukarda.
Umarım googledan gelen arkadaşların işine yarar, ve bana bol bol ziyaretçi getirir
Son Yorumlar