Hi, I am still very new to C++ and need help with new/delete, I am posting what I wasn't able to find answers for on msn search or by myself after hours of testing..

1. Am I using new/delete correctly?

char hehe[1024];
    memset(hehe,0,sizeof(hehe));
    sprintf(hehe,"Some Text");
    
    char *testx;
    int testxlen = strlen(hehe) + 1;
    testx = new char[testxlen];
    for (size_t i = 0; i < testxlen; ++i) testx[i]=hehe[i];
    
    memset(hehe,0,sizeof(hehe));
    sprintf(hehe,"testx:%s\nsize:%d\nlen:%d\n",testx,sizeof(testx),strlen(testx));
    MessageBox(GetActiveWindow(),hehe,"Checking",MB_OK);
    //the size returns 4.. and the len returns 9, all my text is there..
    //but why is the size of the array smaller then the ammount of text? did i do something wrong?
    
    delete[] testx;
    memset(hehe,0,sizeof(hehe));
    sprintf(hehe,"testx:%s\nsize:%d\nlen:%d\n",testx,sizeof(testx),strlen(testx));
    MessageBox(GetActiveWindow(),hehe,"Checking",MB_OK);
    //the memory is deleted (correct?) then why does the size of the array still say 4?

2. Can I use memcpy() on a char created with new? How? (clueless, is it safe?)

3. If I have a global variable that is a pointer (char *haha), can I use new to allocate memory (and use delete[] to remove memory even if it hasn't been allocated first) and have haha point to it? Such as:

char *haha;
int main(void)
{
    char hehe[1024];
      memset(hehe,0,sizeof(hehe));
     sprintf(hehe,"Some Text");
    while (1) {
      MessageBox(GetActiveWindow(),haha,"Checking",MB_OK);
       delete[] haha;//is it safe to use this before memory is allicated? if haha points to nothing (when the program first starts and nothing has been allocated)
      MessageBox(GetActiveWindow(),haha,"Checking2",MB_OK);
      int testxlen = strlen(hehe) + 1;
      haha = new char[testxlen];
      for (size_t g = 0; g < testxlen; ++g) haha[g]=hehe[g];
      Sleep(1000);
    }
    delete[] haha;
    return 0;
}

4. Can anyone provide me with links that they found helpful with new/delete?

Anyone who is able to help me (Links, Comments, Code), thanks in advance.. I am making an attempt and wouldnt post unless I couldn't come up with an answer after trying/searching for hours on end..

Recommended Answers

All 3 Replies

1 Am I using new/delete correctly?

yes, u are using it fine.

the size returns 4.. and the len returns 9, all my text is there..
but why is the size of the array smaller then the ammount of text? did i do something wrong?

no, y did it right, just look at the thing u wrote

sprintf(hehe,"testx:%s\nsize:%d\nlen:%d\n",testx,[B]sizeof(testx),[/B]strlen(testx));

testx is a pointer, and size of a pointer is 4bytes so [B]sizeof(testx),[/B] returns 4, it does not returns the amount of memory pointed by the pointer.

//the memory is deleted (correct?) then why does the size of the array still say 4?

again u deleted the memory pointed by the pointer bt size of pointer remains same that is 4 bytes.
it is a good idea to point the pointer to null after performind delete for example

delete[] testx;
textx = NULL;

2 Can I use memcpy() on a char created with new? How? (clueless, is it safe?)

u can use it
example

char *p = new char[100] ;
char *q = new char[50] ;
memset(p,0,100);
memset(q,0,50);
strcpy(p,"Some Text") ;
memcpy(q,p,strlen(p));
MessageBox(GetActiveWindow(),q,"Checking q",MB_OK);

3 If I have a global variable that is a pointer (char *haha), can I use new to allocate memory (and use delete[] to remove memory even if it hasn't been allocated first) and have haha point to it?

globel variables are kept in data segemet of the programe and they are intialized to NULL, so if u use delete it actually deletes nothing, but i strongly recomed u to not use delete before u allocate memeory. and always set pointer to NULL after u perform delete .
example

char *q = new char[50] ;
memset(q,0,50);
strcpy(q,"Some Text") ;
MessageBox(GetActiveWindow(),q,"Checking q",MB_OK);
//.............other operations..............
//.............other operations..............

//atlast
if(q) {
    delete q ;
    q = NULL ;
}

4 Can anyone provide me with links that they found helpful with new/delete? ;)

pleace dont forget to forward the good links to me also, when u get them. :cool:

Thanks for replying dubeyprateek, you answered all the questions I had. :)

Member Avatar for iamthwee

Thanks for replying dubeyprateek, you answered all the questions I had. :)

Pray tell what is wrong with the std::string.

You can always use the c_str() to convert it back to a char array?

Or am I missing something? :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.