Solved

Deleting pointer arrays

Posted on 2006-06-12
16
247 Views
Last Modified: 2010-04-01
Getting errors when I execute this code,
I believe there is something wrong with the way I am deleting the tokenList
Can someone please check this out for me please.

Thanks,
Raj

CODE:


#include <stdio.h>
#include <string.h>

int tokenize(char *m_buffer, char* m_seperators, char** m_tokenList, int m_tokenCount)
{
      char *token;
      int count=0;      
      
      token = strtok (m_buffer,m_seperators);      
      
      while (token!=NULL && (count<m_tokenCount))
      {
            printf("%d", count);

            m_tokenList[count] = new char[strlen(token)];
            sprintf(m_tokenList[count],"%s",token);            
            
            count++;
            
            token = strtok( NULL, m_seperators );
      }            
      
      return 0;
}

int main(int argc, char* args[])
{
      char *tokenList[8] = {NULL};
      char *newBuffer = new char[25];

      strcpy(newBuffer,"ONE=TWO=THREE=FOUR=FIVE=SIX");

      tokenize(newBuffer,"=\n",tokenList,8);

      for(int i=0;i<8;i++)
      {
            if(tokenList[i]!=NULL)
            {
                  printf("\ni=%d",i);
                  printf("\ntokenList[%d]=%s",i,tokenList[i]);                  
            }
            else
            {
                  printf("\ntokenList[%d] is NULL",i);
            }
      }

      for (i=0;i<8;i++)
      {
            delete [] tokenList[i];
      }

      return 0;
}
0
Comment
Question by:rkbhumagani
  • 9
  • 4
  • 3
16 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 16887630
Hi rkbhumagani,
> int tokenize(char *m_buffer, char* m_seperators, char** m_tokenList,
> int m_tokenCount)

Your function type is passing by value the pointer to an array, so it can not change the calling function's variable.
Try passing a reference to it.

David Maisonave (Axter)
Cheers!
0
 
LVL 30

Expert Comment

by:Axter
ID: 16887695
Here's the corrected code:
#include <stdio.h>
#include <string.h>

int tokenize(char *m_buffer, char* m_seperators, char** &m_tokenList, int m_tokenCount)
{
    char *token;
    int count=0;    

    token = strtok (m_buffer,m_seperators);    

    while (token!=NULL && (count<m_tokenCount))
    {
        printf("%d", count);

        m_tokenList[count] = new char[strlen(token) +1];
        sprintf(m_tokenList[count],"%s",token);          

        count++;

        token = strtok( NULL, m_seperators );
    }          

    return 0;
}

int main(int argc, char* args[])
{
    char **tokenList = new char*[8]();
    char *newBuffer = new char[25];

    strcpy(newBuffer,"ONE=TWO=THREE=FOUR=FIVE=SIX");

    tokenize(newBuffer,"=\n",tokenList,8);

    for(int i=0;i<8;i++)
    {
        if(tokenList[i]!=NULL)
        {
            printf("\ni=%d",i);
            printf("\ntokenList[%d]=%s",i,tokenList[i]);              
        }
        else
        {
            printf("\ntokenList[%d] is NULL",i);
        }
    }

    for (i=0;i<8;i++)
    {
        delete [] tokenList[i];
    }
    delete [] tokenList;

    return 0;
}

0
 
LVL 30

Expert Comment

by:Axter
ID: 16887706
Axter,
> m_tokenList[count] = new char[strlen(token) +1];

Notice that I added a +1.  The original code was not including the NULL terminator in require length.


David Maisonave (Axter)
0
 

Author Comment

by:rkbhumagani
ID: 16888021
I am able to print the data, that is not my problem, the issue is deleting tokenList:

This is the output I get:

012345
i=0
tokenList[0]=ONE
i=1
tokenList[1]=TWO
i=2
tokenList[2]=THREE
i=3
tokenList[3]=FOUR
i=4
tokenList[4]=FIVE
i=5
tokenList[5]=SIX
tokenList[6] is NULL
tokenList[7] is NULL

