Solved

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

Posted on 2001-06-20
18
274 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 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
> 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

by:makerp
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
well spotted, i have noticed an influx of fools at EE of late
0
 
LVL 9

Expert Comment

by:jasonclarke
Comment Utility
> 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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Expert Comment

by:griessh
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
Comment Utility
> 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
Comment Utility
i retract my comment then.
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
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
Comment Utility
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
Comment Utility
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now