c++ char pointer array
In this post, I am going to talk about char pointer array.Some people might say that they prefer use string instead of char pointer array.
I was like that.
If you string object
You do not have to worry about the memory allocation and deletion.
When you want to concatinate two strings, you just need + operator.
If you are dealing with small amount of strings, go ahead of string.
But if you are dealing with large amout of string( I am talking about severy giga byte of string file), you btter use char pointer array.
It is faster.
It is based on my experience, believe me hahaha
Whenever you are using char pointer array, you have to make sure that you allocate enough space for each pointer. Technically, you can use char array pointer with out memory allocation
function a()
{
char * arr[5];
arr[0] = "test";
arr[1] = "test2";
arr[2] = "test3";
arr[3] = "test4";
arr[4] = "test5";
}
The above can be represent the following image
The problem is that once the array leave the function a, those value "test","test2".."test4" will be gone. In addtion, I cannot use string funtion such as strlen, substr etc....
In order to keep the values, you have to put those values in to special memory.
It is called heap .
If values are stored heap, they will stay until you delete.
If you do not use them, you have to delete.(If you don't, it is memory leak)
allocatiing memory
char * a [3];
a[0] = new char[5];
a[1] = new char[5]; --> each array can contain 5 character of array
a[2] = new char[5];
delete
delete [] a[0];
delete [] a[1]; --------> make sure you add []
delete [] a[2];
It would be like following
* the first image is 1d char * array
* the second image is 2d char * array
----------------------------------------------------------------------------------------------------------------------------
The following is practice code. If you have suggestion or correction, please feel free to leave comment.
#include <iostream>
#include <functional> // i am using mac so i need this to use namespace std
#include <string.h>
using namespace std;
void oneDfunction(char* arr[], int len);
void printOneDfunction(char* arr[], int len);
void deleteArray(char* arr[], int len);
// it is quite similar to first dimentional array
// one thing that you have to keep in mind is that you can only ...
// leave blank the first component of array. you have to specify ....
// the length
void twoDfunction(char* arr[][3], int row, int col);
void printTwoDfunction(char* arr[][3], int row, int col);
void deleteTwoDArray(char* arr[][3], int row, int col);
int main()
{
// one dimentional example
cout << "one dimetional char pointer array" << endl;
char * array[5];
int length = 5;
oneDfunction(array, length);
printOneDfunction(array, length);
deleteArray(array, length);
cout << endl;
// two dimentional example
cout << "two dimentional char pointer array << endl" << endl;
char * array2[3][3];
int row = 3;
int col = 3;
twoDfunction(array2, row, col);
printTwoDfunction(array2, row, col);
deleteTwoDArray(array2, row, col);
}
void oneDfunction(char* arr[], int len)
{
for (int i=0; i< len; i++){
// you have to allocate memory for your pointer
// otherwise your pointer do not point any
arr[i] = new char[10];
strcpy(arr[i], "input");
char * tmpP = new char[1];
*tmpP = (i+'0');
strcat(arr[i],tmpP);
delete (tmpP);
}
}
void printOneDfunction(char* arr[], int len)
{
for (int i = 0; i < len; ++i)
{
cout << arr[i] << endl;
}
}
void deleteArray(char* arr[], int len)
{
for (int i =0; i < len; i++){
// you have to put [], otherwise, you are not deleting your array entirely
delete [] arr[i];
}
}
void twoDfunction(char* arr[][3], int row, int col)
{
int num = 0;
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
arr[i][j] = new char [10];
strcpy(arr[i][j], "input");
char * tmpP = new char[1];
*tmpP = (num+'0');
num++;
strcat(arr[i][j],tmpP);
delete (tmpP);
}
}
}
void printTwoDfunction(char* arr[][3], int row, int col)
{
for (int i = 0; i < row; ++i)
{
cout << "row " << i << endl;
for (int j = 0; j < col; ++j)
{
cout << arr[i][j] << endl;
}
cout << endl;
}
}
void deleteTwoDArray(char* arr[][3], int row, int col)
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
delete [] arr[i][j];
}
}
}
No comments:
Post a Comment