Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
J

Javier Luis Lopez

@Javier Luis Lopez
About
Posts
21
Topics
6
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Elon Musk proposes city-to-city travel by rocket, right here on Earth
    J Javier Luis Lopez

    "Once the rockets are up, who cares where they come down?"

    I should prefer it does not land on my house! I think a rocket for going to mars may be totally different than one to go to Japan, because interplanetary one needs 2 or 3 stages and a Earth based one only one.

    The Insider News com question

  • Right method of swapping arrays and classes using pointers
    J Javier Luis Lopez

    Because I would like to swap pointers, not to copy all the data. If I swap 2 arrays of 1k size using pointers is 1000x times faster than copying all the data. This code do it using void * pointers (but I do not know if it could work in other SO), so the next step is to place it in a function:

    int a\[3\]={100,101,102};
    int b\[3\]={200,201,202};
    void \*x;int \*a1=a,\*b1=b;
    x=(void \*) a1;a1=b1;b1=(int \*) x;
    cout << "a:"<
    

    I tried this function using C++11 and seems to work:

    template
    void swap(A *&a,A *&b)
    {
    A *x=a;a=b;b=x;
    }

    void main()
    {
    int a[3]={100,101,102};
    int b[3]={200,201,202};
    int *a1=a,*b1=b;
    swap(a1,b1);
    cout << "a:"<

    Unfortunately I cannot call swap using a and b:

    swap(a,b)

    C / C++ / MFC com data-structures tutorial lounge

  • Right method of swapping arrays and classes using pointers
    J Javier Luis Lopez

    The idea is use the pointers to avoid copying all the data. An example, if there are 3 arrays of pixels: A, B, C and I want to order the arrays I can make the following operation:

    if (distance(A,B)>distance(A,C))
    swap(B,C);

    I tried this code that swaps anything but it does not compile:

    #include
    #include

    using namespace std;

    void swap(void *&a,void *&b)
    {
    void *x=a;a=b;b=x;
    }

    void main()
    {
    int a[3]={100,101,102};
    int b[3]={200,201,202};
    cout << "a:"<

    C / C++ / MFC com data-structures tutorial lounge

  • Right method of swapping arrays and classes using pointers
    J Javier Luis Lopez

    I have read a lot of stuf of programmers triying to swap arrays using for over every element like here (two pages): [^] There also other methods that involves making swapping element by element but it is hidden: std::swap(array1,array2); It would be better using pointers, with only 3 operations all the elements are swapped: double *a,*b; two arrays double **ptr1,**ptr2,**ptrx; ptr1=&a;ptr2=&b; Swapping: ptrx=ptr1;ptr1=ptr2;ptr2=ptrx; Example code:

    #include

    using namespace std;

    double *a;
    double *b;

    class c_class { public: double x; } ;

    void main()
    {
    //1. Initialize of the array:
    a = new double[7];
    b = new double[7];
    long i;
    for (i = 0; i < 7; i++)
    {
    a[i] = i + 100;
    b[i] = i + 300;
    }

    //2. We need three pointers:
    double \*\*ptr1 = &a,\*\*ptr2=&b,\*\*ptrx;
    
    //3. Printing initial array values:
    cout << "=== SWAPPING ARRAYS ===" << endl;
    cout << "STEP 1: ptr1= " << endl;
    for (i = 0; i < 7; i++) cout << (\*ptr1)\[i\] << " ";
    cout << " ptr2= ";
    for (i = 0; i < 7; i++) cout << (\*ptr2)\[i\] << " ";
    cout << endl;
    
    //4. Swapping the arrays in only 3 pointer operations:
    cout << " Swapping the arrays" << endl;
    ptrx = ptr1; ptr1 = ptr2; ptr2 = ptrx;
    //5. Printing the arrays after swapping:
    cout << "STEP 2: ptr1= ";
    for (i = 0; i < 7; i++) cout << (\*ptr1)\[i\] << " ";
    cout << " ptr2= ";
    for (i = 0; i < 7; i++) cout << (\*ptr2)\[i\] << " ";
    cout << endl;
    
    //6. Swapping again the arrays in only 3 pointer operations:
    cout << " Swapping the arrays" << endl;
    ptrx = ptr1; ptr1 = ptr2; ptr2 = ptrx;
    //7. Printing the arrays after swapping: results must be the same than initial ones:
    cout << "STEP 3: ptr1= ";
    for (i = 0; i < 7; i++) cout << (\*ptr1)\[i\] << " ";
    cout << " ptr2= ";
    for (i = 0; i < 7; i++) cout << (\*ptr2)\[i\] << " ";
    cout << endl;
    
    cout << "\\n\\n=== SWAPPING CLASSES ==="<x << " "<x<
    
    C / C++ / MFC com data-structures tutorial lounge

  • Four methods to create, pass to function and delete 2d arrays in c++
    J Javier Luis Lopez

    Thank you leon de boer, I included the 4th method and modified the comment at printf_data2d, I will check the last part of the post.

    C / C++ / MFC c++ data-structures performance question

  • Four methods to create, pass to function and delete 2d arrays in c++
    J Javier Luis Lopez

    You are right, the second method of course works and is my preferred method as long as I do not need a loop to create and delete it, but I have to be careful when sending the array to a function. Thank you to advise that the first one fits with iso standard.

    C / C++ / MFC c++ data-structures performance question

  • Four methods to create, pass to function and delete 2d arrays in c++
    J Javier Luis Lopez

    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
    #include
    using namespace std;//#include
    //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
    
    C / C++ / MFC c++ data-structures performance question

  • OpenCL program uses only 1 core from 192 cuda cores
    J Javier Luis Lopez

    I edited it after reading to make it more easy to understand. I have seen that the old video card is not very good improving the performance of the CPU.

    C / C++ / MFC asp-net graphics question announcement

  • OpenCL program uses only 1 core from 192 cuda cores
    J Javier Luis Lopez

    I made following program to use opencl library supplied with Nvidia cuda 7.5. As result I obtained: Num of devices=1 Device name= quadro K600 Device vendor=NVIDIA Corporation Device Version=OPENCL 1.2 CUDA Driver Version= 353.90 DEVICE_MAX_COMPUTE_UNITS = 1 !!!!!!!!!!!!!!!!!!!!!!!!!! DEVICE_MAX_CLOCK_FREQUENCY = 875 MHZ DEVICE_GLOBAL_MEM_SIZE= 1073741824 Only 1 compute unit at 875 Mhz???? I read that K600 has 192

    #include
    #define __CL_ENABLE_EXCEPTIONS
    #pragma warning(disable:4996)
    #include
    #include
    #include
    #include
    #include

    #define LOCAL_SIZE 512
    #define WIDTH_A (4096*2)
    #define HEIGHT_A (4096*2)

    float *matrix_A;
    float *vector_B;
    float *result_vector;
    float *result_vector_host;

    #define MAX_SOURCE_SIZE (0x100000)

    int main()
    {
    cl_platform_id platform_id = NULL;
    cl_device_id device_id = NULL;
    cl_context context = NULL;
    cl_command_queue command_queue = NULL;
    cl_mem Amobj = NULL;
    cl_mem Bmobj = NULL;
    cl_mem Cmobj = NULL;
    cl_program program = NULL;
    cl_kernel kernel = NULL;
    cl_uint ret_num_devices;
    cl_uint ret_num_platforms;
    cl_int ret;

    int i, j;
    
    /\* Get Platform/Device Information\*/
    ret = clGetPlatformIDs(1, &platform\_id, &ret\_num\_platforms);
    ret = clGetDeviceIDs(platform\_id, CL\_DEVICE\_TYPE\_DEFAULT, 1, &device\_id, &ret\_num\_devices);
    
    /\* Create OpenCL Context \*/
    context = clCreateContext(NULL, 1, &device\_id, NULL, NULL, &ret);
    
    
    char buffer\[10240\];
    cl\_uint buf\_uint;
    cl\_ulong buf\_ulong;
    printf("Num of devices=%i", ret\_num\_devices);
    for (i = 0; i < ret\_num\_devices; i++)
    {
    	printf("  -- %d --\\n", i);
    	clGetDeviceInfo(device\_id, CL\_DEVICE\_NAME, sizeof(buffer), buffer, NULL);
    	printf("  DEVICE\_NAME = %s\\n", buffer);
    	clGetDeviceInfo(device\_id, CL\_DEVICE\_VENDOR, sizeof(buffer), buffer, NULL);
    	printf("  DEVICE\_VENDOR = %s\\n", buffer);
    	clGetDeviceInfo(device\_id, CL\_DEVICE\_VERSION, sizeof(buffer), buffer, NULL);
    	printf("  DEVICE\_VERSION = %s\\n", buffer);
    	clGetDeviceInfo(device\_id, CL\_DRIVER\_VERSION, sizeof(buffer), buffer, NULL);
    	printf("  DRIVER\_VERSION = %s\\n", buffer);
    	clGetDeviceInfo(device\_id, CL\_DEVICE\_MAX\_COMPUTE\_UNITS, sizeof(buf\_uint), &buf\_uint, NULL);
    	printf("  DEVICE\_MAX\_COMPUTE\_UNITS = %u\\n", (unsigned int)buf\_uint);
    	clGetDeviceInfo(device\_id, CL\_DEVICE\_MAX\_CLOCK\_FREQUENCY, sizeof(buf\_uint), &buf\_uint, NULL);
    	printf("  DEVICE\_MAX\_CLOCK\_FREQUENCY = %u\\n", (unsigned int)buf\_uint);
    	clGetDeviceIn
    
    C / C++ / MFC asp-net graphics question announcement

  • How to increase memory to avoid exception std::bad_alloc?
    J Javier Luis Lopez

    About using the task manager>process window, I found that there is an error of memory used varies about 4k from one run to the following, so it can be used to see memory variations in debug sessions taken in account that. I used it to run the following very simple code:

    #include void program_good()
    { //step #2
    char *memory=new char[10000L*1024L];
    memory[1024]=' '; //step #3
    delete[] memory;
    } //step #4

    void program_bad()
    {
    char *memory=new char[10000L*1024L];
    memory[1024]=' ';// step #6
    }

    void main()
    {
    double x[10];

    char ptr0\[1024\];//  step #1
    program\_good();
    char ptr1\[1024\];
    printf("\\nMemory diference=%li",(long) (&ptr1-&ptr0-1024));
    program\_bad();//	step #5
    char ptr2\[1024\];
    printf("\\nMemory diference=%li",(long) (&ptr2-&ptr1-1024));
    
    
    printf("\\n=== FIN ===");//step #6
    getchar();getchar();
    

    In the task manager I found that my "Prueba.exe" memory usage was: step #1 and #2: 484k step #3: 10508k step #4: 484k step #5: 492k (I do not know why printf used 8kb) step #6: 10516k As result the calling of the program_bad() leaks 10Mb plus 32kb So the task manager is not an exact tool but can help

    C / C++ / MFC performance help tutorial question

  • How to increase memory to avoid exception std::bad_alloc?
    J Javier Luis Lopez

    YES, you are right Thank you very much, you solved me a lot of problems

    C / C++ / MFC performance help tutorial question

  • How to increase memory to avoid exception std::bad_alloc?
    J Javier Luis Lopez

    I tried with VS2008 and it fails to allocate more than 2GB becaus delete command does not free memory. Using VS2015 (x86) fails also Using VS2015 x64 fails but more than 4GB are allocated

    C / C++ / MFC performance help tutorial question

  • How to increase memory to avoid exception std::bad_alloc?
    J Javier Luis Lopez

    As can you see in the code I used delete[], I used also delete only and in release mode but the memory was not freed. By using static allocation my program used 26MB only. I tried this simple code and worked fine:

    #include

    void main()
    {
    long i,size=((long)(1024L*1024L*50L/6L))*6;
    for (i=0;i<100;i++)
    {
    __int16 (*memory)[6]=new __int16[1024L*1024L*50L/6L][6];
    memory[1024L*1024L][5]=132;
    delete memory;
    printf("\nAllocated and deallocated %li MB",i*(size/1024L)*sizeof(__int16)/1024);
    }
    printf("\n=== FIN ===");
    getchar();getchar();
    }

    Perhaps the visual studio could not deallocate the memory to be reused because any operation reason

    C / C++ / MFC performance help tutorial question

  • How to increase memory to avoid exception std::bad_alloc?
    J Javier Luis Lopez

    Thank you Jochen. I used the windows task manager and I found that the delete command does not frees memory. I changed to static allocation and the program run. I used a VS2008 sp1, but perhaps I have to add any hotfixes. If someone is interested in the problem I can try also in VS2015 to see what happens

    C / C++ / MFC performance help tutorial question

  • How to increase memory to avoid exception std::bad_alloc?
    J Javier Luis Lopez

    I made a image comparison program, but an exception error appeared at iteration number 1350, the code is here:

    void compara_face(__int16 cara1[YMAX][XMAX],__int16 centro1[2*3],__int16 cara2[YMAX][XMAX],__int16 centro2[2*3],__int16 zoom_level,__int16 num_filtros,double error[2])
    {
    error[0]=error[1]=1e199;//error en direccion x e y
    if (zoom_level >DMAX) { printf("\nERROR en compara_face() zoom_level =%i > %i",zoom_level ,DMAX);return; }
    if (num_filtros>FMAX) { printf("\nERROR en compara_face() num_filtros=%i > %i",num_filtros,FMAX);return; }
    double coef[6];//coeficiente de trasformada de 3 puntos
    trasformada3p_hallacoef(centro1,centro2,coef);
    __int16 (*filtro2)[6]=new __int16[num_filtros][6];
    __int16 (*filtro3)[6]=new __int16[num_filtros][6]; ERROR IS HERE!!!!
    __int16 d=DELTA[zoom_level],z=zoom_level;//delta de x e y
    long i;__int16 x1,y1;

    //direccion x FILTROX\[DMAX\]\[FMAX\]\[2\];
    transformada3P\_dx(coef,FILTROX\[z\],filtro2,d,num\_filtros);
    transformada3P\_dy(coef,FILTROY\[z\],filtro3,d,num\_filtros);
    error\[0\]=error\[1\]=0.0;//error en direccion x e y
    for (i=0;i
    

    The error appear in the second "new" command. The program uses a lot of RAM, perhaps I need to compiler or linker to use more ram or allocate memory as static instead dynamic.
    The program uses about 2 gigabytes

    C / C++ / MFC performance help tutorial question

  • Proposal of a new C/C++ precompiler project
    J Javier Luis Lopez

    There are some questions, not one: - If anyone is interested in help me to make the precompiler (I can extract the functions but not display them to drag&drop using mouse) - What is the right forum or subforum (in codeproject if possible) - If it exist a code editor where the precompiler can be added

    C / C++ / MFC c++ python design data-structures performance

  • Proposal of a new C/C++ precompiler project
    J Javier Luis Lopez

    "Wrong place; please read" May be but I did not found a better place than "General Discussions>C-C++" and I can not move the post to other place and must not remove the post :(

    C / C++ / MFC c++ python design data-structures performance

  • Proposal of a new C/C++ precompiler project
    J Javier Luis Lopez

    Problems: - C, C++ declining due python and other high level languages - C, C++ programming structure comes from Jurassic era - Programmers are always redesigning the wheel - New software are heavy weight, so they takes lots of time to upload from hard disks - Every time a new project is written it must be copied the functions so perhaps it exist different functions version in the hard disk - In the hard disk exists dozen of projects with files and functions distributed in a unclear structure Proposed solution: To design a new precompiler (perhaps based in geany) that performs the following tasks: - Look for all functions in a function directory - Write them in a tree - The user drags and drop the functions from the tree using the mouse - The pre compiler writes the C program to be compiled using only the needed functions - More over the precompiler can access to web sites with functions written by other users - In the project it will appear a dictionary that advises what functions are used - If a function is modified it advises of the affected projects in order to write it with different name - It will be easily readable in a tree the functions dependence with other functions - Build the makefile - User can navigate through the project function tree structure to see one function - The precompiler adds multithreading headers if it is possible when some functions works in parallel - The precompiler can change the calling variable type with others (example double to float) and modifying internal function type definitions accordingly As example the user can write an only one file named “matrix.cpp” that contains ALL matrix functions like the followings. The commented lines before explain the function.

    //matrix2 is the inverse of matrix1. The matrix1 and 2 are squared
    //matrices of NxN dimension
    matrix_inverse(double *matrix1,double *matrix2,long N)

    //high speed inverse of two arrays
    matrix_inverse3x3(double *matrix1,double *matrix2)

    //matrix3=matrix1 x matrix2. The dimension of the 3 matrices is NxN
    matrix_multiplier(double *matrix1,double *matrix2,double *result,long N)

    How the function tree can be seen: http://s12.postimg.org/q50hwfpnh/Proposal_of_a_modern_C.png

    C / C++ / MFC c++ python design data-structures performance

  • Visual Studio error LNK2005: variable XXX redefined in xxx.obj
    J Javier Luis Lopez

    I tried it by delete from the .cpp but unfortunately following error appeared:

    error LNK2001: external symbol "int variable" (?variable@@3HA) unresolved

    I agree with you, there is not other choice. I do not like also global variables so I had not that problem before :)

    C / C++ / MFC help csharp c++ visual-studio tutorial

  • Visual Studio error LNK2005: variable XXX redefined in xxx.obj
    J Javier Luis Lopez

    It works! It must be "declared" as extern in the header file one time and "defined" one time as you said in the main.cpp. It must not be defined again at any other file (source.cpp) unless extern is added The dafault value must be placed in the main.cpp as you wrote I do not like the solution very much because I have to be careful when changing the name of the variable to do in both sides.

    C / C++ / MFC help csharp c++ visual-studio tutorial
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups