PDA

Tam Sürümünü Görmek İçin : Segmetation Fault (Core Dumped)


dotnet
07/11/2007, 14:45
Merhabalar Efendim,

Ufak bir okul ödevi için ufak bir ödev yapıyorum, ve ufak bir class'ım var.
Yaptığı şey ise şu, benim constructor'da belirttiğim String'i alıp bir integer pointer'ın içerisine bit bit yazmak.

mystring.h dosyası

#ifndef MYSTRING_H
#define MYSTRING_H

#include <iostream>
#include <string.h>

using namespace std;

/**
@author İsmail Taha AYKAÇ <ismail@ieee.org>
*/
class MyString{
public:

MyString();
string str;

int *binChars;
char *buf;

char *strToChar();
int *charToBin();
};

#endif


mystring.cc dosyası

#include "mystring.h"

MyString::MyString()
{
str = "Watson, come here, I want you.";
// memset( binChars, 0, str.size() * 8 );
// binChars = charToBin();
char *a = strToChar();
}

char* MyString::strToChar()
{
char buff[30]; //= (char *)malloc(str.length() + 1);
memset( buff, '\0', 30 );
//int size = a.size();
str.copy(buff, str.size());

cout << sizeof(buff);

return &buff[0];
}

int *MyString::charToBin()
{
int i = 0, j, *retValue;


char bufChar, *charSet;
charSet = strToChar();

while(*charSet)
{
bufChar = *charSet;

for(j=0;j<8; i++, j++)
{
bufChar = bufChar << 1;
retValue[i] = (bufChar & 0x80)/128;
}

charSet++;

}

return retValue;
}


MyString Class'ının charToBin metodu, çalıştığında veya ben binChars pointerımı bir yerde kullandığımda

Segmentation Fault (Core Dumped)
hatası veriyor.

Not: compilor olara g++ kullanıyorum

Bir not daha: aynı kod bir şekilde çalışıyordu, bir şeyler yapdım çalışmaz oldu, deli oldum valla :D, yedek almadığıma pişman oldum.


myavuzselim
07/11/2007, 15:20
Kodun hepsine bakamladim ama...

char buff[30]; //= (char *)malloc(str.length() + 1);
...
return &buff[0];
Tekrar malloc yontemine don. Su anda buff stack uzerinde olusturuluyor, ve strToChar geri donunce onun icin ayrilan bolge baska ise kullaniliyor. O yuzden de geri dondurdugun &buff[0] anlamsiz oluyor, ve buyuk ihtimalle de bu segmentation fault'a yol aciyor.

belirttiğim String'i alıp bir integer pointer'ın içerisine bit bit yazmak
Yazdigin kodlar bu is icin cok karmasik geldi. Bunun icin bir sinif tanimlamana da gerek yok bence, su fonksiyonun icini doldur yeter:

int* bits(char* c) {
...
}

dotnet
11/11/2007, 16:37
Hocam daha başka klaslarımda olduğu için bunu klas yapmak işime geliyor, diğer klasların içinde kullanmak daha rahat oluyor.

Biraz ara vermiştim koda, o yüzden cevap biraz geç oldu...

Uzun süredir c++ ile haşır neşir olmadığımdan karşılaşılan basit sorunlar bile ilk başta garip görünüyor...

MyString.h

#include <iostream>
#include <string.h>

using namespace std;

class MyString
{
public:
string str;
int *binChars;

MyString();


void strToChar();
void charToBin(char);
void strToBin();

char *charBuff;
int intBuff[8];

};



MyString.cc

#include "MyString.h"



void MyString::strToChar()
{
if(str == "")
return;

charBuff = (char *)malloc(str.size() + 1);

str.copy(charBuff, str.size());
}

void MyString::charToBin(char a)
{
if(!*charBuff)
return;

for(int i = 0; i < 8 ; i++)
{
intBuff[i] = (a & 0x80)/0x80;
a = a << 1;
}
}

void MyString::strToBin()
{
if(str == "")
return;

int i, j;

strToChar();

for(i=0; *charBuff; i++)
{
charToBin(*charBuff);

for(j=0; j < 8; j++)
{
*binChars = intBuff[j];
binChars += 1;
}

charBuff += 1;
}
charBuff -= i;
binChars -= i * j;
}

MyString::MyString()
{
str = "Watson, come here, I want you.";

binChars = (int *)malloc(str.length() * 4 * 8 + 1);
/* stringteki karakter sayısı * integerın byte olarak büyüklüğü * 8 bit + 1 nolur nolmaz */

strToBin();

strToChar();

}

/*
int main()
{
MyString a;

int i, j;

for(i=0; *a.charBuff; i++)
{
cout << *a.charBuff << "\t" << (int)*a.charBuff << "\t";

a.charToBin(*a.charBuff);

for(j=0; j < 8; j++)
{
cout << *a.binChars << "\t";
a.binChars += 1;
}

cout << endl;

a.charBuff += 1;
}
a.charBuff -= i;
a.binChars -= i * j;
}
*/


MyString.cc dosyasının sonunda bir deneme fonksiyonu bulunmakta.

Malloc yöntemine döndüm ve problem çözüldü.

Demekki neymiş pointer kullanırken sınırları abartacaksak onları önce çizmemiz gerekiyormuş :)