Hi, I have the following code that is giving this error, I cant
simplify the code, I was just testing some theory for something we are
doing and was getting an issue here. Please someone point out whats
wrong with my code. -
class MsgData
-
{
-
char* data;
-
int size;
-
-
public:
-
-
MsgData(long l)
-
{
-
count++;
-
size = 0;
-
data = NULL;
-
operator=(l);
-
}
-
-
MsgData(char* d)
-
{
-
count++;
-
size = 0;
-
data = NULL;
-
if(d)
-
{
-
size = strlen(d);
-
data = new char[size];
-
strcpy(data, d);
-
printf("MsgData(%s) @%d\n", d, data);
-
}
-
}
-
-
MsgData(char* d, int s)
-
{
-
//printf("MsgData(%s, %d)\n", d, s);
-
data = NULL;
-
count++;
-
size = s;
-
data = new char[size];
-
printf("MsgData(%s, %d) @%d\n", d, s, data);
-
-
if(d)
-
{
-
memcpy(data, d, size);
-
data[size] = 0;
-
}
-
}
-
-
MsgData()
-
{
-
//printf("MsgData()\n");
-
count++;
-
data = NULL;
-
size = 0;
-
}
-
-
MsgData(const MsgData& m)
-
{
-
//printf("MsgData(MsgData(%s, %d))\n", m.data, m.size);
-
count++;
-
data = NULL;
-
size = 0;
-
copy(m);
-
}
-
-
~MsgData()
-
{
-
count--;
-
printf("~MsgData(%s, %d) @%d [count=%d]\n", data, size, data,
-
count);
-
delete [] data;
-
data = NULL;
-
//printf("count:%d\n", count);
-
}
-
-
MsgData& copy(char* d, int l)
-
{
-
printf("copy(%s, %d) @%d\n\n", d, l, data);
-
MsgData tmp(d, l);
-
copy(tmp);
-
}
-
-
MsgData& copy(char* d)
-
{
-
printf("copy(%s) @%d\n\n", d, data);
-
return copy(d, strlen(d));
-
}
-
-
MsgData& copy(const MsgData& rhd)
-
{
-
printf("copy(MsgData(%s, %d))\n\n", rhd.data, rhd.size);
-
if(data)
-
{
-
delete [] data;
-
data = NULL;
-
size = 0;
-
}
-
-
if(rhd.size)
-
{
-
size = rhd.size;
-
data = new char[size];
-
memcpy(data, rhd.data, size);
-
data[size] = 0;
-
}
-
-
return *this;
-
}
-
-
MsgData& operator=(long l)
-
{
-
//printf("operator=(%l)\n", l);
-
char tmp[16];
-
sprintf(tmp, "%d", l);
-
return copy(tmp);
-
}
-
-
MsgData& operator=(char* rhd)
-
{
-
printf("operator=(%s)\n\n", rhd);
-
if(!rhd)
-
return *this;
-
-
MsgData tmp(rhd);
-
return copy(tmp);
-
}
-
-
MsgData& operator=(MsgData rhd)
-
{
-
printf("operator=(MsgData(%s, %d)) @%d\n\n", rhd.data, rhd.size,
-
data);
-
return copy(rhd);
-
}
-
-
MsgData operator+(MsgData& rhd)
-
{
-
printf("operator+(MsgData(%s, %d) @%d\n\n", rhd.data, rhd.size,
-
data);
-
MsgData msg;
-
msg.append(*this);
-
msg.append(rhd);
-
printf("newly created append: %s\n", msg.tostr());
-
return msg;
-
}
-
-
MsgData& append(char* rhd)
-
{
-
MsgData msg(rhd);
-
return append(msg);
-
}
-
-
MsgData& append(MsgData& rhd)
-
{
-
if(data && size)
-
{
-
if(rhd.data && rhd.size)
-
{
-
char* tmp = data;
-
data = new char[size+rhd.size];
-
memcpy(data, tmp, size);
-
memcpy(data+size, rhd.data, rhd.size);
-
size +=rhd.size;
-
data[size] = 0;
-
delete [] tmp;
-
tmp = NULL;
-
}
-
return *this;
-
}
-
-
if(rhd.data && rhd.size)
-
{
-
return copy(rhd);
-
}
-
-
return *this;
-
}
-
-
MsgData operator+(char* str)
-
{
-
/*char* tmp = data;
-
data = new char[size + strlen(str)];
-
if(data)
-
memcpy(data, tmp, size);
-
memcpy(data+size, str, strlen(str));
-
size += strlen(str);
-
delete [] tmp;
-
return *this;*/
-
-
/*printf("operator+(%s) to %s\n", str, data);
-
MsgData tmp(0, size+strlen(str));
-
if(data)
-
sprintf(tmp.tostr(), "%s%s", data, str);
-
else
-
strcpy(tmp.tostr(), str);
-
return tmp;*/
-
-
printf("operator+(%s)\n\n", str);
-
MsgData msg(str);
-
return operator+(msg);
-
}
-
-
char operator[](int index)
-
{
-
//printf("operator[%d]\n", index);
-
if(data && (index >=0 && index < size))
-
return data[index];
-
-
return 0;
-
}
-
-
bool operator==(char* rhd)
-
{
-
MsgData tmp(rhd);
-
return operator==(tmp);
-
}
-
-
bool operator==(MsgData& rhd)
-
{
-
if(!data || !rhd.data)
-
return false;
-
-
if(size != rhd.size)
-
return false;
-
-
if(!strncmp(data, rhd.data, size))
-
return true;
-
-
return false;
-
}
-
-
char*& tostr()
-
{
-
//printf("tostr()\n");
-
return data;
-
}
-
};
-
-
And now the driver code which is giving me the issue -
int main()
-
{
-
MsgData m1, m2, m3, m4, m5;
-
m1 = "m1";
-
m2 = "m2";
-
m3 = "m3";
-
-
m4 = m4 + m1 + "123";
-
//m4 = m4 + m1 + m2 + m3 + "123" + "1";
-
printf("%s = %d----------------------------------------%d\n",
-
m4.tostr(), 0,0);
-
m4 = m4 + m1 + "123";
-
printf("%s = %d----------------------------------------%d\n",
-
m4.tostr(), 1,1);
-
m4 = m4 + " " + m1 + "123";
-
m4 = m4 + " " + m1 + "123";
-
m4 = m4 + " " + m1 + "123";
-
printf("%s = %d----------------------------------------%d\n",
-
m4.tostr(), 2,2);
-
-
-
printf("m4=%s\n", m4.tostr());
-
}
-
or if I change those m4 lines to then it again core dumps but if i
comment the 2nd line " m4 = m4 + m1 + "123"; " it works?? -
m4 = m4 + m1 + m2 + m3 + "123" + "1";
-
printf("%s = %d----------------------------------------%d\n",
-
m4.tostr(), 0,0);
-
m4 = m4 + m1 + "123";
-
printf("%s = %d----------------------------------------%d\n",
-
m4.tostr(), 1,1);
-
m4 = m4 + " " + m1 + "123";
-
printf("%s = %d----------------------------------------%d\n",
-
m4.tostr(), 2,2);
-
I will get the same problem...I cant seem to figure whats the
difference in doing this over and over again.
Please someone help! I just cant seem to figure this out.
Thanks
Ankur 2 7321 g3******@gmail.com schrieb:
Hi, I have the following code that is giving this error, I cant
simplify the code, I was just testing some theory for something we are
doing and was getting an issue here. Please someone point out whats
wrong with my code.
[code]
class MsgData
{
char* data;
int size;
public:
MsgData(long l)
{
count++;
'count' has not been defined anywhere.
size = 0;
data = NULL;
It's better to use initializer lists for this.
operator=(l);
}
Without looking to see what operator=() actually does, I will say
that it's an unclear way of writing this. Better would be to use a
private helper function, which both MsgData() and operator=() can
call internally.
>
MsgData(char* d)
This should probably be (const char *d), unless you intend to
modify whatever d points to.
{
count++;
size = 0;
data = NULL;
Same comments as previously.
if(d)
{
size = strlen(d);
You have a signedness mismatch and a possible size mismatch here.
data = new char[size];
You probably meant: new char[size + 1]...
strcpy(data, d);
....yes, that's what you meant. The line above has just corrupted
some memory somewhere.
printf("MsgData(%s) @%d\n", d, data);
Undefined behaviour. Use %p, not %d, to printf() pointers.
}
}
[Lots of similar quality code snipped.]
>
Please someone help! I just cant seem to figure this out.
I suggest that you study a C++ textbook, and start out with basic
(that is, short and uncomplicated) exercises. You are trying to do
too many things at once here. You're also using mainly C idioms
in place of conceptually simpler C++ idioms. It also appears that
you're using a pre-standard compiler, which will limit the usefulness
of any advice you receive here. g3******@gmail.com wrote:
Hi, I have the following code that is giving this error, I cant
simplify the code, I was just testing some theory for something we are
doing and was getting an issue here. Please someone point out whats
wrong with my code.
[code]
class MsgData
{
char* data;
int size;
If you just declared this as
std::string data;
You'd avoid having to mismanipulate string data (and possibly
even writing your own bug-ridden copy constructor, assignment
op, and destructors). This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bin Lu |
last post by:
I keep getting this malloc problem when my program tries to allocate
memory for some pointer. The statement is like:
rsv_cache = (rsvcache *) malloc (sizeof(rsvcache));
It goes through the...
|
by: Rano |
last post by:
/*
Hello,
I've got some troubles with a stupid program...
In fact, I just start with the C language and sometime
I don't understand how I really have to use malloc.
I've readden the FAQ...
|
by: Paminu |
last post by:
I get this error after running my application for some time. What does it
mean and what should I be looking for in my code?
|
by: Tushar |
last post by:
Hi all,
I am experiencing a wierd crash in malloc. There is one call with
malloc(208) and my application crashes in some 'MemPoolSetHighThreads'
funciton which is internally called by malloc.
...
|
by: quiberon2 |
last post by:
Hi,
Sorry if it might be a stupid question but what should returns
malloc(0) ?
void *ptr = malloc(0);
I am running gcc 3.3.5 and a non-null address is returned.
( in the compiler that I am...
|
by: Christopher Benson-Manica |
last post by:
Some recent posts got me thinking about how one might have dealt with
simplistic malloc() implementations which might return NULL for a 64K
request but might accept two 32K requests or four 16K...
|
by: itsolution |
last post by:
Hi folks,
Could you shed some light on this issue?
my program is running on Freebsd as a daemon. When user sends a
request, it forks itself and lets its child process handles the
request....
|
by: CJ |
last post by:
We were discussing implementing malloc(), in particular the following
situation.
Suppose the user requests 1Mb of memory. Unfortunately, we only have
512Kb available. In this situation, most...
|
by: The Doctor |
last post by:
Hey people,
I have a really weird problem.
I am developing a string class, just for fun. I am also writing an
application, which uses the string class, and uses a lot of them. But,
after about...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
| |