By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,854 Members | 2,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,854 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.