creating dynamically growing string using char*

S

shalakasan

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
 
R

red floyd

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.
 
S

shalakasan

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

red said:
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.
 
H

Howard

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
 
M

Marcus Kwok

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.
 
D

David Harmon

On 10 Aug 2006 09:58:33 -0700 in comp.lang.c++, (e-mail address removed)
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.
 
F

Frederick Gotham

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;
}
 
S

shalakasan

Thank u so much!


Frederick said:
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;
}
 
L

Luc The Perverse

David Harmon said:
On 10 Aug 2006 09:58:33 -0700 in comp.lang.c++, (e-mail address removed)
wrote,

It's time to stop not wanting to use std::string.

LOL - oh I like that
 
M

Michael

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
 
B

benben

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top