Seg Fault AND 2d dynamic arrays

This is a discussion on Seg Fault AND 2d dynamic arrays within the C++ Programming forums, part of the General Programming Boards category; I am having trouble creating 2d dynamic arrays -- right now the code just produces a seg. fault. I can't ...

  1. #1
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61

    Seg Fault AND 2d dynamic arrays

    I am having trouble creating 2d dynamic arrays -- right now the code just produces a seg. fault. I can't figure it out... any help will be appreciated.

    CODE: http://pastebin.com/d6816f184

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are only allocating one set of columns for each of your rows. You need to have a loop to allocate columns for every row you have.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. lname[MAX] (for instance) does not exist -- you only created 0 through MAX-1 when you set up lname.
    2. lname[0][0] does not exist -- you never set up anything behind lname[0] (or lname[1], or lname[2], or ....) If you need to create two-dimensional arrays on the fly, you need to put a new[] in a loop so that you create a second dimension behind every possible index in the first dimension.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Just curious, why are you using dynamic arrays here? If it's because you are in a class and required to then that's too bad but there's not much you can do about it. If it's because you're trying to learn about how to deal with dynamic arrays inside classes as a learning exercise, then ok, although I hope you know that it's probably not the best choice in real code. If you're just doing it because you haven't heard of the alternative yet, then now might be a good time to look at that alternative.

    The alternative is a vector. I have yet to hear a good reason not to use a vector over a dynamic array (or a vector of vectors over a 2d dynamic array) other than for learning purposes. The vector is easier to use and harder to screw up.

    If you have to or want to continue with the dynamic array, please remember to implement or disable the copy constructor and copy assignment operator, as those will do the wrong thing if you let the compiler generate them for you. (Note that with a vector this wouldn't be necessary.)

    BTW, normally you don't need a dynamic array if you're just going to give it a constant size, and MAX looks like a constant. Is that just temporary for testing?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Dynamic Memory Allocation (Seg. Fault)
    By dr_jaymahdi in forum C++ Programming
    Replies: 10
    Last Post: 09-30-2007, 02:02 PM

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