468,491 Members | 2,010 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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

Aug 10 '06 #1
12 3191
sh********@gmail.com wrote:
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.
Aug 10 '06 #2
thanks, but i don't want to use std::string. how can make things better
without using it?

red floyd wrote:
sh********@gmail.com wrote:
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.
Aug 10 '06 #3

<sh********@gmail.comwrote in message
news:11*********************@q16g2000cwq.googlegro ups.com...
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:
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

Aug 10 '06 #4
sh********@gmail.com wrote:
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];
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
Aug 10 '06 #5
On 10 Aug 2006 09:58:33 -0700 in comp.lang.c++, sh********@gmail.com
wrote,
>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.

Aug 10 '06 #6
posted:
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.

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

No problem. This is equivalent to:

*lDirName = 'c';

lDirName[1]= ':';

Here you write to memory which isn't yours.

lDirName[2]= '\\';

And again here.

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
Aug 10 '06 #7
Thank u so much!
Frederick Gotham wrote:
posted:
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.

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


No problem. This is equivalent to:

*lDirName = 'c';

lDirName[1]= ':';


Here you write to memory which isn't yours.

lDirName[2]= '\\';


And again here.

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
Aug 10 '06 #8
sh********@gmail.com wrote:
Thank u so much!
Frederick Gotham wrote:
>[redacted]
Please don't top post. See FAQ 5.4:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.4
Aug 10 '06 #9
"David Harmon" <so****@netcom.comwrote in message
news:45****************@news.west.earthlink.net...
On 10 Aug 2006 09:58:33 -0700 in comp.lang.c++, sh********@gmail.com
wrote,
>>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

:)
Aug 11 '06 #10

sh********@gmail.com wrote:
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

Aug 11 '06 #11
sh********@gmail.com wrote:
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
Aug 11 '06 #12

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

Aug 11 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Chris Fink | last post: by
7 posts views Thread by mike | last post: by
11 posts views Thread by Zordiac | last post: by
64 posts views Thread by Philip Potter | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.