[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Crashing on delete of multidemensional array

Posted on 2011-04-26
Medium Priority
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 36

Accepted Solution

mccarl earned 2000 total points
ID: 35471962
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

ID: 35472168
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

873 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