Click here to Skip to main content
14,771,608 members
Home » Discussions » C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: dotted rectangle is not being cleared if any empty string added in MFC comboboc Pin
leon de boer2-May-16 18:57
Memberleon de boer2-May-16 18:57 
QuestionCreation of class members of class created on the heap Pin
ForNow30-Apr-16 20:09
MemberForNow30-Apr-16 20:09 
AnswerRe: Creation of class members of class created on the heap Pin
Richard MacCutchan30-Apr-16 21:38
mveRichard MacCutchan30-Apr-16 21:38 
QuestionFIFO en languge C Pin
Member 1249381829-Apr-16 11:37
MemberMember 1249381829-Apr-16 11:37 
AnswerRe: FIFO en languge C Pin
Bram van Kampen29-Apr-16 12:48
MemberBram van Kampen29-Apr-16 12:48 
AnswerRe: FIFO en languge C Pin
Patrice T30-Apr-16 22:29
mvePatrice T30-Apr-16 22:29 
AnswerRe: FIFO en languge C Pin
CPallini1-May-16 21:49
mveCPallini1-May-16 21:49 
QuestionFour methods to create, pass to function and delete 2d arrays in c++ Pin
Javier Luis Lopez29-Apr-16 0:32
MemberJavier Luis Lopez29-Apr-16 0:32 
Is the first method correct for all the platforms?
I describe the three methods in the following code:

//Shows 4 different ways to create, pass to functions and delete 2d-arrays
#include <stdio.h>
#include <iostream>
using namespace std;//#include <utility>
//std::swap(matriz1m, matriz2m); //The best way but VC11 only!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

#define YMAX 6
#define XMAX 4

 
void print_data2d(float *matriz[XMAX],int max1,char *method);
void print_data2d(float (*matriz)[XMAX],int max1,char *method);
 
void print_adress(float (*matriz)[XMAX],int ymax,char *method);   
void print_adress(float *matriz[XMAX],int ymax,char *method);   
 
