Click here to Skip to main content
15,890,512 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Message of right-clicking on column header of CListCtrl Pin
includeh107-Apr-10 23:00
includeh107-Apr-10 23:00 
RantRe: Message of right-clicking on column header of CListCtrl Pin
Iain Clarke, Warrior Programmer7-Apr-10 23:05
Iain Clarke, Warrior Programmer7-Apr-10 23:05 
GeneralRe: Message of right-clicking on column header of CListCtrl Pin
Adam Roderick J7-Apr-10 23:06
Adam Roderick J7-Apr-10 23:06 
GeneralRe: Message of right-clicking on column header of CListCtrl Pin
David Crow8-Apr-10 3:40
David Crow8-Apr-10 3:40 
AnswerRe: Message of right-clicking on column header of CListCtrl Pin
Rolf Kristensen12-Apr-10 9:26
Rolf Kristensen12-Apr-10 9:26 
GeneralBarcode Application Usage [was: Re: Message of right-clicking on column header of CListCtrl] Pin
gvMadhav24-Jul-10 17:53
gvMadhav24-Jul-10 17:53 
Questionactivex Pin
mazizi7-Apr-10 18:52
mazizi7-Apr-10 18:52 
AnswerRe: activex Pin
Garth J Lancaster7-Apr-10 20:19
professionalGarth J Lancaster7-Apr-10 20:19 
AnswerRe: activex Pin
KingsGambit7-Apr-10 21:32
KingsGambit7-Apr-10 21:32 
QuestionMessageBox / Win32 Pin
Fareed Rizkalla7-Apr-10 14:47
Fareed Rizkalla7-Apr-10 14:47 
AnswerRe: MessageBox / Win32 Pin
Gwenio7-Apr-10 14:59
Gwenio7-Apr-10 14:59 
GeneralRe: MessageBox / Win32 Pin
Fareed Rizkalla7-Apr-10 15:03
Fareed Rizkalla7-Apr-10 15:03 
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 

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.