473,385 Members | 1,569 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,385 software developers and data experts.

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

Similar topics

1
by: Chris Fink | last post by:
I am receiving xml documents from a customer without a reference to a doctype. I know what the Doctype DTD should be need to insert the declaration as follows <?xml version="1.0"...
7
by: mike | last post by:
Hi, I am having difficulty in creating a thread using pthread_create. It seems that pthread_create does not execute 'program', and returns -1; I have checked the API but I am not sure why this...
8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
6
by: Simon Verona | last post by:
I would normally use code such as : Dim Customer as new Customer Dim t as new threading.thread(AddressOf Customer.DisplayCustomer) Customer.CustomerId=MyCustomerId t.start Which would create...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
64
by: Philip Potter | last post by:
Hello clc, I have a buffer in a program which I write to. The buffer has write-only, unsigned-char-at-a-time access, and the amount of space required isn't known a priori. Therefore I want the...
0
by: Syoam4ka | last post by:
My project is about jewellery. I have devided my jewelery into main types, which each one of them has sub types, and each one those sub types has the jewellery. I have a tabcontainer. It includes...
2
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. creating a method to search a tree to find the position of node and to return its pointer value basically i...
5
by: satyabhaskar | last post by:
hi all, In my web page i have created radio buttons dynamically on to the page .....following is my code string Course, Semester, Section; int rowsCount; string con =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.