Deleting a character pointer getting heap error - c++

The following code is supposed to implement my own string class. Similar to if you were to create something like this String s = "Hi";. I am getting an error when it goes to destroy and gets to the part where delete[] data. Is says I am writing when I am out of the heap buffer. These are not cstrings so there is not a null character at the end of my string.
Here is my converting/default constructor:
String346::String346(const char * oldString) : data(NULL), size(static_cast<unsigned int>(strlen(oldString))){
data = new(std::nothrow) char[size];
for (unsigned int i = 0; i <= getSize(); i++){
data[i] = oldString[i];
}
}
Since these functions need to support function chaining I am going to put my both functions that relate to my problem one where if a String346 object was passed or if a char * was passed in.
Concatenating function where char * is passed in:
String346 & String346::concat(const char * catString) {
String346 newCatString(catString);
concat(newCatString);
return (*this);
}
Concatenating function where String346 object is passed in:
String346 & String346::concat(const String346 & catString) {
String346 tempData(data);
size = tempData.getSize() + catString.getSize();
destroy();
data = new (std::nothrow) char[size];
if (data == NULL){
std::cout << "Not enough space to concatinate this string." << std::endl;
}
else{
unsigned int index = 0;
for (unsigned int i = 0; i < getSize(); i++){
if (i < tempData.getSize()){
data[i] = tempData.data[i];
}
else{
data[i] = catString.data[index];
index++;
}
}
}
return (*this);
}
My destroy function which does all the work for the destruction of an object is simple. It contains these three lines:
delete[] data;
data = NULL;
size = 0;
return;

Your constructor allocates a char array containing size elements.
Then, your constructor appears to copy size+1 characters to the array (I am assuming that getSize() returns size).
Therefore, the constructor code runs off the end of the array, and corrupts one byte past the end of the allocated array.
P.S. The static_cast is not needed, and only makes the code more obfuscated.

The first line in your concat method:
String346 tempData(data);
passes a char * to your constructor which is not null terminated, so the call to strlen will go past the end of the string.
The next two lines also don't work:
size = tempData.getSize() + catString.getSize();
destroy();
destroy sets size back to zero, which means the rest of your method will not do anything.
You should try running this through a debugger and single step through it - you can then check the values of your variables at each step and make sure your program is doing what you expect.
Also, if you have a member variable that is freed in the destructor, you should look into the "rule of three" or "rule of five", to make sure things don't get freed twice.

Related

Custom string implementation, operator + overload memory allocation issue

I am trying to create custom string implementation in c++ and I have a problem with overloading operator "+". I want to append new string to existing one and when I try to allocate a new extended string, debugger allocates around 12 bytes more then it should. I have no control over allocation, he just ignores variable "length". Here is the code:
class String
{
private:
char *ptrNiz;
public:
String(const char *niz = "")
{
ptrNiz = new char[strlen(niz)+1];
strcpy_s(ptrNiz, strlen(niz)+1, niz);
cout << "stvoren niz: " << ptrNiz << endl;
}
String(const String& refString)
{
ptrNiz = new char[strlen(refString.ptrNiz) + 1];
strcpy_s(ptrNiz, strlen(refString.ptrNiz) + 1, refString.ptrNiz);
cout << "Kopiran niz: " << ptrNiz << endl;
}
~String()
{
cout << "Unisten objekt: " << ptrNiz << endl;
delete[] ptrNiz;
}
int Length() const
{
return strlen(ptrNiz);
}
int CharAt(int i) const
{
return ptrNiz[i];
}
void Ispisi()
{
cout << ptrNiz << endl;
}
operator char*()
{
return ptrNiz;
}
String operator+=(const const String& ref)
{
const int const length = ref.Length() + this->Length() + 1;
char *temp = new char[length]; // ignores length and allocates more space
for (int i = 0; i < this->Length(); i++)
{
temp[i] = ptrNiz[i];
}
for (int j = 0; j < ref.Length(); j++)
{
temp[this->Length() + j] = ref.ptrNiz[j];
}
return String(temp);
}
};
char *temp = new char[length]; // ignores length and allocates more space
All which your C++ implementation is required to do here is returning a pointer to a memory location with room for length bytes. Nothing more, nothing less. It is allowed to internally allocate more bytes, even though your code must not try to access them (or else the behaviour is undefined).
The low-level details of dynamic memory allocation is an implementation issue, not your code's concern.
A debug build is a very good example for a situation in which a C++ implementation may use extra bytes for debugging information. In fact, that's probably what is happening here. I suppose you are using Visual C++. As its documentation (CRT Debug Heap Details) says:
When you request a memory block, the debug heap manager allocates from
the base heap a slightly larger block of memory than requested and
returns a pointer to your portion of that block.
[...]
The additional memory allocated by the debug heap routines is used for
bookkeeping information, for pointers that link debug memory blocks
together, and for small buffers on either side of your data to catch
overwrites of the allocated region.
Note that there are very many other grave errors in your code. I decided to ignore them for now and just answered the exact question you asked.
You are allocating twice, once in the constructor and again in the operator+= method. You also aren't deleting the pointer in the operator function so that memory is leaking. Either use smart pointers or provide an additional constructor parameter which specifies the ownership of the incoming pointer.
In your code please add the following line in to terminate the string with NULL;
String operator+=(const const String& ref)
{
// You code goes here
temp[length - 1] = '\0';
return String(temp);
}
This will work as expected.
Hope this helps.

