Thread: How i can change 2dyanimc array int vector class

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    95

    How i can change 2dyanimc array int vector class

    // dynamic memory for 2D char array
    char **board = (char**) calloc(column, sizeof(char*));

    for(int i=0; i<column; i++)

    board[i] = (char*) calloc(row, sizeof(char));
    //for char 255 row and colum
    char temp[255];
    inputFile.getline(temp,255);
    //get line by line
    for(int j=0;j<column;j++)
    inputFile.getline(board[j],255);
    for(int k=0;k<column;k++)
    cout<<board[k]<<endl;

    so i want to change into vector class like

    Vector<board>row;

    thanks alot

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What idea do you have?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    95
    i was dynamic memory for 2D char array so i want to change into vector as class in order to be dynamic expand and shrink for the board

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As a side note, avoid calloc/malloc in C++. They don't work well with classes (they cause undefined behavior due to not calling constructors). Similarly, avoid free because it does not call destructors.
    Also, avoid char arrays of any kind. If it's a string you want, you should be looking at std::string. If it's a buffer you need, then you should be looking at either std::string or std::vector.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ph0x
    i was dynamic memory for 2D char array so i want to change into vector as class in order to be dynamic expand and shrink for the board
    That's what you want. What have you tried to get to what you want? Have you taken a good look at documentation for std::vector to see how you might be able to do this?

    Why do you even want a 2D char array in the first place? Is this supposed to be a container of strings?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    The name of the variable: 'board' gives me the idea that he's making a game board like he would in tic-tac-toe or something. In which case, if he wanted vectors, he could just use a single vector and refer to each slot as myVector[boardWidth*y+x] or use a vector in a vector to achieve his 2D array ordeal.

    If you want to change your code, try it. Don't try to dump your work on us and say 'Thanks. Do/Show me how to do this.' It's your project, not ours. The most I'm going to give you at this point is: http://www.cprogramming.com/tutorial/stl/vector.html and http://www.cplusplus.com/reference/vector/vector/
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Rodaxoleaux
    The name of the variable: 'board' gives me the idea that he's making a game board like he would in tic-tac-toe or something. In which case, if he wanted vectors, he could just use a single vector and refer to each slot as myVector[boardWidth*y+x] or use a vector in a vector to achieve his 2D array ordeal.
    Yes. My point is to get Ph0x to actually start thinking about what problem he/she is trying to solve and then communicate that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Rodaxoleaux View Post
    ...In which case, if he wanted vectors, he could just use a single vector and refer to each slot as myVector[boardWidth*y+x] or use a vector in a vector to achieve his 2D array ordeal....
    I really don't like trying to flatten 2 dimensions into one. It's ugly and it's error prone since you most likely will be repeating that pattern everywhere and chances are you can make a mistake somewhere. Much better to use a 2D vector instead or abstract it into some class that exposes a way to get the element at x and y.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Quote Originally Posted by Elysia View Post
    I really don't like trying to flatten 2 dimensions into one. It's ugly and it's error prone ... chances are you can make a mistake somewhere. ...
    I agree 100%. It's still an option though if he's uncomfortable with learning the logic behind containers inside of containers. I know that that was confusing for me when I started out.

    Quote Originally Posted by laserlight View Post
    Yes. My point is to get Ph0x to actually start thinking about what problem he/she is trying to solve and then communicate that.
    The more I read his responses to his threads, the more I think that that's impossible/not going to happen.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-09-2012, 09:47 PM
  2. Replies: 3
    Last Post: 10-05-2011, 12:25 AM
  3. Making stack class using vector class
    By spank in forum C++ Programming
    Replies: 9
    Last Post: 08-10-2007, 03:50 AM
  4. Need turotial/help class array/vector
    By ajpeters in forum C++ Programming
    Replies: 10
    Last Post: 09-08-2005, 12:17 PM
  5. grow function to change size of array class
    By Santos7772002 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:31 AM