473,396 Members | 1,990 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.

Please help me in memory corruption

I have a function in which memory has been allocated using new and not being deleted. The function is as below:

void AddItem(char* buffer, char* items[], int& index)
{
char* temp = new char[strlen(buffer) + 1];

strcpy(temp, buffer);

items[index] = temp;

index++;
}

This function AddItem() has been called 52 times inside the program.
Then I think this is causing some memory issue or its slowing down the program. Please let me know what corrective action can I take.
Thanks.
Jul 31 '07 #1
11 1900
Meetee
931 Expert Mod 512MB
I have a function in which memory has been allocated using new and not being deleted. The function is as below:

void AddItem(char* buffer, char* items[], int& index)
{
char* temp = new char[strlen(buffer) + 1];

strcpy(temp, buffer);

items[index] = temp;

index++;
}

This function AddItem() has been called 52 times inside the program.
Then I think this is causing some memory issue or its slowing down the program. Please let me know what corrective action can I take.
Thanks.
Use this in your function
Expand|Select|Wrap|Line Numbers
  1. delete [] temp;
That will solve memory deallocation problem.

Regards
Jul 31 '07 #2
Use this in your function
Expand|Select|Wrap|Line Numbers
  1. delete [] temp;
That will solve memory deallocation problem.

Regards

But here its copying temp to item[index].
if I will delete temp inside the function it will loose the memory to item[index].
Please let me know.
Jul 31 '07 #3
Meetee
931 Expert Mod 512MB
But here its copying temp to item[index].
if I will delete temp inside the function it will loose the memory to item[index].
Please let me know.
Expand|Select|Wrap|Line Numbers
  1. void AddItem(char* buffer, char* items[], int& index)
  2. {
  3. char* temp = new char[strlen(buffer) + 1];
  4.  
  5. strcpy(temp, buffer);
  6.  
  7. items[index] = temp;
  8.  
  9. index++;
  10.  
  11. delete [] temp;
  12. }
Value of temp is already added in items[index] before deleting temp. So this will not loose any data.

Regards
Jul 31 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
This is incorrect:
Value of temp is already added in items[index] before deleting temp. So this will not loose any data.
True, the value of temp is added in items[index]. However, temp is a pointer so all that been added to items[index] is the address inside temp.

Deleting temp inside the function will delete the string pointed at by
items[index] and that can cuase a program crash when items[index] is used by the calling function later on.

To the OP:
Your code looks fine. 52 calls is not an issue. Even if the buffer had thousands of bytes, you wouldn't notice a slowdown. There's something else going on.

One thing that comes to mind is sorting the array of char* by making copies of the strings rather than swapping the pointers. I need to see where you use this items array.
Jul 31 '07 #5
Here I am pasting the lines of code where it is calling the AddItem() function.

Expand|Select|Wrap|Line Numbers
  1. int PopulateTradeEvents( sDOC_SOURCE* DocSource, char* items[], int& index )
  2. {
  3.    int count = 0;
  4.    int status = sSUCCESS;
  5.    int maxCount = 5;
  6.  
  7.    char buffer[128] = "";
  8.  
  9.    if ( DocSource->Function == sDF_INCEPTION )
  10.    {
  11.       if ( dmgGenTrEvSched( &(DocSource->Trade.Base.Env->cTrEvTerms),
  12.                             &(DocSource->Trade.Base.Env->cTrEvSchd),
  13.                             00 ) == sSUCCESS )
  14.       {
  15.          // GPM 203 RI 247 Additional fields for RT_TERM fields for Swift MT360 message
  16.          if ( DocSource->TradeType == sTT_SWAP )
  17.          {
  18.             for ( count = 0;
  19.                   count < DocSource->Trade.Base.Env->cTrEvTerms.List.ItemsUsed;
  20.                   count++ )
  21.             {
  22.                cTREVTERM* pTrevTerm = (cTREVTERM*)sGetListItem( &DocSource->Trade.Base.Env->cTrEvTerms, count );
  23.                if ( pTrevTerm->EvType == cRT_TERM )
  24.                {
  25.                   char Buf[sMIN_STRING];
  26.  
  27.                   // Calculate Trade Date + Spot for Trade Currency
  28.                   sIDATE TradeSpot = sCcySpotDate( sTradeCcy( &DocSource->Trade, 01 ),
  29.                                                    DocSource->Trade.Base.Env->TradeDate );
  30.  
  31.                   // sIDATE Tenor = pTrevTerm->StartDate - TradeSpot;
  32.                   sprintf( buffer, "TradeEventTerm[%d].SpotTradeDate=<Date>%s;\n",
  33.                                    count, sISybDate( Buf, TradeSpot ) );
  34.                   AddItem( buffer, items, index );
  35.                }
  36.             }
  37.          }
  38.  
  39.          for ( count = 0;
  40.                count < DocSource->Trade.Base.Env->cTrEvSchd.List.ItemsUsed;
  41.                count++ )
  42.          {
  43.             cTREVSCHD* pTrevSched = (cTREVSCHD*)sGetListItem( &DocSource->Trade.Base.Env->cTrEvSchd, count );
  44.  
  45.             char Buf[sMIN_STRING];
  46.  
  47.             sprintf ( buffer, "TradeEventSched[%d].EventType=\"%s\";\n",
  48.                       count, sEditString( NULL, sICEnum( "cTRDEVTYPE",
  49.                                                          pTrevSched->EvType,
  50.                                                          sSHORT_ENUM ), sNO_SPACES ) );
  51.  
  52.             AddItem(buffer, items, index);
  53.  
  54.             sprintf ( buffer, "TradeEventSched[%d].EventDate=<Date>%s;\n",
  55.                       count, sISybDate( Buf, pTrevSched->Date ) );
  56.             AddItem(buffer, items, index);
  57.  
  58.             sprintf ( buffer, "TradeEventSched[%d].NoticeDate=<Date>%s;\n",
  59.                       count, sISybDate( Buf, pTrevSched->NoticeDate ) );
  60.  
  61.             AddItem(buffer, items, index);
  62.  
  63.             sprintf ( buffer, "TradeEventSched[%d].PayDate=<Date>%s;\n",
  64.                       count, sISybDate( Buf, pTrevSched->PayDate ) );
  65.  
  66.             AddItem(buffer, items, index);
  67.  
  68.             sprintf ( buffer, "TradeEventSched[%d].WhoseOpt=\"%s\";\n",
  69.                       count, sEditString( NULL, sICEnum( "cWHOSEOPT",
  70.                                                          pTrevSched->WhoseOpt,
  71.                                                          sSHORT_ENUM ), sNO_SPACES ) );
  72.  
  73.             AddItem(buffer, items, index);
  74.  
  75.  
  76.             // eTrack 3644: Limit size of Sched, otherwise incres will crash
  77.             if ( count == maxCount  )
  78.                break;
  79.          }
  80.       }
  81.       else
  82.       {
  83.          sLogMessage( "Error generating Trade Events for Trade %s",
  84.                       sSERIOUS,
  85.                       0,
  86.                       DocSource->Trade.Base.TradeID.Num );
  87.  
  88.          status = sERROR;
  89.       }
  90.    }
  91.  
  92.    return ( status );
  93. }
  94.  
Please help me, my deliverable is on Friday. Thanks.
Aug 1 '07 #6
JosAH
11,448 Expert 8TB
Please help me, my deliverable is on Friday. Thanks.
Is the buffer size large enough? (count carefully) Are there enough slots in the
items[] array?

kind regards,

Jos
Aug 1 '07 #7
buffer size is 128.

char buffer[128] = "";

But you asked
Are there enough slots in the items[] array. ??????????????
I am not clear what do you mean by slots in the items[] array?

Thanks
Aug 1 '07 #8
JosAH
11,448 Expert 8TB
buffer size is 128.

char buffer[128] = "";

But you asked
Are there enough slots in the items[] array. ??????????????
I am not clear what do you mean by slots in the items[] array?

Thanks
slots == elements, sorry for the alien terminology: how long is that 'items[]'
array? i.e. how many string elements can you store in it? Enough?

kind regards,

Jos
Aug 1 '07 #9
#define MAX_ITEMS 1000
char* items[MAX_ITEMS];

That is , it can be at best 1000.
Thanks.
Aug 1 '07 #10
JosAH
11,448 Expert 8TB
#define MAX_ITEMS 1000
char* items[MAX_ITEMS];

That is , it can be at best 1000.
Thanks.
What do you see when you print out that 'index' reference parameter in your
AddItem function? (print out the length of the string parameter as well, just to
be sure).

kind regards,

Jos
Aug 1 '07 #11
weaknessforcats
9,208 Expert Mod 8TB
Also, your buffer is 128 but you never check the return from any of your
sprintf() calls to see a) there were more than 128 bytes written to the buffer or b) there was a -1 indicating an sprintf() failure.

You might change your buffer size of 128 to something so big you know it's safe and see if your corruption goes away.
Aug 1 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Richard Gabriel | last post by:
Hi everyone, Since we upgraded to MySQL 4.0.13 from 3.23, we have been getting table corruption often. It happens about twice per week (with about 500 queries per second average). I have even...
5
by: Noa Garnett | last post by:
I'm developing on C++, using visual studio 6.0 with service pack 5. I have a memory corruption while debugging. Some of the variables I'm using are suddenly set to zero while progressing along the...
9
by: prabhat143 | last post by:
Hi, I was recently asked to write a function in C that would detect if memory is corrupted. I had no clue about the solution but what I believe is that the solution is not complicated. Does...
10
by: eyh5 | last post by:
Hi, My C code (running on Soalris Unix) has some "segmentation fault" that I wish to use purify to do it. I poked around the web, and found some information about adding some lines in a Makefile...
4
by: indushekara | last post by:
Hi, We are having memory corruption in our application somewhere, unable to find out. one part of code we found that we are specifying wrong format specifier. Could anyone let me know if the...
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
4
by: Harsha | last post by:
Hi All, There is some memory corruption in my application. It is due to the fact that it is multithreaded. Is this due to some missing mutex locking? Is there any way to find where it is...
4
by: Tomassus | last post by:
Hi there, I have a problem with dynamic memory allocation. I know that it would have been easier to use vectors methods, but i want to know what i do here wrong. This is one of my methods in...
14
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using VS2005 and .net 2.0. I'm creating an application that has 3 forms. I want allow users to move forward and backward with the forms and retain the data users have entered. I thought...
66
by: Why Tea | last post by:
typedef struct some_struct { int i; short k, int m; char s; } some_struct_t; Assuming 16 bit or 32-bit alignment, can I assume that s always gets 4 or 8 bytes of allocation due to padding
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.