The variable timeOld is being used without being initialized in c++ - c++

char* timeNew = _com_util::ConvertBSTRToString(cpi->getTime());
if(timeFirst == true)
{
strcpy(timeOld,timeNew);
timeFirst = false;
}
how can I initiliase timeold if I dont know what the size of character array returned by cpi->getTime is?

Allocate memory for it based on length of timeNew:
delete[] timeOld;
timeOld = new char[strlen(timeNew) + 1];
or you could make timeOld a std::string and let it manage memory for you:
std::string timeOld;
timeOld = timeNew; // If timeNew is dynamically allocated you must still
// delete[] it when no longer required, as timeOld
// takes a copy of timeNew, not ownership of timeNew.
You can access the const char* using std::string::c_str() if really required.

Use strings where possible:
char *t= _com_util::ConvertBSTRToString(cpi->getTime());
std::string timeNew(t);
delete[] t;
if(timeFirst == true)
{
timeOld=timeNew;
timeFirst = false;
}
if you don't have to manage the memory returned by teh function simply:
std::string timeNew(_com_util::ConvertBSTRToString(cpi->getTime()));
if(timeFirst == true)
{
timeOld=timeNew;
timeFirst = false;
}

If you have to use ConvertBSTRToString then use boost::scoped_array<char> or boost::shared_array<char> to ensure you get clean-up.
boost::shared_array<char> time;
time.reset( _com_util::ConvertBSTRtoString( cpi->getTime() );
automatically reallocates. No calls to delete or delete[] necessary.

Related

C++ error on delete[] after iterating through pointer

Environment: Windows 7 pro x64, Microsoft Visual Studio 2015 Enterprise, Version 14.0.25424.00 Update 3
int testFunction()
{
std::string _orig = "[188 80% (1/2)O:152]";
std::string _orig2 = "[999 99% (1/1)O:999]";
char *orig = NULL;
char *orig2 = NULL;
orig = new char[_orig.length() + 1];
strcpy(orig, _orig.c_str());
orig2 = new char[_orig2.length() + 1];
strcpy(orig2, _orig2.c_str());
*orig++;
*orig2++;
int a = atoi(orig);
int b = atoi(orig2);
delete[] orig;
delete[] orig2;
return 0;
}
Running the above code crashes with the "_CrtIsValidHeapPointer(block)" error.
If I don't iterate (*orig++ and *orig2++), then no issues.
So my question is, how can I iterate through the pointers and then when I'm done doing what I need to do with them, delete[] them correctly?
You did not delete the pointers you allocated!
delete must be called on the original memory address returned by new. Since you did orig++, you cant delete the address being pointed at!
Iterating can be done with an index, and using array subscription to dereference:
orig[i] = 'a';
Which is the same as doing this:
*(orig+i) = 'a';
Or you can get another pointer onto the same data, and modify this one.
char* pOrig = orig;
++pOrig;
Why did you write
*orig++; // why dereferencing?
Just ++ by itself would do the iteration.
Avoid to use raw pointers. Your code can be simpler:
std::string orig = "[188 80% (1/2)O:152]";
std::string orig2 = "[999 99% (1/1)O:999]";
int a = atoi(orig.c_str() + 1);
int b = atoi(orig2.c_str() + 1);
Your mistake is that you try to delete the shifted pointers instead of the original pointers. As the result heap manager gets wrong allocated block information usually put before the allocated pointer and you got heap corruption.
how can I iterate through the pointers and then when I'm done doing what I need to do with them, delete[] them correctly?
Create a copy of the pointer:
char* orig = new char[size];
char* i = orig;
*i++ = 'a';
delete orig;
A perhaps more common idiom is to dereference a temporary:
for(int i = 0; i < size - 1; i++)
orig[i] = 'a';
I would love to [use std::string], but I need to use atoi(), which won't work on std::string
You are mistaken. atoi works with std::string just fine. Simply use std::string::c_str() just like you did with strcpy. There is absolutely no reason to allocate a block of memory with new.
int testFunction()
{
std::string _orig = "[188 80% (1/2)O:152]";
int a = 0;
for (std::string::iterator it = _orig.begin(); it != _orig.end(); ++it)
{
if (isdigit((char)*it))
a = (atoi(it._Ptr));
}
return 0;
}
I got it. Thanks for everyone who helped me come to this conclusion. Staying with std::string was in fact the best approach.

c++ Access violation writing location 0x00000000

I have the following code:
In Client.cpp there is the constructor that is doing memory allocation for the array "conturi" of type ContBancar.
Client::Client(string nume, string prenume, string adresa)
{
this->nume = nume;
this->prenume = prenume;
this->adresa = adresa;
nrCont = 0;
ContBancar** conturi = new ContBancar*[50];
}
Then there is a method that is adding a account in the "conturi" array:
void Client::adaugaCont(ContBancar* contNou)
{
conturi[nrCont++] = contNou;
}
This is my code in Main.cpp:
ContBancar ron1 ("1.r", 0, Banca::RON);
Client ioana ("Pop", "Ioana", "Str Dorobantilor 3/4");
ioana.adaugaCont(&ron1);
But it gives me the access violation error at runtime, like the array 'conturi' has no memory allocated. I don't understand why, because the memory should be allocated in the constructor.
Could anyone help me with this?
Client::Client(string nume, string prenume, string adresa)
{
this->nume = nume;
this->prenume = prenume;
this->adresa = adresa;
nrCont = 0;
//here is your problem!
ContBancar** conturi = new ContBancar*[50];
}
You redefine conturi as a new array, with the pointer to it stored in the local scope of the constructor.
Change the line to:
conturi = new ContBancar*[50];
and you will then have the object's conturi pointer pointing to the allocated memory.
This will also solve the memory leak you have introduced.
(pointer to heap goes out of scope. memory on heap is leaked)
Or even better, use a std::vector.
In Class definition:
std::vector<ContBancar> conturi;
You don't have to manage the memory yourself with new and delete and you are not restricted to a fixed number of elements either.
You're declaring a new pointer variable with the following line:
ContBancar** conturi = new ContBancar*[50];
and the pointer variable will get destroyed at the end of the function call and leak memory leaving any other same-name member variable untouched.
You should rather use the member variable conturi(assuming you have one from the rest of the code):
Client::Client(string nume, string prenume, string adresa)
{
this->nume = nume;
this->prenume = prenume;
this->adresa = adresa;
nrCont = 0;
conturi = new ContBancar*[50];
}
or you can use a std::vector<ContBancar> conturi member variable which might be easier to use and to deal with.
This is a simple reproducer to better understand the problem:
class Client {
public:
int** conturi = 0;
void function() {
int** conturi = new int*[50];
}
};
int main()
{
Client obj;
obj.function();
if(obj.conturi == 0)
std::cout << "this is still zero"; // This will get printed
}
Example

C++ - Delete std::string*; heap corruption

I'm relatively new to C++ memory management, and I'm getting this weird error of heap corruption (plus an automatic breakpoint in Visual Studio before it). Here is the offending code:
z_world::z_world(char* name)
{
unsigned int i, skip;
char tmp;
//Load data from file
std::string* data = loadString(name);
//Base case if there is no world data
tiles = NULL;
w = 0;
h = 0;
if(data->length() > 0) {
//Set up the 'tiles' array
for(i = 0; i < data->length(); i++) {
if(data->at(i) == '\n')
h++;
if(h == 0)
w++;
}
tiles = new int[data->length()-h];
//Load Data
skip = 0;
for(i = 0; i < data->length(); i++) {
if(data->at(i) == '\n') {
skip++;
printf("\n");
continue;
}
tmp = data->at(i);
tiles[i+skip] = atoi(&tmp);
printf("%i ",tiles[i+skip]);
}
}
delete data;
}
Here's where I load in the string:
std::string* loadString(char* name)
{
ifstream in(name);
std::string* input = new string();
while(in) {
std::string line;
getline(in,line);
input->append(line);
input->append("\n");
}
in.close();
return input;
}
I get the breakpoint and error inside of "delete data;", which makes me think that "data" gets deleted somewhere before that, but I can't find where it would. For reference, this method is to create an object that contains world data for a game in the form of a virtual 2D integer array (for the ID's of the tiles).
Youre problem is probably here:
tiles[i+skip] = atoi(&tmp);
Problem 1:
It should be -skip
tiles[i - skip] =
Problem 2:
The atoi() command is being used incorrectly (tmp does not contain a string). But also I don't think atoi() is the appropriate method. I think what you are looking for is simple assignment. The conversion from char to int is automatic:
tiles[i - skip] = tmp;
Problem 3:
You are not using objects correctly. In this situation there is no need to generate dynamic objects and create a mess with dynamic memory management. It would be simpler to just to create automatic objects and pass those back normally:
std::string* loadString(char* name)
// ^ Don't do this.
std::string loadString(std::string const& name)
// ^^^^^^^ return a string by value.
// The compiler will handle memory management very well.
In general you should not be passing pointers around. In the few situations where you do need pointers they should be held within a smart pointer object or containers (for multiple objects) so that their lifespan is correctly controlled.
atoi(&tmp);
atoi expects a pointer to a null terminated string - not a pointer to a char
There's no need to dynamically allocate the string in the code you've shown. Change the loadString function to
std::string loadString(char* name)
{
ifstream in(name);
std::string input;
// ...
return input;
}
In the caller
std::string data = loadString( name );
Now there's no need to delete the string after you're done.
Instead of
int *tiles = NULL;
tiles = new int[data->length()-h];
use
std::vector<int> tiles;
tiles.resize(data.length() - h);
Also, if you do need to dynamically allocate objects you should be using smart pointers (std::unique_ptr and std::shared_ptr) instead of raw pointers.
There is a bug in
tiles[i+skip] = atoi(&tmp);
For example, for a string
Hello\n
World\n
and for the loop iteration at the point of i == 10, skip is already 1 (since we have encountered the first \n before) and you are writing to tiles[10 + 1], but tiles only has been allocated as an array with 10 elements.
May be the variable input is local to this function. So after returning from this the memory is freed. So, calling later delete on this string tries to free already freed memory.

