C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

why char * gets overwritten after some time in execution...

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Jigar Mehta
Guest





PostPosted: Fri Jan 28, 2005 6:23 am    Post subject: why char * gets overwritten after some time in execution... Reply with quote



Hye all,
I have noted while debugging my program (or also in runtime
execution) that sometimes (not regularly) char * pointer gets
overwritten and looses its value...

So, because of this my couple of products have failed and this is the
reason I have made it compulsary to use char [] instead of char *
because char[] don't overwrite our data...

So, anybody from you can please make me understand why this happens ??

suppose my code is,

char *name;
strcpy(name, "Jigar Mehta");

..
..
..
..
//After some lines of execution (say 50 lines) if I see what's there in
name address, it gets overwritten and value becomes "" instead of
"Jigar Mehta"...

Back to top
Gianni Mariani
Guest





PostPosted: Fri Jan 28, 2005 6:53 am    Post subject: Re: why char * gets overwritten after some time in execution Reply with quote



Jigar Mehta wrote:
Quote:
Hye all,
I have noted while debugging my program (or also in runtime
execution) that sometimes (not regularly) char * pointer gets
overwritten and looses its value...

So, because of this my couple of products have failed and this is the
reason I have made it compulsary to use char [] instead of char *
because char[] don't overwrite our data...

So, anybody from you can please make me understand why this happens ??

suppose my code is,

char *name;
strcpy(name, "Jigar Mehta");


General answer to your problem - USE std::string.

Your problem is that "char *" is a *POINTER* to a char (or the first
element in an array of chars).

The definition :

char * str;

Does not allocate any storage for "char"s at all and if you write to an
unitialized "str" you are invoking - UNDEFINED BEHAVIOUR.

For example:

char * str; strcpy(str, "THIS OVERWRITES SOME RANDOM PLACE IN MEMORY");

You can allocate "automatic" storage by placing an array of chars in the
function and then point to it however this is another source of
"UNDEFINED BEHAVIOUR".

e.g.

char * foo()
{
char data[ 100 ];

char * pointer_to_char;

pointer_to_char = &data[0];

strcpy(str, "THIS IS DEALLOCATED WHEN EXECUTION LEAVES SCOPE");

return pointer_to_char;
}

Back to top
Jigar Mehta
Guest





PostPosted: Fri Jan 28, 2005 7:02 am    Post subject: Re: why char * gets overwritten after some time in execution Reply with quote



Thanks for you reply, but I am new to std::string that you suggested..
You please tell me how to use this in program by giving a code... I
understand the prob. that char * is doing... and I am programming in MS
VC++ 6.0.. So, will I be able to use std::string normally or any
special .h should be included..

Thanks again,

Jigar Mehta

Back to top
Karl Heinz Buchegger
Guest





PostPosted: Fri Jan 28, 2005 11:23 am    Post subject: Re: why char * gets overwritten after some time in execution Reply with quote

Jigar Mehta wrote:
Quote:

Thanks for you reply, but I am new to std::string that you suggested..
You please tell me how to use this in program by giving a code... I
understand the prob. that char * is doing... and I am programming in MS
VC++ 6.0.. So, will I be able to use std::string normally or any
special .h should be included..

You need a book. Seriously.
"Accelerated C++" by "Koenig & Moo"
is often a good choice.

--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Jigar Mehta
Guest





PostPosted: Fri Jan 28, 2005 12:01 pm    Post subject: Re: why char * gets overwritten after some time in execution Reply with quote

Thanks for the suggestion.. I will refer to it..

Back to top
Ioannis Vranos
Guest





PostPosted: Sat Jan 29, 2005 12:59 am    Post subject: Re: why char * gets overwritten after some time in execution Reply with quote

Jigar Mehta wrote:

Quote:
Thanks for you reply, but I am new to std::string that you suggested..
You please tell me how to use this in program by giving a code... I
understand the prob. that char * is doing... and I am programming in MS
VC++ 6.0.. So, will I be able to use std::string normally or any
special .h should be included..

VC++ 6 is too old and does not support the language in a satisfactory
level. Use VC++ 7.x (2002, 2003) or you can download and install Dev-C++:

http://www.bloodshed.net/dev/devcpp.html




--
Ioannis Vranos

http://www23.brinkster.com/noicys

