Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 10th, 2006, 05:25 PM
shalakasan@gmail.com
Guest
 
Posts: n/a
Default creating dynamically growing string using char*

Hi,

I am creating a pointer to char:
char * lDirName = new char;
I am trying to set values character by character.
lDirName[0]= 'c';
lDirName[1]= ':';
lDirName[2]= '\\';

The problem is that, the memory address that "lDirName " is pointing to
has some junk value in it. so instead of "lDirName" taking value as
"c:\" it takes value as "c:\1" or anything that is there in the memory.

I can't use static array because I don't know how long "lDirName "
going to be.

how to overcome this problem?

Best Regards,
Shal

  #2  
Old August 10th, 2006, 05:35 PM
red floyd
Guest
 
Posts: n/a
Default Re: creating dynamically growing string using char*

shalakasan@gmail.com wrote:
Quote:
Hi,
>
I am creating a pointer to char:
char * lDirName = new char;
I am trying to set values character by character.
lDirName[0]= 'c';
lDirName[1]= ':';
lDirName[2]= '\\';
>
The problem is that, the memory address that "lDirName " is pointing to
has some junk value in it. so instead of "lDirName" taking value as
"c:\" it takes value as "c:\1" or anything that is there in the memory.
>
I can't use static array because I don't know how long "lDirName "
going to be.
>
how to overcome this problem?
>
use std::string.
  #3  
Old August 10th, 2006, 05:55 PM
shalakasan@gmail.com
Guest
 
Posts: n/a
Default Re: creating dynamically growing string using char*

thanks, but i don't want to use std::string. how can make things better
without using it?

red floyd wrote:
Quote:
shalakasan@gmail.com wrote:
Quote:
Hi,

I am creating a pointer to char:
char * lDirName = new char;
I am trying to set values character by character.
lDirName[0]= 'c';
lDirName[1]= ':';
lDirName[2]= '\\';

The problem is that, the memory address that "lDirName " is pointing to
has some junk value in it. so instead of "lDirName" taking value as
"c:\" it takes value as "c:\1" or anything that is there in the memory.

I can't use static array because I don't know how long "lDirName "
going to be.

how to overcome this problem?
>
use std::string.
  #4  
Old August 10th, 2006, 05:55 PM
Howard
Guest
 
Posts: n/a
Default Re: creating dynamically growing string using char*


<shalakasan@gmail.comwrote in message
news:1155227241.199958.40700@q16g2000cwq.googlegro ups.com...
Quote:
Hi,
>
I am creating a pointer to char:
char * lDirName = new char;
I am trying to set values character by character.
lDirName[0]= 'c';
lDirName[1]= ':';
lDirName[2]= '\\';
>
The problem is that, the memory address that "lDirName " is pointing to
has some junk value in it. so instead of "lDirName" taking value as
"c:\" it takes value as "c:\1" or anything that is there in the memory.
>
I can't use static array because I don't know how long "lDirName "
going to be.
>
how to overcome this problem?
>
If you can't use std::string for some reason, then you could try this:
Quote:
get the length of the original string
new[] an array of char that's two larger (one for the new character, and
one for the NULL-terminator)
copy the old to the new
append the new character and a NULL-terminator
delete[] the old string
set the original pointer to point to the new string
(You should also new your original string with [1], and initialize it with
"", so that it has a NULL-terminator already.)

Or, assuming that this is in some kind of loop, you could use two loops, one
whose sole job is to determine the length of the string that you'll need,
and then another to actually fill it (after allocating enough space for the
string, plus one for the NULL-terminator).

(There may also be other ways to accomplish what you want, but you haven't
shown us how you're getting those individual characters, so it would be
difficult to guess on any improvements in that area.)

In my opinion, it should now be obvious, from the work required above if
nothing else, that using std::string is the better choice.

-Howard



  #5  
Old August 10th, 2006, 06:05 PM
Marcus Kwok
Guest
 
Posts: n/a
Default Re: creating dynamically growing string using char*

shalakasan@gmail.com wrote:
Quote:
I am creating a pointer to char:
char * lDirName = new char;
Note that lDirName is a pointer to a single char, not to a C-style
string. If you want a C-style string, you need an array of char:

char* lDirName = new char[size];
Quote:
I am trying to set values character by character.
lDirName[0]= 'c';
lDirName[1]= ':';
lDirName[2]= '\\';
>
The problem is that, the memory address that "lDirName " is pointing to
has some junk value in it. so instead of "lDirName" taking value as
"c:\" it takes value as "c:\1" or anything that is there in the memory.
>
I can't use static array because I don't know how long "lDirName "
going to be.
>
how to overcome this problem?
Use std::string (or some other string class that handles this). Since
you said you don't want to use std::string, you could write your own
string class.

Another option is to allocate a "big enough" array of char. Of course,
the problem is finding the right value for "big enough": if it's too
small then you will run out of space, and if it's too big then you're
wasting memory.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
  #6  
Old August 10th, 2006, 06:25 PM
David Harmon
Guest
 
Posts: n/a
Default Re: creating dynamically growing string using char*

On 10 Aug 2006 09:58:33 -0700 in comp.lang.c++, shalakasan@gmail.com
wrote,
Quote:
>thanks, but i don't want to use std::string. how can make things better
>without using it?
It's time to stop not wanting to use std::string.

  #7  
Old August 10th, 2006, 07:05 PM
Frederick Gotham
Guest
 
Posts: n/a
Default Re: creating dynamically growing string using char*

posted:
Quote:
Hi,
>
I am creating a pointer to char:
char * lDirName = new char;

This dynamically allocates a single char, and stores its address in a newly
defined char pointer.

Quote:
I am trying to set values character by character.
lDirName[0]= 'c';

