473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 2010

<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Yang Song | last post by:
HI, I am a little confused about char * and char. How would I be able to return a char* created in a function? Is new() the only way? How would I be able to return a point and a value at the same...
7
by: wwj | last post by:
Hi ,all I want to know the difference between char a and char *p=new char and the difference between the heap and the stack ,and if the char a is corresponding to the stack in MEMORY,and char...
7
by: Dave | last post by:
I've got a problem. I can't seem to figure out how to make a char* buffer for use as a socket buffer using operator 'new' in it's basic form. In affect, I need to allocate space dynamically in...
5
by: Johnathan Doe | last post by:
Why is is necessary to write a function that allocates memory for, or changes, a pointer to a char array using char** instead of char*? E.g. void modify_string( char** string ) { if( string...
13
by: Ekim | last post by:
hy to all, I'm accessing a function from a managed-c++-dll within my c#-application. Therefore I encountered following troubles: In my c#-app I have got a byte-array, which should somehow be...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
18
by: happyvalley | last post by:
Hi, basically, the test function get a char pointer, and assigned a string to it. then the string is passed back by the call-by-reference mechanism. in test(), I reallocate some memory for the...
17
by: dtschoepe | last post by:
Hi, I have a homework project I am working on, so be forwarned, I'm new to C programming. But anyway, having some trouble with a memory allocation issue related to a char * that is a variable...
11
by: krreks | last post by:
Hi. I'm trying to figure out pointers and memory allocation in C. I've read quite a few explanations on the internet and have read in a few books that I've got, but I'm not so much smarter yet......
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.