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 7356 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: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |