[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Memory problem

Posted on 2006-06-05
8
Medium Priority
?
280 Views
Last Modified: 2010-04-01
The following program I wrote is crashing on delete stmt.
I need delete to free  the memory allocated in encrypt().

#include<iostream.h>
#include<string.h>

#define COUNT 4
const int key[ COUNT ] = { 1,2,5,2 };

char* encrypt( char* str )
{
      int length = strlen( str );
      int val =0, num=0;
      char * store = new char[length];

      for( int idx=0; idx< length; idx++){
            if( num==COUNT){
                  num=0;
            }
            val = key[num];
            num++;
            
            char c = str[idx] + val;
            store[idx] = c;
      
      }
      store[length] = 0;
      return store;
}

char* decrypt( char* str );
int main()
{

      char* str = "testtesttest";
      char* str1 = encrypt(str );

      cout<<"Before: "<<str;
      cout<<"After: "<<str1;

      delete str1;//crashing here...why????


      return 0;
}
0
Comment
Question by:openujs
  • 4
  • 2
  • 2
8 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 80 total points
ID: 16839013
>      char * store = new char[length];

You want
     char * store = new char[length+1];

strlen() does not include the NUL terminator in the count.  You need to allocate an extra byte to hold the NUL your assign here:
     store[length] = 0;
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 16839024
Oh, and "//crashing here...why????"  Since you overwrote your allocated block by one byte, you corrupted the memory allocation arena, hence the crash when you call delete.
0
 
LVL 8

Expert Comment

by:List244
ID: 16839792
You delete an array using:

delete [] ARRAYHERE

NOT

delete ARRAYHERE

Change it to:

delete [] str1;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 8

Expert Comment

by:List244
ID: 16839802
Once you change your delete to delete properly and set your length +1 as BrettMJohnson stated, you should have it compiling.
0
 

Author Comment

by:openujs
ID: 16842425
thanks guys..
i knew that how did i overlook that.
0
 
LVL 8

Expert Comment

by:List244
ID: 16843235
Openujs, in the future, you should use the split points option when more than one persons
comments have helped to solve your problem.
0
 

Author Comment

by:openujs
ID: 16844594
sure..i need to figure out how to split points.
0
 
LVL 8

Expert Comment

by:List244
ID: 16844709
You can not do that on a closed question.  However, when closing a question, there is a link directly above
the type-box (Where you type your message) that says "Split points."  You can use that to split points rather
than accepting just one answer.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question