Back to top
Jigar Mehta
Guest





PostPosted: Sat Jan 29, 2005 4:16 am    Post subject: Re: why char * gets overwritten after some time in execution Reply with quote

OK!! then is Dev-C++ giving same or more functionalities than what vc++
6.0 provides.. As I am used to get features from VC++ 6.0 so, It would
be difficult for me to migrate to something giving lesser
functionality...

Back to top
Jack Klein
Guest





PostPosted: Sat Jan 29, 2005 5:30 am    Post subject: Re: why char * gets overwritten after some time in execution Reply with quote

On 27 Jan 2005 22:23:04 -0800, "Jigar Mehta"
<jigarmehta (AT) gatescorp (DOT) com> wrote in comp.lang.c++:

Quote:
Hye all,
I have noted while debugging my program (or also in runtime
execution) that sometimes (not regularly) char * pointer gets
overwritten and looses its value...

So, because of this my couple of products have failed and this is the
reason I have made it compulsary to use char [] instead of char *
because char[] don't overwrite our data...

If the return address is legitimate, here is a statement from the home
page of Jigar Mehta's employer:

"GATES Information focuses on software products and software services
with international resources and access to latest technologies. With
our highly trained development teams; a blend of experience and
academic qualifications, quality and timely services are being offered
by us."

If the OP is member of one of their development teams, with a blend of
experience and academic qualifications, and not just a first semester
student, I shudder.

And I would avoid owning stock in the company...

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

Back to top
Jigar Mehta
Guest





PostPosted: Sat Jan 29, 2005 5:48 am    Post subject: Re: why char * gets overwritten after some time in execution Reply with quote

Thanks for the comment.. Jigar mehta is himself representing for the
question.. No need to comment on GATES Information.. and it will take
some time to analyse http://JK-Technology.com
Thanks again for pointing...

Back to top
Jigar Mehta
Guest





PostPosted: Sat Jan 29, 2005 5:51 am    Post subject: Re: why char * gets overwritten after some time in execution Reply with quote

And If I give my introduction to you, I am M.Sc. in computer
application and information techology (not a first year student.. but
thanks for giving a challenge, i like it).. age is 22.. from India..

Back to top
Ioannis Vranos
Guest





PostPosted: Sat Jan 29, 2005 8:50 am    Post subject: Re: why char * gets overwritten after some time in execution Reply with quote

Jack Klein wrote:

Quote:
If the return address is legitimate, here is a statement from the home
page of Jigar Mehta's employer:

"GATES Information focuses on software products and software services
with international resources and access to latest technologies. With
our highly trained development teams; a blend of experience and
academic qualifications, quality and timely services are being offered
by us."

If the OP is member of one of their development teams, with a blend of
experience and academic qualifications, and not just a first semester
student, I shudder.

And I would avoid owning stock in the company...


Well he may program in some other programming language(s) and just
checking C++ (or C thinking that it is the same, while it is not).




--
Ioannis Vranos

http://www23.brinkster.com/noicys

Back to top
Old Wolf
Guest





PostPosted: Sun Jan 30, 2005 8:04 pm    Post subject: Re: why char * gets overwritten after some time in execution Reply with quote

Jigar Mehta wrote:
Quote:

char *name;
strcpy(name, "Jigar Mehta");

And If I give my introduction to you, I am M.Sc. in computer
application and information techology (not a first year student.. but
thanks for giving a challenge, i like it).. age is 22.. from India..

What university?
Let us know, so we can avoid taking their C courses (and their
English courses, for that matter).


Back to top
Jigar Mehta
Guest





PostPosted: Mon Jan 31, 2005 5:06 am    Post subject: Re: why char * gets overwritten after some time in execution Reply with quote

Ha Ha Ha.. Thanks for the complements..

Back to top
Shan
Guest





PostPosted: Mon Jan 31, 2005 12:14 pm    Post subject: Re: why char * gets overwritten after some time in execution Reply with quote


Jigar Mehta wrote:
Quote:
Ha Ha Ha.. Thanks for the complements..
It is not a complement. Having a Msc degree in Computer Applications

doesnt seem to help you in coding correct C++ programs. I know that you
have undergone C++ courses in your Msc degree programme. If not then
throw the Msc degree out of the window and take some decent course
which will teach what it calims to teach you.
Shan


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.