int nSize;
QString str;
char *p = new char[nSize];
p = str.toLocal8bit.data();
delete[] p;
I got a debug error when I tried to delete a char array in Xcode 5.0.2 but this char array is successfully deleted in Xcode 3.0.2.
If it's deleted successfully anywhere, that's a bug.
char* p = new char[nSize];
So far so good. p is a char array.
p = str.toLocal8bit.data();
p now points to (presumably) some underlying data from str.toLocal8bit. You now have no way to reference the memory you just allocated.
delete[] p;
You've now deleted some other object's memory.
The line
p = str.toLocal8bit.data();
overwrites the pointer that you got from new. When you delete p you're deleting memory you don't control.
If you actually need a copy of the string data, you can use memcpy or strcpy to copy from the string to your array (instead of assigning).
If you don't actually need a copy, you could do:
const char* p = str.toLocal8bit.data();
Related
I have this simple example which fails in "delete poinerToBufferPointer":
char* buffer = new char[8];
memset(buffer, 1, 8);
char** poinerToBufferPointer = &buffer;
delete poinerToBufferPointer;
delete[] buffer;
But if I comment the "delete poinerToBufferPointer"
char* buffer = new char[8];
memset(buffer, 1, 8);
char** poinerToBufferPointer = &buffer;
//delete poinerToBufferPointer;
delete[] buffer;
it's working but the question is who will delete the double pointer?
Also very strange behavior is when I do delete on the pointer only it fail on delete[] buffer.
char* buffer = new char[8];
memset(buffer, 1, 8);
char** poinerToBufferPointer = &buffer;
delete *poinerToBufferPointer; // <--- only delete the pointer that points
delete[] buffer;
What is going on in memory, and what is the right way to delete both pointers?
You do not delete pointers, you delete the thing a pointer points to.
You only need to delete things you allocate with new
delete[] buffer; deletes the array pointed to by the pointer buffer. Since you allocated that with new[] that is correct.
delete pointerToBufferPointer; attempts to delete the pointer pointed to by pointerToBufferPointer. You did not allocate that pointer using new, however, so that is incorrect. A program that attempts to do that has undefined behavior.
If you had allocated the pointer pointed to by pointerToBufferPointer using new then you would need to delete it. For example, the following is correct (though you should almost never need to write code like this):
char** pointerToBufferPointer = new char*;
*pointerToBufferPointer = new char[8];
// ...
delete[] *pointerToBufferPointer;
delete pointerToBufferPOinter;
In most real code you should avoid using new and delete directly at all. For most common use cases, containers like std::string and std::vector that manage their own memory allocation for you are perfectly sufficient and much less error-prone.
I have seen many pages for this error, but I'm not able to grasp what I'm doing wrong with my code, so I was hoping if I posted it, someone could shed some light.
This is a c++ class project. The goal is to write a function that takes two C strings (char*) and returns them concatenated together in a new. What I have here compiles, except for when I try to delete copycat, I get the malloc error in the title of this question.
How can I delete copycat?
I think I should be also deleting the "unused" news I created in the cat2 function (p, q), but that gives me the same error. What am I missing here with deallocating memory?
char* cat2(char* dest1, char* str2)
{
char* p = new char[100];
char* q = new char[100];
char* rvalue = new char[100];
for (p = dest1; *p != 0; p++)
{
;
}
for (q = str2; *q != 0; p++, q++ )
{
*p = *q;
}
*p = 0; /* set the last character to 0 */
rvalue = dest1;
return rvalue;
}
void main()
{
char s1[] = "Hello";
char s2[] = ", World!";
char* copycat = cat2(s1, s2);
cout << copycat;
delete copycat;
}
rvalue = dest1; makes rvalue point to what dest1 is pointing to. This is the local buffer s1 inside main.
So calling delete copycat; is the same as trying delete s1; which fails because it was not allocated via new. (and because it should be delete[]).
You make the same mistake with p = dest1; and q = str2;. These make p and q point to those buffers. You leaked all the memory you allocated via new, and caused a buffer overflow by writing characters past the end of s1.
I guess you intended to copy characters into the buffers you allocated via new. However, p and q buffers are useless anyway; you should be copying characters into the buffer pointed to by rvalue. To do that you will have to work with *rvalue and so on (which means: the location being pointed to by rvalue, instead of making rvalue point somewhere totally different and leaking memory).
I am trying to make an example for char pointers and use of delete operator. Code is very simple:
char name[] = "subject";
char *nameptr = name;
cout <<"&nameptr: " <<&nameptr<< endl;
delete [] nameptr;
and I keep getting this error:
*** glibc detected *** free(): invalid pointer: 0xbf9e4194 ***
and I know nameptr points out to location 0xbf9e4184, from the output.
There is no pointer points out to that location (0xbf9e4194).
I believe it's something to do with my use of delete but I couldn't figure it out.
You should only call delete or delete [] on memory allocated with new or new [], respectively. There's no need to free string literals like "subject".
Examine your code statement by statement:
Here you have an array of characters, containing "subject":
char name[] = "subject";
Here you define a pointer, pointing to the aforementioned array:
char *nameptr = name;
Here you delete[] something that you did not allocate using new[] (in fact, name was not allocated using new[], you didn't write: char * name = new char[...]):
delete [] nameptr;
So, an error is (correctly) detected, because you tried to free something that was not allocated on the heap using new (or malloc).
So, I've made a function that looks like this:
const char **myFunction(char *string)
{
char *bufCopy = new char[strlen(string)];
strcpy(bufCopy,string);
char *tmp = func1(bufCopy);
const char **RetVector = new const char* [6];
RetVector[0] = tmp;
return RetVector;
}
func1 (that is strtok) modified the first argument, so when I delete[] it gives me heap corruption. What could I do?
You must always keep track of the original allocation of your bufCopy, only that one can be deleted. tmp probably only points to part of it (inside the buffer somewhere), so you can't delete it.
I have the following pointer:
jfloat *verticesLocal;
And I want make a new copy to:
jfloat *vertices;
I want to copy the values from verticesLocal to vertices.
How can I do that? I've just tried the following command, but it doesn't work:
memcpy(vertices, verticesLocal, numVerticesLocal * sizeof(jfloat));
I can't see the error because I'm working with Android native code. Sorry.
The idea of "copying a pointer", when taken literally, is nothing more than a simple assignment.
int x = 5;
int* p1 = &x;
int* p2 = p1; // there, we copied the pointer.
In this case, both p1 and p2 point to the same data - the int variable x. However, because this is so trivial, I'm inclined to think that what you really are asking about is how to copy the data that the pointer points to.
In this case, it depends on the data type. If the pointer points to a simple buffer of PODs, this involves allocating a separate buffer, and then using something like memcpy (or preferably std::copy) to copy the data. For example:
int* p1 = new int[100];
// ... fill p1 with values
int* p2 = new int[100]; // create a new buffer
std::copy(p1, p1 + 100, p2); // copy the data into p2
Or, you can use memcpy to copy the buffer byte-by-byte, since the buffer contains PODs.
memcpy(p2, p1, 100 * sizeof(int));
However, if the pointed-to data is not a simple buffer, but rather a C++ object, you can't use memcpy. You need to perform a deep copy of the object, (usually using the object's copy constructor) to get a clone of the object. How this is done, or whether it's even possible, depends on the object. (Some objects are noncopyable.)
I have no clue what a jfloat is, but if the object is, for example, an std::string, you would just do something like:
std::string* s1; // assume this points to a valid string
std::string* s2 = new std::string();
*s2 = *s1; // copies s1 using s2's assignment operator
In this contrived example it would be preferable to avoid heap-allocation altogether, and just use stack variables. But it demonstrates the idea of copying a heap-allocated object.
malloc first, then do your memcpy.
If you are copying the pointer, it is a simple assignment:
jfloat* verticesLocal; // assuming is is allocated already
jfloat* newVertices = verticesLocal;
IF you mean you want to copy the data the point points to, you have to allocate the memory for the new block of memory first:
// assume jfloat* verticesLocal is pointing to a valid memory block of size i
jfloat* newVertices = new jfloat[i];
memcpy(newVertices, verticesLocal, i * sizeof(jfloat));
this is how you copy an array buffer :
unsigned char *pBufferSrc = new unsigned char[10];
unsigned char *pBufCpd = new unsigned char[10];
for (int i = 0; i < 10; i++)
pBufferSrc[i] = i; // pBufferSrc = [0,1,2,3,4,5,6,7,8,9]
memcpy(pBufCpd, pBufferSrc, 10); // data from pBufferSrc is copied to pBufCpd (0,1,2,..9)
delete []pBufferSrc; // if even pBufferSrc buffer gets deleted, pBufCpd still has the data