Converting managed Byte array to unmanaged string

I am trying to convert a managed byte array to std::string in my C++/CLI wrapper; however, I am seeing some corrupt memory in the heap later on. Just wanted to check if I am doing the conversion right. Below is my method is CLI:
string ByteArrayToStr(array<Byte>^ byteArray)
{
int size = byteArray.Length;
IntPtr pnt = Marshal::AllocHGlobal(size + 1);
char* chararray = (char*)pnt.ToPointer();
try
{
Marshal::Copy(byteArray, 0, pnt, size);
*(chararray + size) = 0;
return string(chararray);
}
finally
{
Marshal::FreeHGlobal(pnt);
}
}
Does anything seem wrong in above code?
You are doing an unnecessary explicit copy and playing with a manual memory allocation.
You could just pass the raw pinned pointer to std::string constructor:
string ByteArrayToStr(array<Byte>^ byteArray)
{
pin_ptr<unsigned char> temp = &byteArray[0];
return string(reinterpret_cast<char*>(temp), byteArray->Length);
}

glibc detected - double free or corruption

I get the following error messages when I submit the code (pasted below) to an online gcc compiler.
* glibc detected /run-1326102706-2046832693/solution: double free or corruption (!prev): 0x091901a8 ** =======
The code is as follows:
# include <iostream>
# include <string>
# include <list>
# include <cstring>
using namespace std;
int main()
{
int test_cases, i, score, str_len;
string str;
char first_char, current_char;
list <int> strlist;
list <int> :: iterator it;
cin>>test_cases;
char *cstr[test_cases]; //Creating an array of cstr pointers (test_cases number of pointers)
while(test_cases > 0)
{
cin>>str;
first_char = str.at(0);
str_len = str.length();
score = str_len;
strlist.clear();
cstr[test_cases-1] = new char[str_len];
strcpy(cstr[test_cases-1],str.c_str()); //copying the input str into cstr. This is done to minimize the complexity of std::string's at function.
for(i=1;i<str_len; i++)
{
current_char = *(cstr[test_cases-1]+i);
if (current_char == first_char)
{
score++; strlist.push_front(1);
it = strlist.begin();
if (it != strlist.end())
it++;
}
while (!strlist.empty() && it != strlist.end())
{
if (current_char == *(cstr[test_cases-1] + *(it)))
{
(*it)++;it++;score++;
}
else
it = strlist.erase(it);
}
if (!strlist.empty())
it = strlist.begin();
}
cout<<score<<endl;
delete(cstr[test_cases-1]);
test_cases--;
}
return 0;
}
As mentioned in the code itself, I initially used std::string, but found that the std::string.at function was quite slow (esepcially since this problem has really large input strings). So I decided to store the string input in a character array, so that direct indexing to a particular position would be possible.
Appreciate any help.
There are two problems that I can see:
cstr[test_cases-1] = new char[str_len]; // Not allocating space for terminating NULL.
delete(cstr[test_cases-1]); // Incorrect delete, should be delete[]
// As already pointed out by mooware
Change these two lines to:
cstr[test_cases-1] = new char[str_len + 1];
delete[] cstr[test_cases-1];
You are using array-new ("new char[str_len]") to allocate the strings, but scalar-delete ("delete(cstr[test_cases-1])") to delete them. You should always match the new- and delete-operators, so when you use array-new, also use array-delete ("delete[] cstr[test_cases-1]").
You have two bugs. One is here:
cstr[test_cases-1] = new char[str_len];
strcpy(cstr[test_cases-1],str.c_str());
You allocate one byte too few. That should be new char[str_len+1] since strcpy copies the terminator.
The other is here:
delete(cstr[test_cases-1]);
You cannot allocate with new[] and deallocate with delete. If you allocate with new[], you must deallocate with delete[].