Click here to Skip to main content
16,007,760 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Separet two strings Pin
sharanu5-May-08 1:43
sharanu5-May-08 1:43 
GeneralRe: Separet two strings Pin
Rajesh R Subramanian5-May-08 1:55
professionalRajesh R Subramanian5-May-08 1:55 
AnswerRe: Separet two strings Pin
Rajesh R Subramanian5-May-08 1:09
professionalRajesh R Subramanian5-May-08 1:09 
AnswerRe: Separet two strings Pin
Rajesh R Subramanian5-May-08 0:00
professionalRajesh R Subramanian5-May-08 0:00 
AnswerRe: Separet two strings Pin
David Crow5-May-08 3:37
David Crow5-May-08 3:37 
QuestionAsynchronous Serial Port Communication Pin
phanindra varma4-May-08 21:29
phanindra varma4-May-08 21:29 
AnswerRe: Asynchronous Serial Port Communication Pin
Rajkumar R5-May-08 3:35
Rajkumar R5-May-08 3:35 
QuestionMemory Management Problem C++ (malloc & free) Pin
dehseth4-May-08 20:41
dehseth4-May-08 20:41 
Hey everybody,
I'm using MS Visual C++ 6.0
I do have a code which contains classes in classes. Most inner class has a char** pointer which allocates memory using malloc function and adds a char array to string array.
To be more specific:

<br />
a = (char**) calloc(10, sizeof(char)); // main array<br />
<br />
for (int i = 0; i < 10; i++)<br />
{<br />
    a[i] = (char*) calloc(10 , sizeof(char)); // char arrays<br />
}<br />


the problem is I want a destructor which free this array.
Says Access Violation each time I try to execute. D'Oh! | :doh:

here is my code:

<br />
#include "stdafx.h"<br />
#include <malloc.h><br />
#include <stdio.h><br />
#include <conio.h><br />
<br />
<br />
class sub<br />
{<br />
public:<br />
	char** a;<br />
<br />
	sub();<br />
	~sub();<br />
};<br />
<br />
<br />
sub::sub()<br />
{<br />
	a = (char**) calloc(10, sizeof(char));<br />
<br />
	for (int i = 0; i < 10; i++)<br />
	{<br />
		a[i] = (char*) calloc(10 , sizeof(char));<br />
	}<br />
<br />
	printf("\n%x - %x - %x\n", a[0], &a[0], *a[0]);<br />
};<br />
<br />
sub::~sub()<br />
{<br />
	for (int i = 0; i < 50; i++)<br />
	{<br />
		char** A = a;<br />
		char* Ai = a[i];<br />
		char* Aii = (char *)*((int*)a[i]);<br />
<br />
		free(a[i]); // <big> crash point </big> :confused:<br />
	}<br />
};<br />
<br />
<br />
class allocater<br />
{<br />
public:<br />
	sub** subs;<br />
<br />
	allocater();<br />
	~allocater();<br />
<br />
};<br />
<br />
allocater::allocater()<br />
{<br />
	subs = (sub**) calloc(10, sizeof(subs));<br />
	int i; <br />
<br />
	for (i = 0; i < 10; i++)<br />
	{<br />
		subs[i] = new sub();<br />
	}<br />
};<br />
<br />
<br />
allocater::~allocater()<br />
{<br />
	int i; <br />
<br />
	printf("destructor allocater");<br />
<br />
	for (i = 0; i < 10; i++)<br />
	{<br />
		delete subs[i];<br />
	}<br />
};<br />
<br />
<br />
void main(int argc, char* argv[])<br />
{<br />
	allocater** a = (allocater**) calloc(10, sizeof(allocater));<br />
	int i;<br />
	<br />
	for (i = 0; i < 10; i++)<br />
	{<br />
		a[i] = new allocater();<br />
	}<br />
	<br />
	printf("allocated");<br />
<br />
<br />
//	getch();<br />
<br />
<br />
<br />
	for (i = 0; i < 10; i++)<br />
	{<br />
		delete a[i];<br />
	}<br />
<br />
<br />
	printf("freed");<br />
<br />
	getch();<br />
<br />
}<br />


Thank you everyone.. Smile | :)
AnswerRe: Memory Management Problem C++ (malloc & free) Pin
Cedric Moonen4-May-08 20:44
Cedric Moonen4-May-08 20:44 
AnswerRe: Memory Management Problem C++ (malloc & free) Pin
Rajkumar R4-May-08 21:16
Rajkumar R4-May-08 21:16 
AnswerRe: Memory Management Problem C++ (malloc & free) Pin
BadKarma4-May-08 21:25
BadKarma4-May-08 21:25 
QuestionRe: Memory Management Problem C++ (malloc & free) Pin
Rajkumar R4-May-08 21:34
Rajkumar R4-May-08 21:34 
AnswerRe: Memory Management Problem C++ (malloc & free) Pin
Rajesh R Subramanian4-May-08 22:41
professionalRajesh R Subramanian4-May-08 22:41 
AnswerRe: Memory Management Problem C++ (malloc & free) Pin
Eakalavya4-May-08 21:25
Eakalavya4-May-08 21:25 
GeneralRe: Memory Management Problem C++ (malloc & free) Pin
dehseth5-May-08 1:12
dehseth5-May-08 1:12 
AnswerRe: Memory Management Problem C++ (malloc & free) Pin
jhwurmbach4-May-08 21:36
jhwurmbach4-May-08 21:36 
AnswerRe: Memory Management Problem C++ (malloc & free) Pin
CPallini4-May-08 21:41
mveCPallini4-May-08 21:41 
GeneralRe: Memory Management Problem C++ (malloc & free) Pin
dehseth5-May-08 0:45
dehseth5-May-08 0:45 
QuestionRe: Memory Management Problem C++ (malloc & free) Pin
David Crow5-May-08 3:34
David Crow5-May-08 3:34 
AnswerRe: Memory Management Problem C++ (malloc & free) Pin
dehseth5-May-08 6:05
dehseth5-May-08 6:05 
GeneralRe: Memory Management Problem C++ (malloc & free) Pin
David Crow5-May-08 6:17
David Crow5-May-08 6:17 
QuestionUse Date/Time in SQL statement Pin
includeh104-May-08 20:29
includeh104-May-08 20:29 
QuestionRe: Use Date/Time in SQL statement Pin
Rajesh R Subramanian4-May-08 20:41
professionalRajesh R Subramanian4-May-08 20:41 
AnswerRe: Use Date/Time in SQL statement Pin
Ozer Karaagac5-May-08 0:03
professionalOzer Karaagac5-May-08 0:03 
QuestionNeed a solution in adding activeX controls in MFC dialog.... Pin
Khathar4-May-08 19:37
Khathar4-May-08 19:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.