Dynamic array sizing causes crash

This is a discussion on Dynamic array sizing causes crash within the C++ Programming forums, part of the General Programming Boards category; I'm trying to allocate memory to my array but I get a crash. int *cellWidth; int cols = 50; cellWidth=(int*)malloc(sizeof(int)*cols); ...

  1. #1
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293

    Dynamic array sizing causes crash

    I'm trying to allocate memory to my array but I get a crash.

    int *cellWidth;
    int cols = 50;
    cellWidth=(int*)malloc(sizeof(int)*cols);

    That's a pretty trimmed up version of my source, only the relevant lines out of it.

    Anways I get a crash on XP pro, I gather it's a simple problem I tend to crash my programs with these memory things all the time but never really understood half of them except for the ones that go out of array bounds.

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    int *cellWidth;
    int cols = 50;
    cellWidth = new int[cols];

    (brackets or parenthesis, i forget, but that hsould be right. unless im missing something)

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The first is really something in C (though, C++ has it as well), the second C++. I think your crash is elsewhere.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    *smacks head* thanks Zach, I did exactly the same thing with my 2d array I was setting the variable before I had sized it which was causing the crash.

    Cheers to you also revelation!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner: dynamic array of strings
    By pc2-brazil in forum C++ Programming
    Replies: 10
    Last Post: 04-29-2008, 05:29 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 10:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 12:48 PM

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