Segmentation fault

B

bumps

Guys,

I am getting segmentation fault while I am trying to open a file in a
small function.

int PingSamplesList::saveListElements (char *outputFileString)
{
PingSampleElement *tmpPtr;
int rc = 0;
ofstream outFile;
char lineBuffer[2048];
char *tmpLine = lineBuffer;
cout << "PingSamplesList::saveListElements to file: " <<
outputFileString;

outFile.open(outputFileString, ios::eek:ut | ios::app);

tmpPtr = (PingSampleElement *)head;

while (tmpPtr != NULL)
{
sprintf(tmpLine, "%12.6f %9lf %9lf %9lf %0.4f
%9lf %9lf %9lf %0.4f \n",
tmpPtr->timeStamp,
tmpPtr->up_p_estimate,
tmpPtr->up_p_d_estimate,
tmpPtr->up_p_d_new_estimate,
tmpPtr->up_LossRate,
tmpPtr->dn_p_estimate,
tmpPtr->dn_p_d_estimate,
tmpPtr->dn_p_d_new_estimate,
tmpPtr->dn_LossRate
);
outFile << tmpLine ;
tmpPtr = (PingSampleElement *) tmpPtr->next;
}
outFile.close();
return rc;
}

When the function gets called the first time, it works perfectly. But
when it gets called the second time, it crashes with segmentation
faults. With some debugging statements I found that the program crashes
in the line

outFile.open(outputFileString, ios::eek:ut | ios::app);

and outputFileString variable is NOT NULL.

Backtrace from GDB

#0 0x420741df in malloc_consolidate () from /lib/i686/libc.so.6
#1 0x42073ab7 in _int_malloc () from /lib/i686/libc.so.6
#2 0x42073155 in malloc () from /lib/i686/libc.so.6
#3 0x400ab2fe in operator new(unsigned) (sz=1108521344) at
.../../../../libstdc++-v3/libsupc++/new_op.cc:48
#4 0x400ab44f in operator new[](unsigned) (sz=1108521344) at
.../../../../libstdc++-v3/libsupc++/new_opv.cc:36
#5 0x4009235d in std::basic_filebuf said:
::_M_allocate_internal_buffer() (this=0x4212b180)
at
/usr/src/build/146482-i386/BUILD/gcc-3.2-20020903/obj-i386-redhat-linux/i386-redhat-linux/libstdc++-v3/include/bits/fstream.tcc:52
#6 0x40092185 in std::basic_filebuf said:
::eek:pen(char const*, std::_Ios_Openmode) (this=0x4212b180,
__s=0x4212b180 "\001", __mode=17)
at
/usr/src/build/146482-i386/BUILD/gcc-3.2-20020903/obj-i386-redhat-linux/i386-redhat-linux/libstdc++-v3/include/bits/fstream.tcc:91
#7 0x0805d741 in PingSamplesList::saveListElements(String&) ()
#8 0x0804b88c in metricClient::doClient() ()
#9 0x0804ac28 in main ()
#10 0x420158d4 in __libc_start_main () from /lib/i686/libc.so.6


Any help is really appreciated!!

-PS
 
V

Victor Bazarov

bumps said:
I am getting segmentation fault while I am trying to open a file in a
small function.

