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

x
?
Solved

Crashing on delete of multidemensional array

Posted on 2011-04-26
2
Medium Priority
?
342 Views
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;
while(s<63000){
	dynamicArray[s] = "my str";
	s++;
}

//free the allocated memory


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

Open in new window

0
Comment
Question by:microwhat
2 Comments
 
LVL 36

Accepted Solution

by:
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

0
 

Author Closing Comment

by:microwhat
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.
0

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