i get error message

This is a discussion on i get error message within the C++ Programming forums, part of the General Programming Boards category; hello im new to c++ and im trying out this exercise from a book where i have to convert list ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    i get error message

    hello im new to c++ and im trying out this exercise from a book where i have to convert list of char* to c style strings then convert it to a vector of strings.
    here's the code:

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    
    using namespace std;
    
    int main()
    {
    	list<char*> c;
    	c.push_back("hi");
    	c.push_back("fu");
    	c.push_back("ga");
    	vector<string> sv;
    	char** c2=new char*[c.size()];
    
           
    	for (int i=0; i < 2; i++)
    		c2[i]=new char[2];
            // convert to c style strings
    	for (list<char*>::iterator j=c.begin(); j!=c.end(); ++j)
    	{
    		static int i=0;
    		c2[i]=*j;
    		++i;
    	}
            // convert to vector of strings
    	for (char**p=c2; p < c2+c.size(); ++p)
    		sv.push_back(*p);
      
    	for (vector<string>::iterator i=sv.begin(); i!=sv.end(); ++i)
    		cout << *i << endl;
            // the codes below seem to cause the problem. i use free version of visual c++ and when i exclude the code below, the program runs fine. 
    	for (int i=0; i<c.size(); ++i)
    		delete []c2[i];
    	delete [] c2;
    	system("pause");
    	return 0;
    }
    visual c++ generates an error message:
    Debug Assertion Failed!
    Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

  2. #2
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    I got a few things:

    # 1. When making strings out of arrays of characters, you would do it like this:
    Code:
    string test = "Hi";
    or like this, in "c-style":
    Code:
    char test[3];
    test[0] = 'H';
    test[1] = 'i';
    test[2] = '\0';
    Now, it's vital you remember to add that \0 at the end, or else, functions will have trouble understanding where your string ends


    #2. Also, remember that doing this: char* test = "Hi", is a bad idea.
    This is because "Hi" gives you a const char*, not char*

    #3. Try to avoid using system(), as it's OS-dependent.
    Last edited by Drogin; 04-09-2009 at 07:11 PM.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by Drogin View Post
    or like this, in "c-style":
    Code:
    char test[3];
    test[0] = 'H';
    test[1] = 'i';
    test[2] = '\0';
    Now, it's vital you remember to add that \0 at the end, or else, functions will have trouble understanding where your string ends
    Or more commonly...
    Code:
    char test[] = "Hi";
    Your for loop is overwriting the memory you allocated with the pointer in the list. This is a memory leak. Later when you try and delete[] the memory, you are actually trying to delete the pointers stored in the list.

    I'm not sure if this is being done for an assignment, but why not use std::string instead of char* in your program?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Also, you are not allocating enough memory in the first place. You need to add an extra byte for the null terminator. Then fix your bug by changing this
    Code:
    c2[i]=*j;
    to this
    Code:
    strcpy(c2[i], *j);

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21