?
Solved

how to enlarge the length of a char string and...

Posted on 2001-06-20
18
Medium Priority
?
284 Views
Last Modified: 2010-04-02
i'm comming.
help? the question:
char * a="12345678";

we know the length of a is 8.now i want to enlarge the length  of a but the address of a doesn't change.for example after i enlarge the *a's length,the *a may be "123456789",we konw that the system malloc the a and the *(a+8)='\0',when i make *(a+8)='9' ;*(a+9)='\0',the error MAY occupy because the address a+9 may be private or i cannot access (write) .
how to solve the problem.

thanks
           
0
Comment
Question by:mysetee
  • 5
  • 5
  • 3
  • +4
18 Comments
 
LVL 5

Accepted Solution

by:
djbusychild earned 200 total points
ID: 6213114
I'm sorry, but I'm having problems understanding your Q.

if you're wonderng how you can enlarge a given memory block, you can use realloc(). be aware that realloc can move the whole memory segement to a new location, free the original space and return you that new pointer.
0
 
LVL 10

Expert Comment

by:makerp
ID: 6213192
malloc the first time then realloc

/* include one for the NULL char */
char *buffer = (char*)malloc(9);

strcpy(buffer,"12345678");

buffer = (char*)realloc(buffer,(strlen(buffer) + 1) + 10);

strcat(buffer,"91011121314");
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6213663
> malloc the first time then realloc

the trouble is that realloc may move the buffer to a new location.

The only way to ensure that it does not move is to allocate enough space in the first place.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 10

Expert Comment

by:makerp
ID: 6213671
or use _expand, that dont move a block but has a greater chance of failing cos it does not move the block. it dont matter if the buffer moves providing there are not pointers pointing to the buffer elsewhere in the code that will become stuffed if the block moves
0
 
LVL 30

Expert Comment

by:Axter
ID: 6213902
Don't use a pointer, and assign the proper amount as per jasonclarke above comment.

char a[32]="12345678";
0
 
LVL 11

Expert Comment

by:griessh
ID: 6214158
Folks

mysetee has three questions open now with this problem. In all comments we try to explain to him that an expression like char * a="12345678"; doesn't let him modify the string, because it is a const. We all tried to ask him to allocate memory (malloc or char a[]). He just doesn't want to listen. I think it is useless to discuss that topic any further.

mysetee

I would suggest you close two of your open questions and concentrate on one only! You will not get better answers by asking the same question over and over again.
======
Werner
0
 
LVL 10

Expert Comment

by:makerp
ID: 6214172
well spotted, i have noticed an influx of fools at EE of late
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6214525
> well spotted, i have noticed an influx of fools at EE of late

that's unfair.  There is nothing stupid about the question.  This kind of comment can only serve to stop people from asking questions.  The forum is open to all levels of C++ programmer.
0
 

Expert Comment

by:Trull
ID: 6216363
if there is no explicit reason that makes you allocate the memory for the variable at programming time, you should use dynamic allocation. the easiest way would be to create a function that creates a new variable and return the address of this new variable to your 'a' that you want to keep the same.
if this is interesting to you, let me know and i can scetch some code for you.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6216592
Hi Trull, welcome to EE

We appreciate your help here, but in EE we have a few rules, one of them you will find (together with others) at the end of this page:
==========================================================
Comments
Comments are intended to be used as a collaboration tool. Many Experts choose to post their solutions as comments only.

Answers
An answer is a specific solution to a question and should be submitted if it will solve the questioner's problem and doesn't duplicate a previous comment
==========================================================
We talked about alloction (static and dynamic) in these threads several times already, so your answer is not really a new addition. The basic problem of mysetee is that he does not understand the concept of a string constant.
0
 

Expert Comment

by:Trull
ID: 6216601
i realised that i shouldnt have posted as an answer after this post. i read a notice similar to yours, that a moderator was giving another person. Thanks for taking your time and being polite.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6216691
Hey Trull, we need everybody here to solve problems. And everybody started somewhen. So again, welcome in EE and thanks for your support.

======
Werner
0
 
LVL 10

Expert Comment

by:makerp
ID: 6217494
> well spotted, i have noticed an influx of fools at EE of late

that's unfair.  There is nothing stupid about the question.  This kind of comment can only serve to
stop people from asking questions.  The forum is open to all levels of C++ programmer

this question has been asked 3 times by the same person, thats what i mean by what i said
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6217587
> asked 3 times by the same person, thats what i mean by what i said

this often seems to happen by accident. I am not sure if that is what has happened here.
0
 
LVL 10

Expert Comment

by:makerp
ID: 6217680
i retract my comment then.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6217998
Sorry jasonclarke

That was not an 'accident'. If you look at the questions, they have different titles, slightly different topic, different days, but always the same answer: "You cannot change a string constant. You have to allocate memory for a variable".
I wouldn't say it as harsh as makerp did, but as I stated earlier here, asking the same question again and again shows me the asker doesn't (want to) understand the problem.

======
Werner
0
 
LVL 11

Expert Comment

by:griessh
ID: 7014566
Dear mysetee

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "dbusychild"

comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7030986
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question