//This print method only works for method 1 & 2:
void print_data2d(float *matriz,int ymax,int xmax,char *method); 
//This print method only works for method 3 & 4:
void print_data2d(float **matriz,int ymax,int xmax,char *method); 
 

 
int main()
{
	int x,y;
 
	//The three methods to create the arrays:

	//1. First method: one-step "new" and one-step adress assign:
	//1.1. Buffer memory reservation:
	float *matriz1_m=new float[XMAX*YMAX];//Buffer
	//1.2. Address assign:
	float (*matriz1)[XMAX]=(float (*)[XMAX]) &matriz1_m[0];//Assign

 
	//2. Second method: No buffer needed. Only one-step "new" and one-step adress assign:
	float (*matriz2)[XMAX]=new float[YMAX][XMAX];//array definition   [XMAX], caution!!
 
	//3. Another method: by using for loop for "new" and adress assign:
	//3.1. Array declaration:
	float *matriz3[YMAX];   // [YMAX] caution!!
	//3.2. Address assign:
	for (y=0;y<YMAX;y++)
		matriz3[y]=new float[XMAX];

	//4. The ACTUAL CLASSICAL METHOD
	//4.1. Create and Array of pointers:
	float **matriz4 = new (float*[YMAX]);   // Create an array of pointers
	//4.2. Fill each pointer in the array with a allocated pointer to a block of memory:
	for (int y = 0; y < YMAX; y++)
		matriz4[y] = new float[XMAX];; 
 
	//Filling thhe four arrays:
	for (y=0;y<YMAX;y++)
		for (x=0;x<XMAX;x++)
		{
			matriz1[y][x]=matriz2[y][x]=matriz3[y][x]=matriz4[y][x]=(y+1)*1000.0f+x;
		}
	
	print_data2d(&matriz1[0][0],YMAX,XMAX,"Values 1st method");
	//This only works for 3rd method:
	print_data2d(matriz3,YMAX,XMAX,"Values 3rd method");
	print_data2d(matriz4,YMAX,XMAX,"Values 4th method");

	print_data2d(matriz1,YMAX,"Values 1st method");
	print_data2d(matriz2,YMAX,"Values 2nd method");
	print_data2d(matriz3,YMAX,"Values 3rd method");
	print_data2d(matriz4,YMAX,"Values 4rd method");
 
	print_adress( matriz1,XMAX,"Adress 1st method");
	print_adress( matriz2,XMAX,"Adress 2nd method");
	print_adress( matriz3,XMAX,"Adress 3rd method");
	print_adress( matriz4,XMAX,"Adress 3rd method");
 
	//Array size
	//Minimum array size:
	long size=sizeof(float)*XMAX*YMAX;
	printf("\nMinimum    =%li",size);
	size= (sizeof(float) + (long) (&matriz1[YMAX-1][XMAX-1]) ) - (long) &matriz1[0][0];
	printf("\nArray 1    =%li",size);
	size= (sizeof(float) + (long) (&matriz2[YMAX-1][XMAX-1]) ) - (long) &matriz2[0][0];
	printf("\nArray 2    =%li",size);
	size= (sizeof(float) + (long) (&matriz3[YMAX-1][XMAX-1]) ) - (long) &matriz3[0][0];
	printf("\nArray 3    =%li",size);
	size= (sizeof(float) + (long) (&matriz4[YMAX-1][XMAX-1]) ) - (long) &matriz4[0][0];
	printf("\nArray 4    =%li",size); 
 
	//Memory free for the three methods:
	//1.3. For the first creation method only one delete is used as long as one "new" was used:
	delete matriz1_m;
	//2.3. For the second creation method also only one delete is used as long as one "new" was used:
	delete[] matriz2;
	//3.3. For the third method as long as a loop was used for "new" commands, a for loop is needed for delete commands:
	for (int y=0;y<YMAX;y++)
		delete matriz3[y];
	//4.3. For the third method as long as a loop was used for "new" commands, a for loop is needed for delete commands:
	for (int y=0;y<YMAX;y++)
		delete matriz4[y];
	printf("\n\n=== END ===");getchar();getchar();
	return 1;
}
 
void print_adress(float (*matriz)[XMAX],int xmax,char *method)   
{
	cout << "\n============= INI DIR "<< method << "=============\n";
	int x,y;
	for (y=0;y<YMAX;y++)
	{
		for (x=0;x<xmax;x++)
			cout << ((long) (&matriz[y][x]) - (long) (&matriz[0][0])) << '\t';
		cout <<endl;
	}
	cout << "\n============= END DIR "<< method << "=============\n";
}
 
void print_adress(float *matriz[XMAX],int xmax,char *method)   
{
	cout << "\n============= INI DIR "<< method << "=============\n";
	int x,y;
	for (y=0;y<YMAX;y++)
	{
		for (x=0;x<xmax;x++)
			cout << ((long) (&matriz[y][x]) - (long) (&matriz[0][0])) << '\t';
		cout <<endl;
	}
	cout << "\n============= END DIR "<< method << "=============\n";
}
 
//Prueba de paso de matriz 2d
void print_data2d(float *matriz[XMAX],int maxy,char *method)
{
	cout << "\n============= INI VAL "<< method << "=============\n";	
	long y,x;
	for (y=0;y<maxy;y++)
	{
		for (x=0;x<XMAX;x++)
		{
			cout << matriz[y][x] <<'\t';
		}
		cout <<endl;
	}
	cout << "============= FIN VAL "<< method << "=============\n";	
}
void print_data2d(float (*matriz)[XMAX],int maxy,char *method)
{
	cout << "\n============= INI VAL "<< method << "=============\n";	
	long y,x;
	for (y=0;y<maxy;y++)
	{
		for (x=0;x<XMAX;x++)
		{
			cout << matriz[y][x] <<'\t';
		}
		cout <<endl;
	}
	cout << "============= FIN VAL "<< method << "=============\n";	
}
 

