473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

malloc(): memory corruption (fast) [Cant seem to figure this out] on FC5 + G++ 4.1.0

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.
Expand|Select|Wrap|Line Numbers
  1. class MsgData
  2. {
  3. char* data;
  4. int size;
  5.  
  6. public:
  7.  
  8. MsgData(long l)
  9. {
  10. count++;
  11. size = 0;
  12. data = NULL;
  13. operator=(l);
  14. }
  15.  
  16. MsgData(char* d)
  17. {
  18. count++;
  19. size = 0;
  20. data = NULL;
  21. if(d)
  22. {
  23. size = strlen(d);
  24. data = new char[size];
  25. strcpy(data, d);
  26. printf("MsgData(%s) @%d\n", d, data);
  27. }
  28. }
  29.  
  30. MsgData(char* d, int s)
  31. {
  32. //printf("MsgData(%s, %d)\n", d, s);
  33. data = NULL;
  34. count++;
  35. size = s;
  36. data = new char[size];
  37. printf("MsgData(%s, %d) @%d\n", d, s, data);
  38.  
  39. if(d)
  40. {
  41. memcpy(data, d, size);
  42. data[size] = 0;
  43. }
  44. }
  45.  
  46. MsgData()
  47. {
  48. //printf("MsgData()\n");
  49. count++;
  50. data = NULL;
  51. size = 0;
  52. }
  53.  
  54. MsgData(const MsgData& m)
  55. {
  56. //printf("MsgData(MsgData(%s, %d))\n", m.data, m.size);
  57. count++;
  58. data = NULL;
  59. size = 0;
  60. copy(m);
  61. }
  62.  
  63. ~MsgData()
  64. {
  65. count--;
  66. printf("~MsgData(%s, %d) @%d [count=%d]\n", data, size, data,
  67. count);
  68. delete [] data;
  69. data = NULL;
  70. //printf("count:%d\n", count);
  71. }
  72.  
  73. MsgData& copy(char* d, int l)
  74. {
  75. printf("copy(%s, %d) @%d\n\n", d, l, data);
  76. MsgData tmp(d, l);
  77. copy(tmp);
  78. }
  79.  
  80. MsgData& copy(char* d)
  81. {
  82. printf("copy(%s) @%d\n\n", d, data);
  83. return copy(d, strlen(d));
  84. }
  85.  
  86. MsgData& copy(const MsgData& rhd)
  87. {
  88. printf("copy(MsgData(%s, %d))\n\n", rhd.data, rhd.size);
  89. if(data)
  90. {
  91. delete [] data;
  92. data = NULL;
  93. size = 0;
  94. }
  95.  
  96. if(rhd.size)
  97. {
  98. size = rhd.size;
  99. data = new char[size];
  100. memcpy(data, rhd.data, size);
  101. data[size] = 0;
  102. }
  103.  
  104. return *this;
  105. }
  106.  
  107. MsgData& operator=(long l)
  108. {
  109. //printf("operator=(%l)\n", l);
  110. char tmp[16];
  111. sprintf(tmp, "%d", l);
  112. return copy(tmp);
  113. }
  114.  
  115. MsgData& operator=(char* rhd)
  116. {
  117. printf("operator=(%s)\n\n", rhd);
  118. if(!rhd)
  119. return *this;
  120.  
  121. MsgData tmp(rhd);
  122. return copy(tmp);
  123. }
  124.  
  125. MsgData& operator=(MsgData rhd)
  126. {
  127. printf("operator=(MsgData(%s, %d)) @%d\n\n", rhd.data, rhd.size,
  128. data);
  129. return copy(rhd);
  130. }
  131.  
  132. MsgData operator+(MsgData& rhd)
  133. {
  134. printf("operator+(MsgData(%s, %d) @%d\n\n", rhd.data, rhd.size,
  135. data);
  136. MsgData msg;
  137. msg.append(*this);
  138. msg.append(rhd);
  139. printf("newly created append: %s\n", msg.tostr());
  140. return msg;
  141. }
  142.  
  143. MsgData& append(char* rhd)
  144. {
  145. MsgData msg(rhd);
  146. return append(msg);
  147. }
  148.  
  149. MsgData& append(MsgData& rhd)
  150. {
  151. if(data && size)
  152. {
  153. if(rhd.data && rhd.size)
  154. {
  155. char* tmp = data;
  156. data = new char[size+rhd.size];
  157. memcpy(data, tmp, size);
  158. memcpy(data+size, rhd.data, rhd.size);
  159. size +=rhd.size;
  160. data[size] = 0;
  161. delete [] tmp;
  162. tmp = NULL;
  163. }
  164. return *this;
  165. }
  166.  
  167. if(rhd.data && rhd.size)
  168. {
  169. return copy(rhd);
  170. }
  171.  
  172. return *this;
  173. }
  174.  
  175. MsgData operator+(char* str)
  176. {
  177. /*char* tmp = data;
  178. data = new char[size + strlen(str)];
  179. if(data)
  180. memcpy(data, tmp, size);
  181. memcpy(data+size, str, strlen(str));
  182. size += strlen(str);
  183. delete [] tmp;
  184. return *this;*/
  185.  
  186. /*printf("operator+(%s) to %s\n", str, data);
  187. MsgData tmp(0, size+strlen(str));
  188. if(data)
  189. sprintf(tmp.tostr(), "%s%s", data, str);
  190. else
  191. strcpy(tmp.tostr(), str);
  192. return tmp;*/
  193.  
  194. printf("operator+(%s)\n\n", str);
  195. MsgData msg(str);
  196. return operator+(msg);
  197. }
  198.  
  199. char operator[](int index)
  200. {
  201. //printf("operator[%d]\n", index);
  202. if(data && (index >=0 && index < size))
  203. return data[index];
  204.  
  205. return 0;
  206. }
  207.  
  208. bool operator==(char* rhd)
  209. {
  210. MsgData tmp(rhd);
  211. return operator==(tmp);
  212. }
  213.  
  214. bool operator==(MsgData& rhd)
  215. {
  216. if(!data || !rhd.data)
  217. return false;
  218.  
  219. if(size != rhd.size)
  220. return false;
  221.  
  222. if(!strncmp(data, rhd.data, size))
  223. return true;
  224.  
  225. return false;
  226. }
  227.  
  228. char*& tostr()
  229. {
  230. //printf("tostr()\n");
  231. return data;
  232. }
  233. };
  234.  
  235.  

