Crashing on delete of multidemensional array

Posted on 2011-04-26
Last Modified: 2012-06-21
I'm not sure what the issue is, but the execution freezes up on the first iteration of the delete in the free. This only happens though if i add the string "my str" to the array. I'm debugging in VS 2005, and the code literally just stops executing with no errors or cpu usage.
int ROWS=63000;
int COLUMNS=260;
char **dynamicArray;

//memory allocated for elements of rows.

dynamicArray = new char *[ROWS] ;

//memory allocated for  elements of each column.

for( int i = 0 ; i < ROWS ; i++ )
dynamicArray[i] = new char[COLUMNS];

int s=0;
	dynamicArray[s] = "my str";

//free the allocated memory

for( int i = 0 ; i < ROWS ; i++ )
delete [] dynamicArray[i] ;
delete [] dynamicArray ;

Open in new window

Question by:microwhat
    LVL 35

    Accepted Solution

    On line 17, you should be copying (strcpy) the string into your dynamic array. What is happening here is that you are setting the pointer in the dynamicArray to be equal to the location in memory that is the static string, and then in the loop below it is trying to free that static string, which it can't do.

    Try something like the below at line 17...

    strcpy(dynamicArray[i], "my str");

    Open in new window


    Author Closing Comment

    Ahh, makes sense.  Funny thing to is i used a strcpy_s at first. I don't even know why it didn't work at first. Oh Well. Still learned something new though. Thanks.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

    754 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

    17 Experts available now in Live!

    Get 1:1 Help Now