void print_data2d(float **matriz3,int ymax,int xmax,char *method) 
{
	cout << "\n============= INI VAL "<< method << "=============\n";	
	long y,x;
	for (y=0;y<ymax;y++)
	{
		for (x=0;x<xmax;x++)
		{
			cout << matriz3[y][x] <<'\t';
		}
		cout <<endl;
	}
	cout << "============= FIN VAL "<< method << "=============\n";	
}
 

void print_data2d(float *matriz1,int ymax,int xmax,char *method) 
{
	cout << "\n============= INI VAL "<< method << "=============\n";	
	long y,x;
	for (y=0;y<ymax;y++)
	{
		for (x=0;x<xmax;x++)
		{
			cout << matriz1[y*xmax+x] <<'\t';
		}
		cout <<endl;
	}
	cout << "============= FIN VAL "<< method << "=============\n";	
}


modified 30-Apr-16 5:09am.

AnswerRe: Three methods to create, pass to function and delete 2d arrays in c++ Pin
Bram van Kampen29-Apr-16 13:27
MemberBram van Kampen29-Apr-16 13:27 
GeneralRe: Three methods to create, pass to function and delete 2d arrays in c++ Pin
Javier Luis Lopez30-Apr-16 0:05
MemberJavier Luis Lopez30-Apr-16 0:05 
AnswerRe: Three methods to create, pass to function and delete 2d arrays in c++ Pin
leon de boer29-Apr-16 19:01
Memberleon de boer29-Apr-16 19:01 
GeneralRe: Three methods to create, pass to function and delete 2d arrays in c++ Pin
Javier Luis Lopez30-Apr-16 0:08
MemberJavier Luis Lopez30-Apr-16 0:08 
GeneralRe: Three methods to create, pass to function and delete 2d arrays in c++ Pin
leon de boer30-Apr-16 1:47
Memberleon de boer30-Apr-16 1:47 
QuestionHow to send huge data via sockets in continuous intervals Pin
manoharbalu28-Apr-16 23:52
Membermanoharbalu28-Apr-16 23:52 
AnswerRe: How to send huge data via sockets in continuous intervals Pin
Shyam Kodase29-Apr-16 0:39
MemberShyam Kodase29-Apr-16 0:39 
GeneralRe: How to send huge data via sockets in continuous intervals Pin
manoharbalu29-Apr-16 1:32
Membermanoharbalu29-Apr-16 1:32 
GeneralRe: How to send huge data via sockets in continuous intervals Pin
Shyam Kodase22-May-16 22:15
MemberShyam Kodase22-May-16 22:15 
AnswerRe: How to send huge data via sockets in continuous intervals Pin
Shyam Kodase29-Apr-16 0:42
MemberShyam Kodase29-Apr-16 0:42 
QuestionPhone Dialer Pin
Bram van Kampen28-Apr-16 13:57
MemberBram van Kampen28-Apr-16 13:57 
QuestionRe: Phone Dialer Pin
David Crow28-Apr-16 18:16
MemberDavid Crow28-Apr-16 18:16 
AnswerRe: Phone Dialer Pin
Bram van Kampen29-Apr-16 12:43
MemberBram van Kampen29-Apr-16 12:43 
QuestionRe: Phone Dialer Pin
David Crow30-Apr-16 10:30
MemberDavid Crow30-Apr-16 10:30 
AnswerRe: Phone Dialer Pin
Bram van Kampen1-May-16 15:30
MemberBram van Kampen1-May-16 15:30 
SuggestionRe: Phone Dialer Pin
David Crow1-May-16 17:32
MemberDavid Crow1-May-16 17:32 
GeneralRe: Phone Dialer Pin
Bram van Kampen2-May-16 15:12
MemberBram van Kampen2-May-16 15:12 

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.