472,989 Members | 2,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 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 7321
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: 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=()=>{
2
isladogs
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...
0
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...
2
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...
4
NeoPa
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 :...
1
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...
0
isladogs
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...
3
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...
0
NeoPa
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...

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.