array of char*
-
Hi there! I need an array of strings of
char *
type and I am not clear about how to free up memory after use. The code below does not work.char \* ptr\_p\_c\[3\]; ptr\_p\_c\[0\] = new char; strcpy (ptr\_p\_c\[0\], "hi there"); ptr\_p\_c\[1\] = new char; strcpy (ptr\_p\_c\[1\], "whatever"); delete ptr\_p\_c;
I know I shouldn't be using this type for strings and all this. Believe me, I've tried repeatedly to convince my customer this is not the right way to do things, but unfortunately it is not my decission... Thanks a lot.
-
Hi there! I need an array of strings of
char *
type and I am not clear about how to free up memory after use. The code below does not work.char \* ptr\_p\_c\[3\]; ptr\_p\_c\[0\] = new char; strcpy (ptr\_p\_c\[0\], "hi there"); ptr\_p\_c\[1\] = new char; strcpy (ptr\_p\_c\[1\], "whatever"); delete ptr\_p\_c;
I know I shouldn't be using this type for strings and all this. Believe me, I've tried repeatedly to convince my customer this is not the right way to do things, but unfortunately it is not my decission... Thanks a lot.
First up, the "customer" has no business wrt such low level programming details. I smell something else here. Anyway, to the point, the allocation itself is flawed. You are allocating just one character and copying several characters into it. A lot of corrupted memory. The delete will cry. The proper way to delete is to loop through the array and delete every allocated sub array.
...byte till it megahertz... my donation to web rubbish
-
First up, the "customer" has no business wrt such low level programming details. I smell something else here. Anyway, to the point, the allocation itself is flawed. You are allocating just one character and copying several characters into it. A lot of corrupted memory. The delete will cry. The proper way to delete is to loop through the array and delete every allocated sub array.
...byte till it megahertz... my donation to web rubbish
Something like this??
const int num = 3; char \* ptr\_p\_c\[num\]; for (int i = 0; i<num; i++) ptr\_p\_c\[i\] = new char\[20\]; strcpy\_s (ptr\_p\_c\[0\], 9, "hi there"); strcpy\_s (ptr\_p\_c\[1\], 7, "change"); for (int i = 0; i<num; i++) delete ptr\_p\_c\[i\];
-
Hi there! I need an array of strings of
char *
type and I am not clear about how to free up memory after use. The code below does not work.char \* ptr\_p\_c\[3\]; ptr\_p\_c\[0\] = new char; strcpy (ptr\_p\_c\[0\], "hi there"); ptr\_p\_c\[1\] = new char; strcpy (ptr\_p\_c\[1\], "whatever"); delete ptr\_p\_c;
I know I shouldn't be using this type for strings and all this. Believe me, I've tried repeatedly to convince my customer this is not the right way to do things, but unfortunately it is not my decission... Thanks a lot.
Well, you already know that avoiding
std::string
is just plain silly so I don't bother you more:char * ptr_p_c[2];
ptr_p_c[0] = new char[sizeof("hi there")];
strcpy (ptr_p_c[0], "hi there");
ptr_p_c[1] = new char[sizeof("whatever")];
strcpy (ptr_p_c[1], "whatever");
delete ptr_p_c[0];
delete ptr_p_c[1];If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Something like this??
const int num = 3; char \* ptr\_p\_c\[num\]; for (int i = 0; i<num; i++) ptr\_p\_c\[i\] = new char\[20\]; strcpy\_s (ptr\_p\_c\[0\], 9, "hi there"); strcpy\_s (ptr\_p\_c\[1\], 7, "change"); for (int i = 0; i<num; i++) delete ptr\_p\_c\[i\];
better
const int N = ??
const char* names[N] = {"...", "...", ...}
char *arr[N];
for(n=0;n
...byte till it megahertz... -
Well, you already know that avoiding
std::string
is just plain silly so I don't bother you more:char * ptr_p_c[2];
ptr_p_c[0] = new char[sizeof("hi there")];
strcpy (ptr_p_c[0], "hi there");
ptr_p_c[1] = new char[sizeof("whatever")];
strcpy (ptr_p_c[1], "whatever");
delete ptr_p_c[0];
delete ptr_p_c[1];If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi there! I need an array of strings of
char *
type and I am not clear about how to free up memory after use. The code below does not work.char \* ptr\_p\_c\[3\]; ptr\_p\_c\[0\] = new char; strcpy (ptr\_p\_c\[0\], "hi there"); ptr\_p\_c\[1\] = new char; strcpy (ptr\_p\_c\[1\], "whatever"); delete ptr\_p\_c;
I know I shouldn't be using this type for strings and all this. Believe me, I've tried repeatedly to convince my customer this is not the right way to do things, but unfortunately it is not my decission... Thanks a lot.
Does the memory for those strings necessarily need to come from the heap? If not, what about:
char *ptr_p_c[] = { "hi there", "whatever" };
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
Hi there! I need an array of strings of
char *
type and I am not clear about how to free up memory after use. The code below does not work.char \* ptr\_p\_c\[3\]; ptr\_p\_c\[0\] = new char; strcpy (ptr\_p\_c\[0\], "hi there"); ptr\_p\_c\[1\] = new char; strcpy (ptr\_p\_c\[1\], "whatever"); delete ptr\_p\_c;
I know I shouldn't be using this type for strings and all this. Believe me, I've tried repeatedly to convince my customer this is not the right way to do things, but unfortunately it is not my decission... Thanks a lot.
piul wrote:
Believe me, I've tried repeatedly to convince my customer this is not the right way to do things, but unfortunately it is not my decission...
If you are a professional, then MAKE IT your decision. if it takes less time to develop, less time to debug, make the code more secure and readable, than you need to do it the right way. anyway,you should delete each item in the ptr_p_c array. good luck.
Watched code never compiles.
-
Hi there! I need an array of strings of
char *
type and I am not clear about how to free up memory after use. The code below does not work.char \* ptr\_p\_c\[3\]; ptr\_p\_c\[0\] = new char; strcpy (ptr\_p\_c\[0\], "hi there"); ptr\_p\_c\[1\] = new char; strcpy (ptr\_p\_c\[1\], "whatever"); delete ptr\_p\_c;
I know I shouldn't be using this type for strings and all this. Believe me, I've tried repeatedly to convince my customer this is not the right way to do things, but unfortunately it is not my decission... Thanks a lot.
piul wrote:
Believe me, I've tried repeatedly to convince my customer this is not the right way to do things
Why does the customer need to know how you are implementing a small piece of code? If they understand that level of detail then why do they need you?
Just say 'NO' to evaluated arguments for diadic functions! Ash
-
Hi there! I need an array of strings of
char *
type and I am not clear about how to free up memory after use. The code below does not work.char \* ptr\_p\_c\[3\]; ptr\_p\_c\[0\] = new char; strcpy (ptr\_p\_c\[0\], "hi there"); ptr\_p\_c\[1\] = new char; strcpy (ptr\_p\_c\[1\], "whatever"); delete ptr\_p\_c;
I know I shouldn't be using this type for strings and all this. Believe me, I've tried repeatedly to convince my customer this is not the right way to do things, but unfortunately it is not my decission... Thanks a lot.
I know dumb customers. I also know dumber customers. But this is the dumbest customer.
«_Superman_» _I love work. It gives me something to do between weekends.
-
piul wrote:
Believe me, I've tried repeatedly to convince my customer this is not the right way to do things
Why does the customer need to know how you are implementing a small piece of code? If they understand that level of detail then why do they need you?
Just say 'NO' to evaluated arguments for diadic functions! Ash
Because he's afraid of using
new
for instancing strings. :rolleyes:If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I know dumb customers. I also know dumber customers. But this is the dumbest customer.
«_Superman_» _I love work. It gives me something to do between weekends.
You know dumb customers. I know dumb customers. He knows dumb customers. Hey Superman, your customers. Hey Superman, YOUR customers are just not that dumb. :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I know dumb customers. I also know dumber customers. But this is the dumbest customer.
«_Superman_» _I love work. It gives me something to do between weekends.
So dumb customers are your kryptonite, eh?
...byte till it megahertz... my donation to web rubbish
-
So dumb customers are your kryptonite, eh?
...byte till it megahertz... my donation to web rubbish
A good reason to dislike them. :)
«_Superman_» _I love work. It gives me something to do between weekends.
-
Well, you already know that avoiding
std::string
is just plain silly so I don't bother you more:char * ptr_p_c[2];
ptr_p_c[0] = new char[sizeof("hi there")];
strcpy (ptr_p_c[0], "hi there");
ptr_p_c[1] = new char[sizeof("whatever")];
strcpy (ptr_p_c[1], "whatever");
delete ptr_p_c[0];
delete ptr_p_c[1];If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]It looks like your sample is an example of why std:::string should be used: The null terminator will overflow the char[] buffers you allocated by one byte.
-
It looks like your sample is an example of why std:::string should be used: The null terminator will overflow the char[] buffers you allocated by one byte.
You're wrong. Hint: I didn't use
strlen
. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You're wrong. Hint: I didn't use
strlen
. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Oops, I stand corrected.