Solved

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

Posted on 2001-06-20
18
280 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
[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
  • Learn & ask questions
  • 5
  • 5
  • 3
  • +4
18 Comments
 
LVL 5

Accepted Solution

by:
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

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

728 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