|
Given his code it will be a sort routine like bubble sort, but it looks like homework to me so I am reluctant.
Usually they are asked to do an in place array sort and it. He hasn't worked out how to typecast the pointers properly and it seems to escape him that you can just malloc the arrays which is what his "new" statement hides.
In vino veritas
modified 20-Sep-16 6:53am.
|
|
|
|
|
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 <iostream>
#include <string>
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:"<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
cout << "b:"<<b[0]<<" "<<b[1]<<" "<<b[2]<<endl;
swap((void *) a,(void *) b);
cout << "a:"<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
cout << "b:"<<b[0]<<" "<<b[1]<<" "<<b[2]<<endl;
cout << "=== END ===" << endl; getchar();
}
modified 18-Aug-17 5:26am.
|
|
|
|
|
Why are you using void* in your swap method and calls? Void pointers do not point to anything so the compiler cannot figure out what to do. Since both arrays are of int type, then that is the type of pointer you should be using. Also your swap method does not make much sense, again you are swapping pointers not data.
|
|
|
|
|
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:"<<a1[0]<<" "<<a1[1]<<" "<<a1[2]<<endl;
cout << "b:"<<b1[0]<<" "<<b1[1]<<" "<<b1[2]<<endl;
I tried this function using C++11 and seems to work:
template<class A>
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:"<<a1[0]<<" "<<a1[1]<<" "<<a1[2]<<endl;
cout << "b:"<<b1[0]<<" "<<b1[1]<<" "<<b1[2]<<endl;
}
Unfortunately I cannot call swap using a and b:
swap(a,b)
modified 21-Aug-17 3:52am.
|
|
|
|
|
void *x;int *a1=a,*b1=b;
x=(void *) a1;a1=b1;b1=(int *) x;
That just makes no sense, x should be int* the same as a and b .
|
|
|
|
|
As I am sure this is homework I won't give you the answer, what I will tell you is the problem with the typecast
Using the dereference operator (the *) is like mathematics there is an order to things just like in mathematics.
If I gave you 2 + 3 * 4 and you wanted the add before the multiply you need brackets (2 + 3) * 4 the mult carries higher precedence normally.
Same problem happens in dereference operator when using arrays
double **a;
*a[2] has two possible breakdowns so lets use brackets (*a)[2] or *(a[2])
*a[2] is actually equivalent to the latter *(a[2])
I bet that is not what you were expecting and reading it as
Your code doesn't do anything like what your text comments say because you are missing brackets. You need the dereference completed prior to the array use and (*a)[2] is the correct use for you.
Lets give you a simple code
double myArray[6] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 };
double *p = &myArray[0];
double d = p[3];
double **q = &p;
double s = (*q)[3];
double t = *q[2];
Does that last line look familiar?
It is the same if the you are derefencing an object or a class
*a.xyz may naively be intended as (*a).xyz or *(a.xyz)
*a->xyz may naively be intended as (*a)->xyz or *(a->xyz)
So look up and learn the precedence order of dereferencing and if you want the other USE BRACKETS. It's actually good practice to always use brackets if you are dereferencing complex items so you know for sure what the order will be be, you do the same with long chains with mathematics. It's a normal gotcha to not dereference what you think you are on complex items because of dereference precedence order (you can't just read it left to right and expect that order).
In vino veritas
modified 21-Sep-16 1:38am.
|
|
|
|
|
|
Can anyone suggest me, how to implement program that break (.png) image in two equal part?
|
|
|
|
|
|
If you're able to, you really should consider leveraging c++ for this task.
Doing so allows you to use functionality built into windows for well over a decade - GDI+.
GDI+ allows one to read formats windows supports natively, also including methods that facilitate the saving of bitmap images to files on disk.
|
|
|
|
|
He has asked for C and he said nothing about Windows so why bring up Windows/GDI ... You need to remember not everything that is requested is for a PC and often not on Windows.
It's highly possible he doesn't have a C++ compiler available or even as an option for what he is working on. There is a very large embedded industry out there and almost none of those micro-controllers and processors have a C++ compiler available.
Don't make assumptions about what he is doing.
In vino veritas
|
|
|
|
|
Simple. He didn't explicitly say they would be no good. CodProject is, as you're no doubt fully aware, an MS-centric site. It's also not uncommon for new members to be less explicit than would be beneficial.
I don't need to remember anything pal. I'm fully aware that plenty of articles here are for Arduino projects.
No-poop sherlock. I've spent time in it myself.
Go back and read my post, ya dolt. Here, I've saved you the trouble. I even highlighted the important parts you seemed to have made your own assumptions about.
If you're able to, you really should consider leveraging c++ for this task.
The only assumption I made was that all offerings of help made in good faith would be gratefully received - not necessarily useful, but received gratefully all the same.
Go and get a dog up ya
|
|
|
|
|
I have no idea why you feel the rant is necessary .. I have referred it to admin
I found your comment strange for the following reasons, so we are clear
1.) The original PNG library was written in C and is still maintained (20 years so far)
2.) It's dubious there is any advantage on converting PNGLIB to C++ you are just likely to get bugs any C++ compiler can compile PNGLIB anyhow
3.) The OP said nothing about windows
4.) Even if you are on windows the GDI+ does not support PNG because it is licenced .. SEE => PNGLIB.
Even IPicture which support JPG and GIF does not support PNG. So you would still have to write your own GDI+ extension code.
There is no malice intended, I just think your advice was misguided for the above reasons. Now if I am in error on any of those points then please let me know.
In vino veritas
modified 20-Sep-16 6:36am.
|
|
|
|
|
You might use a library, or, as others suggested, GDI+ . The natural way for accessing GDI+ is using C++ . However it exposes also a (not recommended) flat API[^].
|
|
|
|
|
Same comment as above the poster said nothing about being on Windows. Why would you assume he has it available? He asked to split a PNG file with C code that was it.
In vino veritas
|
|
|
|
|
Because this is the C/C++/MFC forum. It is just a guess, anyway.
|
|
|
|
|
Okay so in C you will find references to libpng which was done for a computer book all over the internet, it is a platform-independent library that contains C functions for handling PNG images that is 20 years old. Technically the code is ANSI C (C89) which should run on most C compilers even those for micro-controllers.
It isn't bad and easily understandable. You will often find more up to date conversion of the original code
but it's more often you will run across the read implementation than the write.
The code to use the png library isn't hard as you will see
A simple libpng example program[^]
You download the png library from the pnglib homepage
libpng Home Page[^]
There are manuals and "howto" instructions and pages there.
In vino veritas
modified 19-Sep-16 13:29pm.
|
|
|
|
|
We are using the Carlos Antollini ado classes in our C/C++ program and when I try to close the database it hangs. The function never returns. I cannot figure out why. Anyone have any ideas?
m_pProjectDatabase declared as:
CADODatabase* m_pProjectDatabase;
if (m_pProjectDatabase != nullptr)
{
m_pProjectDatabase->Close();
}
This calls the ado2 function:
void CADODatabase::Close()
{
try
{
if(IsOpen())
{
if(m_doxCatalog.m_pCatalog != NULL)
{
_ConnectionPtr pConn = m_doxCatalog.m_pCatalog->GetActiveConnection();
pConn->Close();
}
}
}
catch(CString ex)
{
throw;
}
catch(_com_error &e)
{
dump_com_error(e);
}
catch(...)
{
throw (CString)"Unexplained Error in CADODatabase::Close";
}
}
and from there, nothing. Off into cyberspace never to be seen again.
|
|
|
|
|
Did you debug this CADODatabase::Close() code?
|
|
|
|
|
I have 2 vc 630 both of them the drink section is inop. I ran a motor count and it recognize the first 20 motors and not the drink section everything seem to be plugged in that I can see James
|
|
|
|
|
Sorry, we are software developers, primarily. We are not vending machine support people.
|
|
|
|
|
the muffler bearings need more walnut oil
|
|
|
|
|
I think some tickets to a seminar by Tony Robins sounds like just what your machines need.
|
|
|
|
|
Call machine support.
I think nothing can be done without physical access to the machines.
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
I receive an old project developing with COM and C++, this project throw much exception for the INVALID_POINTER,
I guess maybe the pointer is release, but then use this pointer.
Is there anyway to check where the memory leak and where the pointer is released?
|
|
|
|