int PingSamplesList::saveListElements (char *outputFileString)
{
PingSampleElement *tmpPtr;

Don't declare anything before you actually need it. Muddies the waters.
int rc = 0;
ofstream outFile;

There is no need to separately define it and then open it later. Open
it right where you need it.
char lineBuffer[2048];

The buffer is not initialised. You might consider = {0}; for clarity.
(It's not necessary, though).
char *tmpLine = lineBuffer;
cout << "PingSamplesList::saveListElements to file: " <<
outputFileString;

And what does it print here when you call it the second time?
outFile.open(outputFileString, ios::eek:ut | ios::app);

tmpPtr = (PingSampleElement *)head;

What's "head"? Some member? Consider commenting what you post.

Also, you might [re]consider your class implementation. Why do
you need a C-style cast here? What type does "head" have?
while (tmpPtr != NULL)
{
sprintf(tmpLine, "%12.6f %9lf %9lf %9lf %0.4f
%9lf %9lf %9lf %0.4f \n",
tmpPtr->timeStamp,
tmpPtr->up_p_estimate,
tmpPtr->up_p_d_estimate,
tmpPtr->up_p_d_new_estimate,
tmpPtr->up_LossRate,
tmpPtr->dn_p_estimate,
tmpPtr->dn_p_d_estimate,
tmpPtr->dn_p_d_new_estimate,
tmpPtr->dn_LossRate
);
outFile << tmpLine ;
tmpPtr = (PingSampleElement *) tmpPtr->next;

Again, what type does 'next' have? Do you really need to cast here?
}
outFile.close();

There is no need to separately close it. The destructor will close.
return rc;
}

When the function gets called the first time, it works perfectly. But
when it gets called the second time, it crashes with segmentation
faults. With some debugging statements I found that the program crashes
in the line

outFile.open(outputFileString, ios::eek:ut | ios::app);

and outputFileString variable is NOT NULL.

It's not NULL, but it contains something that is not a file name.

You probably screw up the contents of 'outputFileString' array in some
way _outside_ this function between the first and the second calls so
that 'open' refuses to accept it as a name. Could it be you delete[]
it but forget to make it NULL?
Backtrace from GDB

#0 0x420741df in malloc_consolidate () from /lib/i686/libc.so.6
#1 0x42073ab7 in _int_malloc () from /lib/i686/libc.so.6
#2 0x42073155 in malloc () from /lib/i686/libc.so.6
#3 0x400ab2fe in operator new(unsigned) (sz=1108521344) at
../../../../libstdc++-v3/libsupc++/new_op.cc:48
#4 0x400ab44f in operator new[](unsigned) (sz=1108521344) at
../../../../libstdc++-v3/libsupc++/new_opv.cc:36
#5 0x4009235d in std::basic_filebuf said:
::_M_allocate_internal_buffer() (this=0x4212b180)

at
/usr/src/build/146482-i386/BUILD/gcc-3.2-20020903/obj-i386-redhat-linux/i386-redhat-linux/libstdc++-v3/include/bits/fstream.tcc:52
#6 0x40092185 in std::basic_filebuf said:
::eek:pen(char const*, std::_Ios_Openmode) (this=0x4212b180,

__s=0x4212b180 "\001", __mode=17)

.. ^^^^^^ Seems like your filename is screwed.
at
/usr/src/build/146482-i386/BUILD/gcc-3.2-20020903/obj-i386-redhat-linux/i386-redhat-linux/libstdc++-v3/include/bits/fstream.tcc:91
#7 0x0805d741 in PingSamplesList::saveListElements(String&) ()
^^^^^^^^
That's not the same signature you posted above. The one above has char*.
#8 0x0804b88c in metricClient::doClient() ()
#9 0x0804ac28 in main ()
#10 0x420158d4 in __libc_start_main () from /lib/i686/libc.so.6

V
 
R

red floyd

bumps said:
Guys,

I am getting segmentation fault while I am trying to open a file in a
small function.

int PingSamplesList::saveListElements (char *outputFileString)
{
PingSampleElement *tmpPtr;
int rc = 0;
ofstream outFile;
char lineBuffer[2048];
char *tmpLine = lineBuffer;
cout << "PingSamplesList::saveListElements to file: " <<
outputFileString;

outFile.open(outputFileString, ios::eek:ut | ios::app);

tmpPtr = (PingSampleElement *)head;

while (tmpPtr != NULL)
{
sprintf(tmpLine, "%12.6f %9lf %9lf %9lf %0.4f
%9lf %9lf %9lf %0.4f \n",
tmpPtr->timeStamp,
tmpPtr->up_p_estimate,
tmpPtr->up_p_d_estimate,
tmpPtr->up_p_d_new_estimate,
tmpPtr->up_LossRate,
tmpPtr->dn_p_estimate,
tmpPtr->dn_p_d_estimate,
tmpPtr->dn_p_d_new_estimate,
tmpPtr->dn_LossRate
);
outFile << tmpLine ;
tmpPtr = (PingSampleElement *) tmpPtr->next;
}
outFile.close();
return rc;
}

When the function gets called the first time, it works perfectly. But
when it gets called the second time, it crashes with segmentation
faults. With some debugging statements I found that the program crashes
in the line

outFile.open(outputFileString, ios::eek:ut | ios::app);

and outputFileString variable is NOT NULL.

Backtrace from GDB

#0 0x420741df in malloc_consolidate () from /lib/i686/libc.so.6
#1 0x42073ab7 in _int_malloc () from /lib/i686/libc.so.6
#2 0x42073155 in malloc () from /lib/i686/libc.so.6
#3 0x400ab2fe in operator new(unsigned) (sz=1108521344) at
../../../../libstdc++-v3/libsupc++/new_op.cc:48
#4 0x400ab44f in operator new[](unsigned) (sz=1108521344) at
../../../../libstdc++-v3/libsupc++/new_opv.cc:36
#5 0x4009235d in std::basic_filebuf said:
::_M_allocate_internal_buffer() (this=0x4212b180)

at
/usr/src/build/146482-i386/BUILD/gcc-3.2-20020903/obj-i386-redhat-linux/i386-redhat-linux/libstdc++-v3/include/bits/fstream.tcc:52
#6 0x40092185 in std::basic_filebuf said:
::eek:pen(char const*, std::_Ios_Openmode) (this=0x4212b180,

__s=0x4212b180 "\001", __mode=17)
at
/usr/src/build/146482-i386/BUILD/gcc-3.2-20020903/obj-i386-redhat-linux/i386-redhat-linux/libstdc++-v3/include/bits/fstream.tcc:91
#7 0x0805d741 in PingSamplesList::saveListElements(String&) ()
#8 0x0804b88c in metricClient::doClient() ()
#9 0x0804ac28 in main ()
#10 0x420158d4 in __libc_start_main () from /lib/i686/libc.so.6


Any help is really appreciated!!

-PS

First of all, the stack dump you provided doesn't match the code fragment.

Your fragment has a signature

int PingSamplesList::saveListElements (char *outputFileString)

Your stackdump as a signature

<sometype> PingSamplesList::saveListElements (String&)

1. What is "String"?
2. What is "head"?

You need to provide more info.
 
J

Jay Nabonne

Backtrace from GDB

#0 0x420741df in malloc_consolidate () from /lib/i686/libc.so.6
#1 0x42073ab7 in _int_malloc () from /lib/i686/libc.so.6
#2 0x42073155 in malloc () from /lib/i686/libc.so.6
#3 0x400ab2fe in operator new(unsigned) (sz=1108521344) at
../../../../libstdc++-v3/libsupc++/new_op.cc:48
#4 0x400ab44f in operator new[](unsigned) (sz=1108521344) at
../../../../libstdc++-v3/libsupc++/new_opv.cc:36

It's dying in new/malloc. Most likely, you've corrupted the heap
somewhere, probably by writing off the end of some allocated memory. It's
highly likely the problem lies somewhere else besides the code you
listed (whatever code gets executed between the first and second call to
your function).

- Jay
 
P

Prawit Chaivong

bumps said:
Guys,

I am getting segmentation fault while I am trying to open a file in a
small function.

int PingSamplesList::saveListElements (char *outputFileString)
{
PingSampleElement *tmpPtr;
int rc = 0;
ofstream outFile;
char lineBuffer[2048];
char *tmpLine = lineBuffer;
cout << "PingSamplesList::saveListElements to file: " <<
outputFileString;

outFile.open(outputFileString, ios::eek:ut | ios::app);

tmpPtr = (PingSampleElement *)head; What is head?

while (tmpPtr != NULL)
{
sprintf(tmpLine, "%12.6f %9lf %9lf %9lf %0.4f
%9lf %9lf %9lf %0.4f \n",
tmpPtr->timeStamp,
tmpPtr->up_p_estimate,
tmpPtr->up_p_d_estimate,
tmpPtr->up_p_d_new_estimate,
tmpPtr->up_LossRate,
tmpPtr->dn_p_estimate,
tmpPtr->dn_p_d_estimate,
tmpPtr->dn_p_d_new_estimate,
tmpPtr->dn_LossRate
);
Is tmpLine big enought? If not, may be the problem if it's in heap.
And from your corestack, it seems to me like heap corrupt.

The problem could be in somewhere else beside this function. because
accessing unallocated heap cause undefind behaviour.
outFile << tmpLine ;
tmpPtr = (PingSampleElement *) tmpPtr->next;
}
outFile.close();
return rc;
}

When the function gets called the first time, it works perfectly. But
when it gets called the second time, it crashes with segmentation
faults. With some debugging statements I found that the program crashes
in the line

outFile.open(outputFileString, ios::eek:ut | ios::app);

and outputFileString variable is NOT NULL.

Backtrace from GDB

#0 0x420741df in malloc_consolidate () from /lib/i686/libc.so.6
#1 0x42073ab7 in _int_malloc () from /lib/i686/libc.so.6
#2 0x42073155 in malloc () from /lib/i686/libc.so.6
#3 0x400ab2fe in operator new(unsigned) (sz=1108521344) at
../../../../libstdc++-v3/libsupc++/new_op.cc:48
#4 0x400ab44f in operator new[](unsigned) (sz=1108521344) at
../../../../libstdc++-v3/libsupc++/new_opv.cc:36
#5 0x4009235d in std::basic_filebuf said:
::_M_allocate_internal_buffer() (this=0x4212b180)
at
/usr/src/build/146482-i386/BUILD/gcc-3.2-20020903/obj-i386-redhat-linux/i386-redhat-linux/libstdc++-v3/include/bits/fstream.tcc:52
#6 0x40092185 in std::basic_filebuf said:
::eek:pen(char const*, std::_Ios_Openmode) (this=0x4212b180,
__s=0x4212b180 "\001", __mode=17)
at
/usr/src/build/146482-i386/BUILD/gcc-3.2-20020903/obj-i386-redhat-linux/i386-redhat-linux/libstdc++-v3/include/bits/fstream.tcc:91
#7 0x0805d741 in PingSamplesList::saveListElements(String&) ()
#8 0x0804b88c in metricClient::doClient() ()
#9 0x0804ac28 in main ()
#10 0x420158d4 in __libc_start_main () from /lib/i686/libc.so.6


Any help is really appreciated!!

-PS
 
P

Prawit Chaivong

Please ignore previous post.
tmpLine isn't in heap, it's in stack

But I insist the last paragraph that I posted.
And I'd like to know what is "head"

Regards,
 
B

bumps

Victor said:
bumps said:
I am getting segmentation fault while I am trying to open a file in a
small function.

int PingSamplesList::saveListElements (char *outputFileString)
{
PingSampleElement *tmpPtr;

Don't declare anything before you actually need it. Muddies the waters.
int rc = 0;
ofstream outFile;

There is no need to separately define it and then open it later. Open
it right where you need it.
char lineBuffer[2048];

The buffer is not initialised. You might consider = {0}; for clarity.
(It's not necessary, though).
char *tmpLine = lineBuffer;
cout << "PingSamplesList::saveListElements to file: " <<
outputFileString;

And what does it print here when you call it the second time?

The file name is getting printed perfectly !

What's "head"? Some member? Consider commenting what you post.

head points to the Head of linked list. I am basically dumping the
contents of linked list after a specific period.


Also, you might [re]consider your class implementation. Why do
you need a C-style cast here? What type does "head" have?

head is of type Class "SampleElement" and the class "PingSampleElement"
inherits that class.


Again, what type does 'next' have? Do you really need to cast here?

YES



There is no need to separately close it. The destructor will close.


It's not NULL, but it contains something that is not a file name.

I don't think so. Coz, right before i call open, I am printing the
filename and its printing perfectly.


You probably screw up the contents of 'outputFileString' array in some
way _outside_ this function between the first and the second calls so
that 'open' refuses to accept it as a name. Could it be you delete[]
it but forget to make it NULL?
Backtrace from GDB

#0 0x420741df in malloc_consolidate () from /lib/i686/libc.so.6
#1 0x42073ab7 in _int_malloc () from /lib/i686/libc.so.6
#2 0x42073155 in malloc () from /lib/i686/libc.so.6
#3 0x400ab2fe in operator new(unsigned) (sz=1108521344) at
../../../../libstdc++-v3/libsupc++/new_op.cc:48
#4 0x400ab44f in operator new[](unsigned) (sz=1108521344) at
../../../../libstdc++-v3/libsupc++/new_opv.cc:36
#5 0x4009235d in std::basic_filebuf said:
::_M_allocate_internal_buffer() (this=0x4212b180)

at
/usr/src/build/146482-i386/BUILD/gcc-3.2-20020903/obj-i386-redhat-linux/i386-redhat-linux/libstdc++-v3/include/bits/fstream.tcc:52
#6 0x40092185 in std::basic_filebuf said:
::eek:pen(char const*, std::_Ios_Openmode) (this=0x4212b180,

__s=0x4212b180 "\001", __mode=17)

. ^^^^^^ Seems like your filename is screwed.
at
/usr/src/build/146482-i386/BUILD/gcc-3.2-20020903/obj-i386-redhat-linux/i386-redhat-linux/libstdc++-v3/include/bits/fstream.tcc:91
#7 0x0805d741 in PingSamplesList::saveListElements(String&) ()
^^^^^^^^
That's not the same signature you posted above. The one above has char*.


true...i was debugging with char* instead of custom class String
later(didn't make any difference) and forgot to edit the argument
before i posted the code..sorry abt tht..
 
B

bumps

I tried replacing C++ ofstream with C's FILE, fopen and now its working
properly...i wonder watz the problem...but still interested in finding
out the bug...



Guys,

I am getting segmentation fault while I am trying to open a file in a
small function.

int PingSamplesList::saveListElements (char *outputFileString)
{
PingSampleElement *tmpPtr;
int rc = 0;
ofstream outFile;
char lineBuffer[2048];
char *tmpLine = lineBuffer;
cout << "PingSamplesList::saveListElements to file: " <<
outputFileString;

outFile.open(outputFileString, ios::eek:ut | ios::app);

tmpPtr = (PingSampleElement *)head;

while (tmpPtr != NULL)
{
sprintf(tmpLine, "%12.6f %9lf %9lf %9lf %0.4f
%9lf %9lf %9lf %0.4f \n",
tmpPtr->timeStamp,
tmpPtr->up_p_estimate,
tmpPtr->up_p_d_estimate,
tmpPtr->up_p_d_new_estimate,
tmpPtr->up_LossRate,
tmpPtr->dn_p_estimate,
tmpPtr->dn_p_d_estimate,
tmpPtr->dn_p_d_new_estimate,
tmpPtr->dn_LossRate
);
outFile << tmpLine ;
tmpPtr = (PingSampleElement *) tmpPtr->next;
}
outFile.close();
return rc;
}

When the function gets called the first time, it works perfectly. But
when it gets called the second time, it crashes with segmentation
faults. With some debugging statements I found that the program crashes
in the line

outFile.open(outputFileString, ios::eek:ut | ios::app);

and outputFileString variable is NOT NULL.

Backtrace from GDB

#0 0x420741df in malloc_consolidate () from /lib/i686/libc.so.6
#1 0x42073ab7 in _int_malloc () from /lib/i686/libc.so.6
#2 0x42073155 in malloc () from /lib/i686/libc.so.6
#3 0x400ab2fe in operator new(unsigned) (sz=1108521344) at
../../../../libstdc++-v3/libsupc++/new_op.cc:48
#4 0x400ab44f in operator new[](unsigned) (sz=1108521344) at
../../../../libstdc++-v3/libsupc++/new_opv.cc:36
#5 0x4009235d in std::basic_filebuf said:
::_M_allocate_internal_buffer() (this=0x4212b180)
at
/usr/src/build/146482-i386/BUILD/gcc-3.2-20020903/obj-i386-redhat-linux/i386-redhat-linux/libstdc++-v3/include/bits/fstream.tcc:52
#6 0x40092185 in std::basic_filebuf said:
::eek:pen(char const*, std::_Ios_Openmode) (this=0x4212b180,
__s=0x4212b180 "\001", __mode=17)
at
/usr/src/build/146482-i386/BUILD/gcc-3.2-20020903/obj-i386-redhat-linux/i386-redhat-linux/libstdc++-v3/include/bits/fstream.tcc:91
#7 0x0805d741 in PingSamplesList::saveListElements(String&) ()
#8 0x0804b88c in metricClient::doClient() ()
#9 0x0804ac28 in main ()
#10 0x420158d4 in __libc_start_main () from /lib/i686/libc.so.6


Any help is really appreciated!!

-PS
 
P

Prawit Chaivong

It's quite strange.
Your function is:
int PingSamplesList::saveListEleme­nts (char *outputFileString)
But in callstack
#7 0x0805d741 in PingSamplesList::saveListEleme­nts(String&) ()

char* and String&

Do I miss something?
 
B

bumps

my bad....

correct version ->
int PingSamplesList::saveListElements (String& outputFileString)

and String is a custom Class..

I was debugging with char* and forgot to replace that with String &
before I posted...
 
P

Prawit Chaivong

Can I see the implementation of your String class?
And I'd like to know what is "head" as well.

Thank you very much
 
B

bumps

allrite.....

class

class String {

private:
char *stringData;
int size;
int noDeleteFlag;

public:
~String();
String();
String(char *data);
String(char *data, int dataSize);
int length();
char * getBytes(); //closest to Java's return of a byte[] ???
char * getString(); //returns a ptr to the string
int attachData(char *stringData); //no copy of data...
int attachData( char * data, int noDeleteFlagParam); //caller can
retain ownership
int subset(char *stringData);
int equals(char *stringData);
int equals(String & stringData);
int endIndexOf(String & str);
int indexOf(String & str);
int indexOf(int beginIndex,String & str);
int indexOf(char *str);
String * substring(int beginIndex, int endIndex);
String& operator=(String&);
};


String::String()
{
noDeleteFlag = 0;
size = 0;
stringData = NULL;
}

String::String( char * stringParam)
{
noDeleteFlag = 0;
size = strlen(stringParam);
stringData = new char[size+1];
strcpy(stringData,stringParam);
}

String::String( char * dataPtr, int dataSize)
{
noDeleteFlag = 0;
size = dataSize;
stringData = new char[size+1];
bcopy(dataPtr, stringData,dataSize);
stringData[size] = 0x0; //make a string
}

Destructor

String::~String()
{
if ((stringData != NULL) && (noDeleteFlag == 0)) {
delete(stringData);
}
}


char * String::getString()
{
return((char *)stringData);
}



I am not posting source of other member functions as they are not
relavant to the current problem. Let me know if you need more source

thanks!
 
B

bumps

head is a pointer to an object of type class SampleElement and
PingSampleElement inherits the class SampleElement.

head points to the start of linked list..
 
V

Victor Bazarov

bumps said:
allrite.....

class

class String {

private:
char *stringData;
int size;
int noDeleteFlag;

public:
~String();
String();
String(char *data);
String(char *data, int dataSize);

Where is the copy-constructor?
int length();
char * getBytes(); //closest to Java's return of a byte[] ???
char * getString(); //returns a ptr to the string
int attachData(char *stringData); //no copy of data...
int attachData( char * data, int noDeleteFlagParam); //caller can
retain ownership
int subset(char *stringData);
int equals(char *stringData);
int equals(String & stringData);
int endIndexOf(String & str);
int indexOf(String & str);
int indexOf(int beginIndex,String & str);
int indexOf(char *str);
String * substring(int beginIndex, int endIndex);
String& operator=(String&);
};


String::String()
{
noDeleteFlag = 0;
size = 0;
stringData = NULL;
}

String::String( char * stringParam)
{
noDeleteFlag = 0;
size = strlen(stringParam);
stringData = new char[size+1];
strcpy(stringData,stringParam);
}

String::String( char * dataPtr, int dataSize)
{
noDeleteFlag = 0;
size = dataSize;
stringData = new char[size+1];
bcopy(dataPtr, stringData,dataSize);
stringData[size] = 0x0; //make a string
}

Destructor

String::~String()
{
if ((stringData != NULL) && (noDeleteFlag == 0)) {
delete(stringData);
}
}

[...]

I am not posting source of other member functions as they are not
relavant to the current problem. Let me know if you need more source

No more source needed. Read about "the Rule of Three".

And, believe me, you're much better off _not_ implementing your own
'String' and instead using 'std::string'. It will allow you to
concentrate on the problem at hand.

V
 
V

Victor Bazarov

bumps said:
head is a pointer to an object of type class SampleElement and
PingSampleElement inherits the class SampleElement.

head points to the start of linked list..

Just to keep up with C++, replace the C-style cast in your code
with 'static_cast'.

V
 
D

Default User

bumps said:
I tried replacing C++ ofstream with C's FILE, fopen and now its working
properly...i wonder watz the problem...but still interested in finding
out the bug...


Please don't top-post. Your reply text belongs following or
interspersed with the quotes.



Brian
 
D

Default User

bumps said:
head is a pointer to an object of type class SampleElement and
PingSampleElement inherits the class SampleElement.

head points to the start of linked list..


Please use the quoting feature for ALL your replies.




Brian
 
P

Prawit Chaivong

bumps said:
allrite.....

class

class String {

private:
char *stringData;
int size;
int noDeleteFlag;

public:
~String();
String();
String(char *data);
String(char *data, int dataSize);
int length();
char * getBytes(); //closest to Java's return of a byte[] ???
char * getString(); //returns a ptr to the string
int attachData(char *stringData); //no copy of data...
int attachData( char * data, int noDeleteFlagParam); //caller can
retain ownership
int subset(char *stringData);
int equals(char *stringData);
int equals(String & stringData);
int endIndexOf(String & str);
int indexOf(String & str);
int indexOf(int beginIndex,String & str);
int indexOf(char *str);
String * substring(int beginIndex, int endIndex);
String& operator=(String&);
};


String::String()
{
noDeleteFlag = 0;
size = 0;
stringData = NULL;
}

String::String( char * stringParam)
{
noDeleteFlag = 0;
size = strlen(stringParam);
stringData = new char[size+1];
strcpy(stringData,stringParam);
}

String::String( char * dataPtr, int dataSize)
{
noDeleteFlag = 0;
size = dataSize;
stringData = new char[size+1];
bcopy(dataPtr, stringData,dataSize);
stringData[size] = 0x0; //make a string
}

Destructor

String::~String()
{
if ((stringData != NULL) && (noDeleteFlag == 0)) {
delete(stringData);
}
}


char * String::getString()
{
return((char *)stringData);
}



I am not posting source of other member functions as they are not
relavant to the current problem. Let me know if you need more source

thanks!

Another question bumps.
Did you use outputFileString.getString() as a parameter when you were
trying to open file?
Like this:
outFile.open(outputFileString.getString(), ios::eek:ut | ios::app);
 
B

bumps

Prawit said:
bumps said:
allrite.....

class

class String {

private:
char *stringData;
int size;
int noDeleteFlag;

public:
~String();
String();
String(char *data);
String(char *data, int dataSize);
int length();
char * getBytes(); //closest to Java's return of a byte[] ???
char * getString(); //returns a ptr to the string
int attachData(char *stringData); //no copy of data...
int attachData( char * data, int noDeleteFlagParam); //caller can
retain ownership
int subset(char *stringData);
int equals(char *stringData);
int equals(String & stringData);
int endIndexOf(String & str);
int indexOf(String & str);
int indexOf(int beginIndex,String & str);
int indexOf(char *str);
String * substring(int beginIndex, int endIndex);
String& operator=(String&);
};


String::String()
{
noDeleteFlag = 0;
size = 0;
stringData = NULL;
}

String::String( char * stringParam)
{
noDeleteFlag = 0;
size = strlen(stringParam);
stringData = new char[size+1];
strcpy(stringData,stringParam);
}

String::String( char * dataPtr, int dataSize)
{
noDeleteFlag = 0;
size = dataSize;
stringData = new char[size+1];
bcopy(dataPtr, stringData,dataSize);
stringData[size] = 0x0; //make a string
}

Destructor

String::~String()
{
if ((stringData != NULL) && (noDeleteFlag == 0)) {
delete(stringData);
}
}


char * String::getString()
{
return((char *)stringData);
}



I am not posting source of other member functions as they are not
relavant to the current problem. Let me know if you need more source

thanks!

Another question bumps.
Did you use outputFileString.getString() as a parameter when you were
trying to open file?
Like this:
outFile.open(outputFileString.getString(), ios::eek:ut | ios::app);



Yes...i am using outputFileString.getString() as parameter...
 
R

Ralph D. Ungermann

bumps said:
String::String( char * stringParam)
{
noDeleteFlag = 0;
size = strlen(stringParam);
stringData = new char[size+1];

operator new[]
strcpy(stringData,stringParam);
}

String::~String()
{
if ((stringData != NULL) && (noDeleteFlag == 0)) {
delete(stringData);

operator delete -- where are the brackets?


This might corrupt your heap.


Ralph
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top