Click here to Skip to main content
15,881,644 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: MessageBox / Win32 Pin
Gwenio7-Apr-10 15:07
Gwenio7-Apr-10 15:07 
AnswerRe: MessageBox / Win32 Pin
wuyouzi297-Apr-10 21:03
wuyouzi297-Apr-10 21:03 
AnswerRe: MessageBox / Win32 Pin
KarstenK8-Apr-10 0:19
mveKarstenK8-Apr-10 0:19 
Questionfrom the index pos in ch[],delete the the string of len Pin
wbgxx7-Apr-10 13:17
wbgxx7-Apr-10 13:17 
AnswerRe: from the index pos in ch[],delete the the string of len Pin
Richard Andrew x647-Apr-10 13:32
professionalRichard Andrew x647-Apr-10 13:32 
GeneralRe: from the index pos in ch[],delete the the string of len Pin
wbgxx7-Apr-10 22:21
wbgxx7-Apr-10 22:21 
GeneralRe: from the index pos in ch[],delete the the string of len Pin
Gwenio8-Apr-10 3:41
Gwenio8-Apr-10 3:41 
AnswerRe: from the index pos in ch[],delete the the string of len [modified] Pin
Gwenio7-Apr-10 13:41
Gwenio7-Apr-10 13:41 
void StrDelete1(char ch[], int pos,int len)
/*from the index pos in ch[],delete the the string of len*/
{
    if (pos<0 || pos >(MAX-len))        {
            printf("the string delete error!");
            exit(1);
        }
    int i;
    for (i=0; i<MAX; i++)    printf("%c", ch[i]);
    char *temp = (char*)malloc(sizeof(char));
    if (temp == NULL)    {
       exit(1);
    }
    for (i=pos; i<MAX-len; i++)    {
        temp = &ch[i];
         free(temp);
    }
        printf("\n");
       for(i=0; i<MAX; i++)    printf("%c", ch[i]);
}


First off, you should remove "char *temp = (char*)malloc(sizeof(char));". The allocated memory is never used, and it is never released.

To my knowledge, free() releases a block of memory, not just that at the address pointed to. Therefore what you are trying to do may not be possible. I suggest you either call free() on the array (if it is allocated with malloc()) and release it, or set the element at pos to '\0'.

Also, you are accessing memory you are trying to free at the end of the function. Do not do that.

EDIT:

Okay, I think I have figured out away to accomplish you goal. Here is a sample to get you started in the right direction (you may want to revise it):

char * DeleteString1(char * ch,int pos,int len) {
    memmove(ch+pos,ch+pos+len,strlen(ch)-(pos+len));//Move content past pos+len to pos.
    return realloc(ch,strlen(ch));//Return a downsized memory block - should be the same as ch, but it is good to be sure.
}

modified on Wednesday, April 7, 2010 8:27 PM

GeneralRe: from the index pos in ch[],delete the the string of len Pin
KarstenK8-Apr-10 0:21
mveKarstenK8-Apr-10 0:21 
GeneralRe: from the index pos in ch[],delete the the string of len Pin
Gwenio8-Apr-10 3:33
Gwenio8-Apr-10 3:33 
GeneralRe: from the index pos in ch[],delete the the string of len Pin
KarstenK8-Apr-10 3:48
mveKarstenK8-Apr-10 3:48 
GeneralRe: from the index pos in ch[],delete the the string of len Pin
Gwenio8-Apr-10 4:24
Gwenio8-Apr-10 4:24 
GeneralRe: from the index pos in ch[],delete the the string of len Pin
KarstenK8-Apr-10 20:37
mveKarstenK8-Apr-10 20:37 
AnswerRe: from the index pos in ch[],delete the the string of len Pin
Eugen Podsypalnikov7-Apr-10 20:10
Eugen Podsypalnikov7-Apr-10 20:10 
QuestionRe: from the index pos in ch[],delete the the string of len Pin
David Crow8-Apr-10 3:43
David Crow8-Apr-10 3:43 
AnswerRe: from the index pos in ch[],delete the the string of len Pin
wbgxx8-Apr-10 4:00
wbgxx8-Apr-10 4:00 
Questioncannot convert from 'double' to 'class _variant_t * Pin
browneyes867-Apr-10 9:38
browneyes867-Apr-10 9:38 
AnswerRe: cannot convert from 'double' to 'class _variant_t * Pin
Iain Clarke, Warrior Programmer7-Apr-10 10:03
Iain Clarke, Warrior Programmer7-Apr-10 10:03 
AnswerRe: cannot convert from 'double' to 'class _variant_t * Pin
Eugen Podsypalnikov7-Apr-10 10:04
Eugen Podsypalnikov7-Apr-10 10:04 
Questionresolving a shortcut file name Pin
Alan Kurlansky7-Apr-10 9:30
Alan Kurlansky7-Apr-10 9:30 
AnswerRe: resolving a shortcut file name Pin
Nuri Ismail7-Apr-10 10:10
Nuri Ismail7-Apr-10 10:10 
QuestionIs forward declaration a hack for bad, lazy programmers? Pin
Vaclav_7-Apr-10 7:16
Vaclav_7-Apr-10 7:16 
AnswerRe: Is forward declaration a hack for bad, lazy programmers? Pin
Chris Losinger7-Apr-10 7:25
professionalChris Losinger7-Apr-10 7:25 
AnswerRe: Is forward declaration a hack for bad, lazy programmers? PinPopular
Moak7-Apr-10 7:48
Moak7-Apr-10 7:48 
GeneralRe: Is forward declaration a hack for bad, lazy programmers? Pin
William Winner7-Apr-10 11:41
William Winner7-Apr-10 11:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.