C++ Where is the seg fault?

I am currently tackling this assignment for my computer science class:
Make your own dynamic array template. It should allow creating contiguous arrays (filled with things of the same type) which you can extend without worrying about running out of space.
Do one version using malloc and free.
Do one version using new and delete.
My version using new and delete works flawlessly; however, in trying to convert my new/delete code to using malloc/free, I keep getting a seg fault. I have narrowed down the segfault (I think), to being in a single function: addData. Take a look at the code in my main I used to test this:
Array2<int> *testArray3 = new Array2<int>(5);
Array2<int> *testArray4;
testArray3->initArray();
testArray3->printArray();
testArray4 = testArray3->addData(7);
testArray4->printArray();
return 0;
This gives a seg fault; however, when I change it to this:
Array2<int> *testArray3 = new Array2<int>(5);
Array2<int> *testArray4;
testArray3->initArray();
testArray3->printArray();
testArray4 = testArray3; //->addData(7);
testArray4->printArray();
return 0;
There is no seg fault. This makes me believe the issue is in my addData function. Here is the code for that:
Array2<T> *addData(T dataToAdd){
Array2 <T> *tmp;
tmp->data = this->getData();
Array2 <T> *newData;
newData->data = (T *) malloc(sizeof(T)*(this->size + 1));
for (int i = 0; i < tmp->getSize() + 1; ++i){
if (i < tmp->getSize()){
//newData->data[i] = tmp->data[i];
newData->setData(tmp->getData()[i], i);
}
else{
//newData->data[i] = dataToAdd;
newData->setData(dataToAdd, i);
}
}
free(tmp->data);
free(this->data);
return newData;
};
I am new to programming as a whole and have not completely wrapped my head around pointers and memory allocation, etc. Any advice you could give me would be greatly appreciated! In case you need to see the rest of the code, here is the entire file I coded my template in. Thank you so much for your time!
#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;
template<typename T>
class Array2{
public:
Array2(int size){
this->size = size;
data = (T *) malloc(sizeof(T)*size);
};
Array2<T> *addData(T dataToAdd){
Array2 <T> *tmp;
tmp->data = this->getData();
Array2 <T> *newData;
newData->data = (T *) malloc(sizeof(T)*(this->size + 1));
for (int i = 0; i < tmp->getSize() + 1; ++i){
if (i < tmp->getSize()){
//newData->data[i] = tmp->data[i];
newData->setData(tmp->getData()[i], i);
}
else{
//newData->data[i] = dataToAdd;
newData->setData(dataToAdd, i);
}
}
free(tmp->data);
free(this->data);
return newData;
};
~Array2(){
free(this->data);
};
void initArray(){
for (int i = 0; i < this->size; ++i){
//this->data[i] = i;
this->setData(i, i);
}
};
void printArray(){
//ostringstream oss;
string answer = "";
for (int i = 0; i < this->size; ++i){
//oss << this->data[i] + " ";
cout << this->data[i] << " ";
}
//answer = oss.str();
cout << answer << endl;
};
T* getData(){
return this->data;
}
int getSize(){
return this->size;
}
void setData(T data, int index){
this->getData()[index] = data;
}
private:
int size;
T* data;
};
Array2 <T> *tmp;
Allocates a pointer. This does not point the pointer at anything or allocate any storage for the pointer to point at. What it points at without being explicitly assigned is undefined. If you are lucky, and you are this time, tmp points at an invalid location and the program crashes. If you are unlucky, tmp points at some usable region of program memory and lets you write over it, destroying whatever information was there.
tmp->data = this->getData();
Attempts to access the data member at tmp, but fortunately for you the access is in invalid memory and the program comes to a halt. It also has tmp's data pointing at this's data, and that's a dangerous position to be in. Changes to one will happen to the other because they both use the same storage. Also think about what will happen to this->data if you free tmp->data.
Or perhaps I'm wrong and the halt is here for the same reason:
Array2 <T> *newData;
newData->data = (T *) malloc(sizeof(T)*(this->size + 1));
Both need to be fixed. tmp doesn't have to live long, so we can make it a temporary local variable.
Array2 <T> tmp;
Typically this will be created on the stack and destroyed when the function ends and tmp goes out of scope.
But this will not work because Array2's constructor requires a size so it can allocate the array's storage. You need to find out how big to make it. Probably something along the lines of:
Array2 <T> tmp(this->size + 1);
But frankly I don't think you need tmp at all. You should be able to copy the dataToAdd directly into newData without using tmp as an intermediary.
newData is eventually going to be returned to the caller, so it needs a longer scope. Time to use new.
Array2 <T> *newData = new Array2 <T>(this->size + 1);
And through the magic of the constructor... Wait a sec. Can't use new. That makes this hard. malloc doesn't call constructors, so while malloc will allocate resources for newData, it doesn't do the grunt work to set newData up properly. Rule of thumb is Never malloc An Object. There will be exceptions I'm sure, but you shouldn't be asked for this. I recommend using new here and politely telling the instructor they are on crack if they complain.
Anyway, new Array2 <T>(this->size + 1) will allocate the data storage for you with it's constructor.
There is an easier way to do this next bit
for (int i = 0; i < tmp->getSize() + 1; ++i){
if (i < tmp->getSize()){
//newData->data[i] = tmp->data[i];
newData->setData(tmp->getData()[i], i);
}
else{
//newData->data[i] = dataToAdd;
newData->setData(dataToAdd, i);
}
}
Try:
for (int i = 0; i < tmp->size; ++i){
newData->data[i] = tmp->data[i]; // you were right here
}
newData->data[tmp->size] = dataToAdd;
And back to something I hinted at earlier:
free(tmp->data);
free(this->data);
Both tmp->data and this->data point to the same memory. To be honest I'm not sure what happens if you free the same memory twice, but I doubt it's good. Regardless, I don't think you want to free it. That would leave this in a broken state.
Recap and fixes
Array2<T> *addData(T dataToAdd)
{
Array2 <T> *newData = new Array2 <T>(this->size + 1);
for (int i = 0; i < this->size; ++i)
{
newData->data[i] = this->data[i];
}
newData->data[this->size] = dataToAdd;
return newData;
};
This version leaves this intact and returns a newData that is one bigger than this. What it doesn't do is add anything to this. Which is goofy for a method named addData.
It also leads to stuff like this:
mydata = myData->addData(data);
which leaks memory. The original mydata is lost without deletion, resulting in a memory leak.
What I think you really need is a lot simpler:
Array2<T> & addData(T dataToAdd)
{
this->data = realloc(this->data, this->size + 1);
this->data[this->size] = dataToAdd;
this->size++;
return *this;
};
realloc effectively allocates a new buffer, copies the old buffer into the new one, and frees the old buffer all in one fell swoop. Groovy.
We then add the new element and increment the count of elements stored.
Finally we return a reference to the object so it can be used in a chain.
Usage can be
myData.addData(data);
myData.addData(data).addData(moredata);
myData.addData(data).printArray();
and if you have operator << support written
std::cout << myData.addData(data) << std::endl;
I'd go back over the new version of Array if I were you. Most of the bugs picked off here are conceptual errors and also apply to it. You might just be getting unlucky and it merely looks like it works. I just read C++ Calling Template Function Error. The posted solutions fixed the immediate problem, but did not touch the underlying memory management problems.
As for the rest of your class, I advice following the link and answering What is The Rule of Three? Because Array2 violates the heck out of it.