and then I get a dialog box with the following message

Debug Error!
Program: C:\ENG\Codebase\test2\Debug\test2.exe
DAMAGE: after Normal block [#52] at 0x002F0FE0
(Press Retry to debug the application)

If I remove this part:

  for (i=0;i<8;i++)
     {
          delete [] tokenList[i];
     }


it runs fine, so it looks like I am not de-allocating memory properly.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 16888186
The pointers in the tokenList array point to parts of newBuffer. Delete newBuffer only.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 16888212
i.e.

--------8<--------
int main(int argc, char* args[])
{
    char **tokenList = new char*[8]();
    char *newBuffer = new char[25];

  // ...

    delete [] tokenList;
    delete [] newBuffer;


    return 0;
}
--------8<--------

strtok works by poking '\0' characters into the buffer, which you are tokenising. It doesn't do any memory allocation.
0
 
LVL 30

Expert Comment

by:Axter
ID: 16888226
>>I am able to print the data, that is not my problem, the issue is deleting tokenList:

It doesn't matter if you're able to print the data.
Writing to areas that are not allocated properly will cause undefined behavior, which include runtime errors when de-allocating memory.

Please try the code changes, before dismissing it.

You're getting the error because you're not allocating enough memory.
0
 
LVL 30

Expert Comment

by:Axter
ID: 16888243
>>it runs fine, so it looks like I am not de-allocating memory properly.

The problem is that you're not allocating memory properly.

Removing deallocation is just resulting in a memory leak, which you're not going to get a runtime error for in your program.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Expert Comment

by:Axter
ID: 16888278
To reiterate:
The following line of code is the reason you're getting a runtime error:

m_tokenList[count] = new char[strlen(token)];

You can fix the above code by just adding one:

m_tokenList[count] = new char[strlen(token) +1];


Please try the code change, so you can verify.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 16888325
> To reiterate:

David is right. I didn't read it properly.
0
 

Author Comment

by:rkbhumagani
ID: 16888433
I used the exact code you have sent and I get the following error message:


The instruction at "0x00401f53" referenced memory at "0xcdcdcdcd". The memory could not be "read"

Click on OK to terminate the program
Click on CANCEL to debug the program

0
 
LVL 30

Accepted Solution

by:
Axter earned 500 total points
ID: 16888449
>>I used the exact code you have sent and I get the following error message:

Try using your original code, and just change the line of code for the allocation.
m_tokenList[count] = new char[strlen(token) +1];

0
 
LVL 30

Expert Comment

by:Axter
ID: 16888461
I also recommend you post the new code, even if you think it's the same code.
0
 

Author Comment

by:rkbhumagani
ID: 16888944
Hello Axter,

it works now, i made the following change
m_tokenList[count] = new char[strlen(token) +1];
and it works fine. Thanks a lot man. Was juggling between 2 - 3 different thigns and failed to see your point.

I assummed that to delete an array of pointers:

char *tokenList[8];

I needed to do something different other than

for (int i=0;i<8;i++)
{
   delete [] tokenList[i];
}

So this is a valid way to delete the pointer array, or is there any other better way. In short is this programming method better or do you suggest any improvements.

Thanks,
Raj
0
 
LVL 30

Expert Comment

by:Axter
ID: 16889060
>>So this is a valid way to delete the pointer array, or is there any other better way. In short is this programming method better or do you suggest any improvements.

I always recommend avoiding using new and delete when you can.
You can avoid using new and delete, by using std::vector<std::string>, which will give you an array of strings.

By using a vector, you don't have to worry about calling new or delete, or managing the size or allocations/deallocations.

0
 

Author Comment

by:rkbhumagani
ID: 16889085
Thank you Axter. I was intentionally avoiding STL to check out with regular data types. But I think I will get back to using vectors. Thanks a lot.

Raj
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

910 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

16 Experts available now in Live!

Get 1:1 Help Now