No problem. This is equivalent to:

*lDirName = 'c';

Quote:
lDirName[1]= ':';

Here you write to memory which isn't yours.

Quote:
lDirName[2]= '\\';

And again here.

Quote:
The problem is that, the memory address that "lDirName " is pointing to
has some junk value in it. so instead of "lDirName" taking value as
"c:\" it takes value as "c:\1" or anything that is there in the memory.
>
I can't use static array because I don't know how long "lDirName "
going to be.
>
how to overcome this problem?

Perhaps something like:

#include <cassert>
#include <cstddef>
#include <cstring>
#include <iostream>

using std::size_t;
using std::strlen;
using std::strcpy;
using std::cout;

char *const PrependDrive(char const *const path)
{
assert(path);
size_t const len = strlen(path);
assert(len);

char *const buf = new char[len + 3 + 1];

char *p = buf;
*p++ = 'c';
*p++ = ':';
*p++ = '\\';
strcpy(p,path);

return buf;
}

int main()
{
char *const p = PrependDrive("windows\\system");

cout << p;

delete [] p;
}

--

Frederick Gotham
  #8  
Old August 10th, 2006, 07:35 PM
shalakasan@gmail.com
Guest
 
Posts: n/a
Default Re: creating dynamically growing string using char*

Thank u so much!


Frederick Gotham wrote:
Quote:
posted:
>
Quote:
Hi,

I am creating a pointer to char:
char * lDirName = new char;
>
>
This dynamically allocates a single char, and stores its address in a newly
defined char pointer.
>
>
Quote:
I am trying to set values character by character.
lDirName[0]= 'c';
>
>
No problem. This is equivalent to:
>
*lDirName = 'c';
>
>
Quote:
lDirName[1]= ':';
>
>
Here you write to memory which isn't yours.
>
>
Quote:
lDirName[2]= '\\';
>
>
And again here.
>
>
Quote:
The problem is that, the memory address that "lDirName " is pointing to
has some junk value in it. so instead of "lDirName" taking value as
"c:\" it takes value as "c:\1" or anything that is there in the memory.

I can't use static array because I don't know how long "lDirName "
going to be.

how to overcome this problem?
>
>
Perhaps something like:
>
#include <cassert>
#include <cstddef>
#include <cstring>
#include <iostream>
>
using std::size_t;
using std::strlen;
using std::strcpy;
using std::cout;
>
char *const PrependDrive(char const *const path)
{
assert(path);
size_t const len = strlen(path);
assert(len);
>
char *const buf = new char[len + 3 + 1];
>
char *p = buf;
*p++ = 'c';
*p++ = ':';
*p++ = '\\';
strcpy(p,path);
>
return buf;
}
>
int main()
{
char *const p = PrependDrive("windows\\system");
>
cout << p;
>
delete [] p;
}
>
--
>
Frederick Gotham
  #9  
Old August 10th, 2006, 07:55 PM
red floyd
Guest
 
Posts: n/a
Default Re: creating dynamically growing string using char*

shalakasan@gmail.com wrote:
Quote:
Thank u so much!
>
>
Frederick Gotham wrote:
Quote:
>[redacted]
Please don't top post. See FAQ 5.4:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.4
  #10  
Old August 11th, 2006, 01:45 AM
Luc The Perverse
Guest
 
Posts: n/a
Default Re: creating dynamically growing string using char*

"David Harmon" <source@netcom.comwrote in message
news:45016aae.126283359@news.west.earthlink.net...
Quote:
On 10 Aug 2006 09:58:33 -0700 in comp.lang.c++, shalakasan@gmail.com
wrote,
Quote:
>>thanks, but i don't want to use std::string. how can make things better
>>without using it?
>
It's time to stop not wanting to use std::string.
LOL - oh I like that

--
LTP

:)


  #11  
Old August 11th, 2006, 02:45 AM
Michael
Guest
 
Posts: n/a
Default Re: creating dynamically growing string using char*


shalakasan@gmail.com wrote:
Quote:
Hi,
>
I am creating a pointer to char:
char * lDirName = new char;
I am trying to set values character by character.
lDirName[0]= 'c';
lDirName[1]= ':';
lDirName[2]= '\\';
>
The problem is that, the memory address that "lDirName " is pointing to
has some junk value in it. so instead of "lDirName" taking value as
"c:\" it takes value as "c:\1" or anything that is there in the memory.
>
I can't use static array because I don't know how long "lDirName "
going to be.
>
how to overcome this problem?
>
Best Regards,
Shal
Hi,

You might want to put '\0' as an end. The following code might be what
you want.

#include <iostream>

using namespace std;

int main()
{

char* p=new char;
p[0]='a';
p[1]='b';
p[2]='\0';

do
{
cout<<*p++;
} while(*p);

return 0;

}

HTH,
Michael

  #12  
Old August 11th, 2006, 09:45 AM
benben
Guest
 
Posts: n/a
Default Re: creating dynamically growing string using char*

shalakasan@gmail.com wrote:
Quote:
thanks, but i don't want to use std::string. how can make things better
without using it?
If you strongly resist std::string then consider std::vector<char>
before you fiddle with new[] and delete[]. std::vector<probably does a
better job in memory management than what you might be able to craft.

But then you have to handle the terminating zero. Have fun.

Regards,
Ben
  #13  
Old August 11th, 2006, 12:15 PM
Gavin Deane
Guest
 
Posts: n/a
Default Re: creating dynamically growing string using char*


Michael wrote:
Quote:
char* p=new char;
I think you mean char* p = new char[3];
Quote:
p[0]='a';
p[1]='b';
p[2]='\0';
Gavin Deane

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles