Solved

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

Posted on 2001-06-20
279 Views
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
Question by:mysetee
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 5
• 3
• +4

LVL 5

Accepted Solution

djbusychild earned 50 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

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

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

LVL 10

Expert Comment

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

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

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

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

LVL 9

Expert Comment

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

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

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 are intended to be used as a collaboration tool. Many Experts choose to post their solutions as comments only.

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

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

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

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

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

ID: 6217680
i retract my comment then.
0

LVL 11

Expert Comment

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

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"

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/

======
Werner
0

LVL 6

Expert Comment

ID: 7030986
Force accepted

** Mindphaser - Community Support Moderator **
0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

How to programmatically differentiate between C and Java 10 219
is twain_32.dll cmpatible with windows 10 ? 10 267
Dynamically allocate memory 9 74
I could not set window to top 4 60
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base classâ€¦
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more â€¦
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 difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Botâ€¦
###### Suggested Courses
Course of the Month8 days, 18 hours left to enroll