470,833 Members | 1,352 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

char table / pointer memory allocation

Hi,

Why this program work?

#include<iostream.h>
class test
{
public:
char *ptr;
void setPtr(char* p){
ptr = p;
}
void print(){
cout << ptr << endl;
}
};

main()
{
test t;
t.setPtr("abc");
t.print();
system("pause");
}

Is argument of function setPtr - "abc" - a temporary char table, made
on stack, which is deleted when setPtr function ends? If it is, the
pointer - ptr - point then to the memory addres which was deleted, so
print method should print random characters or may crash the program.
When I made char table using new:

main()
{
char* tab = new char[4];
strcpy(tab, "abc");
test t;
t.setPtr(tab);
delete[] tab;
t.print();
system("pause");
}

print() method prints random characters.
But why the first program doesn't crash / prints random chars ?

thanks in advance
best regards
mehafi
Jun 27 '08 #1
5 1886

<me****@gmail.comwrote in message Hi,
>
Why this program work?

#include<iostream.h>
Non standard header. Get any recent book on C++ that would explain why this
is not portable.
class test
{
public:
char *ptr;
void setPtr(char* p){
ptr = p;
}
void print(){
cout << ptr << endl;
}
};

main()
You need an explicit "int main()".
{
test t;
t.setPtr("abc");
t.print();
system("pause");
}

Is argument of function setPtr - "abc" - a temporary char table, made
on stack, which is deleted when setPtr function ends? If it is, the
"abc" is not on stack, it's a string literal and has static storage
duration.
The storage for these objects shall last for the duration of the program
pointer - ptr - point then to the memory addres which was deleted, so
print method should print random characters or may crash the program.
When I made char table using new:
What's a char table?
main()
{
char* tab = new char[4];
strcpy(tab, "abc");
test t;
t.setPtr(tab);
delete[] tab;
t.print();
system("pause");
}

print() method prints random characters.
Because of undefined behavior.
But why the first program doesn't crash / prints random chars ?
Read above.
--
http://techytalk.googlepages.com
Jun 27 '08 #2
thx.
Jun 27 '08 #3
me****@gmail.com wrote in news:b1ee3325-348a-41ec-88bd-d3aea0a2c784
@f36g2000hsa.googlegroups.com:
Hi,

Why this program work?

#include<iostream.h>
class test
{
public:
char *ptr;
void setPtr(char* p){
ptr = p;
}
void print(){
cout << ptr << endl;
}
};

main()
{
test t;
t.setPtr("abc");
t.print();
system("pause");
}

Is argument of function setPtr - "abc" - a temporary char table, made
on stack, which is deleted when setPtr function ends? If it is, the
"abc" is a string literal which has static duration (compiled into the
executable code and not going anywhere from there). Thus it will be alive
during the whole program.

Besides, I recommend to get familiar with std::string and forget the
string lifetime issues forever.

Regards
Paavo


Jun 27 '08 #4
On May 7, 9:47 pm, Paavo Helde <nob...@ebi.eewrote:
meh...@gmail.com wrote in news:b1ee3325-348a-41ec-88bd-d3aea0a2c784
@f36g2000hsa.googlegroups.com:
Besides, I recommend to get familiar with std::string and
forget the string lifetime issues forever.
I can't let that pass. The only times I use C style strings is
when lifetime is an issue; a static char[] with a constant
initializer is static initialized, and effectively has an
infinite lifetime. A static std::string can easily be accessed
before it is constructed, or after it is destructed.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #5
James Kanze <ja*********@gmail.comwrote in news:65660d3f-3e1f-4924-93e7-
8e**********@j22g2000hsf.googlegroups.com:

[...]
>
Nobody suggested using char[] for mutable objects.
And nobody has objected using string literals in the code.

Paavo
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Yang Song | last post: by
7 posts views Thread by Dave | last post: by
5 posts views Thread by Johnathan Doe | last post: by
13 posts views Thread by Ekim | last post: by
18 posts views Thread by Pedro Pinto | last post: by
17 posts views Thread by dtschoepe | last post: by
11 posts views Thread by krreks | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.