And now the driver code which is giving me the issue

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. MsgData m1, m2, m3, m4, m5;
  4. m1 = "m1";
  5. m2 = "m2";
  6. m3 = "m3";
  7.  
  8. m4 = m4 + m1 + "123";
  9. //m4 = m4 + m1 + m2 + m3 + "123" + "1";
  10. printf("%s = %d----------------------------------------%d\n",
  11. m4.tostr(), 0,0);
  12. m4 =  m4 + m1 + "123";
  13. printf("%s = %d----------------------------------------%d\n",
  14. m4.tostr(), 1,1);
  15. m4 = m4 + " " + m1 + "123";
  16. m4 = m4 + " " + m1 + "123";
  17. m4 = m4 + " " + m1 + "123";
  18. printf("%s = %d----------------------------------------%d\n",
  19. m4.tostr(), 2,2);
  20.  
  21.  
  22. printf("m4=%s\n", m4.tostr());
  23. }
  24.  
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??

Expand|Select|Wrap|Line Numbers
  1. m4 = m4 + m1 + m2 + m3 + "123" + "1";
  2. printf("%s = %d----------------------------------------%d\n",
  3. m4.tostr(), 0,0);
  4. m4 =  m4 + m1 + "123";
  5. printf("%s = %d----------------------------------------%d\n",
  6. m4.tostr(), 1,1);
  7. m4 = m4 + " " + m1 + "123";
  8. printf("%s = %d----------------------------------------%d\n",
  9. m4.tostr(), 2,2);
  10.  
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

Aug 9 '06 #1
2 7356
trm
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.

Aug 9 '06 #2
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).
Aug 11 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
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...
7
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...
3
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?
6
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. ...
82
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...
17
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...
6
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....
43
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...
11
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...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
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...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.