Converting integers into a char* c++

Here is the function I have, "Sprite" is an object in the program, and "GetSpriteAtPosition" just returns a pointer to the correct sprite at the coordinates.
My problem is that I store a letter in each sprite, in the form of an integer. 0 is a, and 25 is z, with everything in between respectively. I need my function to return a char* that gives me the letters of a row of sprites, so if in the program the sprites spell out "abcdefgh", then that's what I need this function to print out. There's an 8x8 grid of sprites, and I'm getting the coordinates correctly, but I get an error that I can't convert an int to a char* in the marked line. What can I do to get this to work?
Thanks in advance!
char* RowLetters(int row)
{
char* pointer;
for( int i = 0; i < 8; i++)
{
Sprite* selectedSprite = SpriteAtPosition(row*50, i * 50);
if(selectedSprite != NULL)
{
char* temp = (char)(selectedSprite->Frame() + 97); //error here
pointer = strcat(pointer, temp);
}
else
{
pointer = strcat(pointer, "test");
}
}
return pointer;
}
Try this:
char temp = (char)(selectedSprite->Frame() + 97);
pointer = strcat(pointer, &temp);
I've changed the variable into a standard char rather than a pointer and then passed a reference to strcat() with the & operator.
EDIT:
As pointed out in the comments, this doesn't work because &temp isn't NULL terminated. I used to get around this when I programmed more C by doing the following.
char temp[2];
temp[0] = (char)(selectedSprite->Frame() + 97);
temp[1] = '\0';
pointer = strcat(pointer, temp);
Of course, the temp array could be declared outside the for() loop for a little better performance (in theory).
None of this addresses the other problems with the code like pointer never being declared. I think a broader understanding of the calling function would be in order to determine whether pointer should be allocated within this function or passed in by the caller.
Your code as written, will have undefined behavior because pointer is not initialized, and does not point to any valid memory that you have allocated (to hold the appropriate length of letters in the row.
If this truly is C++, as you state, then you don't want to return a char* from this function, as that implies that you have a static string already allocated within that function (yuck), or you will be dynamically allocating the string in that function and the caller must free it (yuck).
Neither of these options is ideal.
I'd suggest a very simple change to return a std::string, like this:
std::string RowLetters(int row)
{
std::string pointer;
for( int i = 0; i < 8; i++)
{
Sprite* selectedSprite = SpriteAtPosition(row*50, i * 50);
if(selectedSprite != NULL)
{
pointer.push_back((char)(selectedSprite->Frame() + 97));
}
else
{
// ???
// pointer = strcat(pointer, "test");
}
}
return pointer;
}

I think my destructor is causing an error to occur. Can you help me figure out why?

The problem is in my assignment operator, I've forgotten to deallocate and reallocate memory for my pointer in my string class. I must have accidentally treated it like a copy constructor; This has been a great lesson in why memory management is very important. Thanks everyone for your help.
ive implemented my own string class and this seems to be the last function on the call stack before it breaks.
String::~String(){
delete [] rep;
len=0;
}
Can someone help me understand what the problem is?
Here is the function which calls it
template <class T>
void SList<T>::RemoveAfter(typename SList<T>::Iterator i){
assert(i.nodePointer !=0 && i.nodePointer->next!=0);
Node *save = i.nodePointer -> next;
i.nodePointer->next = i.nodePointer->next->next;
delete save;
}
If there is any more info you need to help me figure out why this occurs let me know.
By the way, if I use a type int, I dont have this problem, so I know the issue has to be with my string class...right?
More info as requested:
struct Node{ // Node: Stores the next Node and the data.
T data;
Node *next;
Node() {next =0;}
Node(const T& a, Node *p = 0){data=a;next=p;}
};
the error:
Windows has triggered a breakpoint in Algorithms.exe.
This may be due to a corruption of the heap, which indicates a bug in Algorithms.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while Algorithms.exe has focus.
The output window may have more diagnostic information.
Example functionality that breaks:
String item1("Example"), item2("Example");
SList<String> list1;
list1.AddFirst(item2);
list1.AddFirst(item1);
list1.AddLast("List Class");
list1.AddLast("Functionality");
SList<String>::Iterator i1;
i1 = list1.Begin();
i1++;
i1++;
list1.RemoveAfter(i1);
Example that works
SList<int> list1;
list1.AddFirst(1);
list1.AddFirst(2);
list1.AddLast(3);
list1.AddLast(4);
SList<int>::Iterator i1;
i1 = list1.Begin();
i1++;
i1++;
list1.RemoveAfter(i1);
system("pause");
More information:
//Default Constructor
String::String(){
rep = new char[1];
rep[0] = '\0';
len = 0;
}
//Constructor - Converts char* to String object
String::String(const char *s){
len=0;
const char *temp = s;
while(*temp){
++len;
++temp;
}//Sets len of rep to the length of s
rep = new char[len + 1];
for(int i=0; i<=len; ++i)
rep[i]=s[i];
}
//Copy Constructor
String::String(const String &obj){
len=0;
char *temp = obj.rep;
while (*temp){
++len;
++temp;
}//Sets len of rep to length of obj.rep
rep = new char[len + 1];
for (int i = 0; i<=len; ++i)
rep[i] = obj.rep[i];
}
//Assignment operator
const String& String::operator=(const String &rhs){
if (this != &rhs){
len=0;
char *temp = rhs.rep;
while(*temp){
++len;
++temp;
}//Sets len of this to length of rhs.rep
for(int i = 0; i<=len;++i)
rep[i]=rhs.rep[i];
}
return *this;
}
In your assignment operator you are potentially writing outside the bounds of the allocated area pointed to by char* rep, which will probably corrupt the stack (ie. cause undefined-behavior).
When you later try to deallocate this segment of memory a corrupt stack might cause things to go banans, and it's a very plausible this is the reason for the error message you are receiving when running your application.
As stated by the message itself "this may be due to a corruption of the heap".
How do I fix my problem?
Inside your assignment operator you will need to do three things:
deallocate the previous allocated memory in this (unless this->len and rhs.len have the same value, if so; skip step 2. as well)
allocate the same amount of memory as rhs has for rhs.rep (ie. rhs.len bytes)
update this->len to represent the length of the new content
copy the bytes stored in the memory pointed to by rhs.rep to the memory segment pointed to by this->rep.
Currently you are only doing step 3 & 4.

Returning array of dynamically allocated wchar_t* from a function

I have a function whose signature is as follows:
GetCustomers( wchar_t** Name,int *count);
In main method: Call to customer looks like this:
GetCustomers( Name,&count);
The body of the function is as follows: (since count of customers is unknown , I am trying to allocate meomry dynamically)
GetCustomers( wchar_t** Name,int *count)
{
//Logic to get customer count : Stored in int myCustomersCount
Names = new wchar_t*[myCustomersCount];
for (int i=0; i < myCustomersCount; i++ )
{
Names[i] = new wchar_t;
}
//Logic to get customer names in wchar_t* strName = "Name1";
Names[0] = strName;
*count = myCustomersCount;
}
I would think that this implementation would allow array Name to be passed back correctly to the Main() function with memory allocation on heap but it seems not to work. What is wrong here? myCustomersCount seems to be correct in caller.
PS: The code compile and executes but array received in Main is garbage.
You seem to be thinking in terms of C, not really C++. I'd use something like:
std::vector<std::string> GetCustomers();
or (probably preferred):
template <class outIt>
void GetCustomers(outIt output_iterator);
The latter you'd use something like:
std::vector<std::wstring> customers;
GetCustomers(std::back_inserter(customers));
The third obvious possibility would be to just equip your customers class with a begin() and end() member functions that return iterators to the customers data.
Edit2: Here's some tested demo code:
#include <stdio.h>
#include <string.h>
#include <wchar.h>
void GetCustomers(wchar_t ***names, int *count) {
static wchar_t *myCustomers[] = {
L"You",
L"Him",
L"Her"
};
int myCustomersCount = 3;
wchar_t **temp = new wchar_t *[myCustomersCount];
*count = myCustomersCount;
for (int i=0; i<myCustomersCount; i++) {
temp[i] = new wchar_t[wcslen(myCustomers[i])+1];
wcscpy(temp[i], myCustomers[i]);
}
*names = temp;
}
int main() {
wchar_t **customers;
int count;
GetCustomers(&customers, &count);
for (int i=0; i<count; i++)
printf("%S\n", customers[i]);
return 0;
}
I'm really not sure what you're trying to do here; from what I understand about your code; you're trying to store some strings into an array of an array of character pointers.
GetCustomers(wchar_t **Name, int *count) {
Name = new wchar_t*[myCustomersCount];
for(int i = 0; i < myCustomersCount; i++) {
/* Get your customer name and store into strName */
Name[i] = strName;
}
*count = myCustomersCount;
}
In main, presumably you've got something like this
wchar_t *Name = NULL;
and then you say
GetCustomers( Name,&count);
This passes Name by value, but you want to pass it by reference:
GetCustomers( &Name,&count);
And presumably it's just a typo, but your parameter name is Name (singular) but you refer to it as Names (plural) in the function:
GetCustomers( wchar_t** Name,int *count)
{
//Logic to get customer count : Stored in int myCustomersCount
Names = new wchar_t*[myCustomersCount];
In any case, you want to assign to where Name is pointing, not to itself:
*Names = new wchar_t*[myCustomersCount];
Then for each element in Names you allocate one character, but then overwrite the first one with strName. The allocation is unnecessary (and in fact is a memory leak), and you should assign from strName to each element within the loop, as Suroot's answer does.
2 definite issue and 1 potential issue with your code. The main issue causing your problem first: Name itself is passed by value. That means when you you assign to it in the very first line of your function when you new the memory, you're assigning to the copy, not the original! You have three options: 1) keep the double pointer, make the caller responsible for allocating the memory, and add a third parameter for number of names that the array can hold (recommended) or 2) make Name a triple pointer (wchar_t*** Name) and then you can assign to it by dereferencing it: *Name = new wchar_t*[myCustomersCount]; or 3) just return the wchar_t** since you don't use the passed value for anything.
Then another definite issue: when you allocate memory for each name, you need to use the new[] operator there as well because otherwise you're only allocating room for a single wchar_t.
Finally, the potential issue. You don't show how exactly this code is getting each customer name. But if strName points to memory that is getting reused for each customer name as you put them all in your array, you're going to have to wstrcpy each name into the array. If it doesn't, then you don't need to allocate memory for each Names[i] as you can just store the result right into Names[i].
A final note: just from looking at this code it seems like you're going to have lots of problems with memory management as it seems very unclear who is responsible for allocating and deallocating memory which is likely going to lead to memory leaks. Try your best to keep the responsibility for allocating and deallocating the memory in the same location and you'll reduce lots of potential headaches -- have callers allocate the memory before they call the function and have the caller deallocate the memory when they're done with it.
/* changed */
wchar_t** GetCustomers( int *count)
{
//Logic to get customer count : Stored in int myCustomersCount
wchar_t **Names = new wchar_t*[myCustomersCount];
for (int i=0; i < myCustomersCount; i++ )
{
/* changed */
Names[i] = new wchar_t[MAX_NAME_SIZE];
}
//Logic to get customer names in wchar_t* strName = "Name1";
Names[0] = strName; /* possible wstrcpy needed here? */
*count = myCustomersCount;
/* changed */
return Names;
}
Edit
If you really absolutely can't change the function signature, the only solution I can think of is to flatten your array and use C memory functions (you could also just use a long series of news and deletes, but why not use realloc when this is what it's made for and you're managing memory without using other C++ features like the STL anyways?):
GetCustomers( wchar_t **Names, int *count)
{
//Logic to get customer count : Stored in int myCustomersCount
size_t names_size = 0;
for (int i=0; i < myCustomersCount; i++ )
{
strName = ???; // whatever you use to get the next name
size_t old_names_size = names_size;
names_size += (wstrlen(strName) + 1) * sizeof(wchar_t); //+1 for NULL
*Names = realloc(*Names, names_size);
if (!*Names) {
// Memory allocation failed, log it, abort, do whatever
}
wstrcpy(Names[old_names_size], strName);
}
*count = myCustomersCount;
}
Note that this assumes that Name has already been initialized and points to memory where you can store a wchar_t*, just like the original version assumed count has been initialized and points to memory where you can store an int.
I thought I'd make a fresh start in a new answer.
Here's a simple program that does what I think you're trying to do, with the constraint that the signature of GetCustomers must not be altered.
void GetCustomers(wchar_t** Names,int *count)
{
// Allocate the array of names
wchar_t **ar = new wchar_t*[3];
// Allocate space for each name in the array
ar[0] = new wchar_t[10];
ar[1] = new wchar_t[10];
ar[2] = new wchar_t[10];
// Fill in the names
wcscpy(ar[0],L"joe");
wcscpy(ar[1],L"jim");
wcscpy(ar[2],L"bob");
// Return the array through the bad GetCustomers signature
*Names = (wchar_t*)ar;
*count = 3;
}
int wmain(int argc, wchar_t* argv[])
{
// names is an array of strings
wchar_t **names = NULL;
int count;
// Squeeze names into the dodgy GetCustomers signature
GetCustomers((wchar_t**)&names,&count);
// Delete each name
for(size_t x = 0; x < count; ++x)
delete[] names[x];
// Delete the array
delete[] names;
return 0;
}
Note that I've matched the cast inside the function with another one in main. This way we keep everything as it should be, except for that pesky GetCustomers signature.
Does this help?