void * bubbleSort(void * data){
int * str;
str = (int* ) data;
int temp = 0;
for ( int i = 0 ; i < len ; i++ ){
for (int j = i + 1; j < len ; j++ ){
if(str[i] > str[j]){
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
}
int main(){
int *data= new int[1000000];
...
pthread_t thread[input];
for ( int i = 0 ; i < input ; i ++){
pthread_create(&thread[i],NULL,bubbleSort,arguments);
pthread_join(thread[i],NULL);
}
}
I have int *data[1000000] and I want to use pthread to pass parameter to bubble sort.
The above is the code I wrote, but it is no output
How to successfully work ?
To fix your immediate problem: your data variable is defined with the wrong type.
int *data[1000] is an array of 1000 int *, which decays to int**. Your bubbleSort function expects an int *.
Declare data as follows instead:
int * data = new int[10000];
and then you can simply pass it to pthread_create as a void* like you do now.
However, modern C++ has a std::thread which is far easier to work with:
std::thread sorter(bubbleSort, data);
sorter.join();
Related
need a better approach to pass address arr[0][2], given that is has to be received in a double pointer.
want to pass arr[0][2] without storing in any other variable.
#include <iostream>
using namespace std;
int help(int **arr)
{
cout<<**arr;
}
int main()
{
{
int n=3,m=3,k=0;
int **arr = new int*[n];
for(int i = 0; i < n; i++) {
arr[i] = new int[m];
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
arr[i][j]=k;
k++;
}
}
int *g=*arr+2;
int **h=&g;
help(h);
}
}
There is no better way. Unfortunately C++ syntax x[y] can be used to mean two very different operations: if x is an array then is indexing, if x is a pointer they it's indirection and indexing.
If a caller expects a pointer to a pointer and you've a bidimensional matrix there's nothing you can do except actually creating the pointer that is not present in the matrix and pass its address.
The fact that with an array of pointers, with a pointer to a pointer and with a 2d array the syntax to reach an element is x[y][z] is irrelevant... they are three very different operations.
Why not just write
int *p = &arr[0][2];
help( &p );
If you want to get an access to the whole array using a pointer of the type int ** then you can use the following approach.
#include <iostream>
void help(int **arr, size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
std::cout << ( *arr )[i] << ' ';
}
std::cout << '\n';
}
int main()
{
int arr[2][3]={{1,2,3},{4,5,6}};
int *p = reinterpret_cast<int *>( arr );
help( &p, 6 );
return 0;
}
The program output is
1 2 3 4 5 6
I made a function to initialize a dynamic 3d array
void initialize_array_3d(char ***Array, int size1, int size2, int size3)
{
Array = new char**[size1];
for (int t=0; t<size1+1; t++)
{
Array[t] = new char *[size2];
for (int r=0; r<size2+1; r++)
{
Array[t][r] = new char [size3];
}
}
}
And it works fine but when I try to delete it with a function
void deinitialize_array_3d(char ***Array, int size1, int size2)
{
for (int t=0; t<size1+1; t++)
{
for (int r=0; r<size2+1; r++)
{
delete[] Array[t][r];
}
delete[] Array[t];
}
delete[] Array;
}
My program crashes, why? and how do i fix it?
In main:
int main()
{
char ***Failed;
initialize_array_3d(Failed, 5, 4, 2);
deinitialize_array_3d(Failed, 5, 4);
Failed = nullptr;
return 0;
}
For starters function initialize_array_3d is wrong. It should look at least the following way
void initialize_array_3d( char ****Array, int size1, int size2, int size3 )
^^^^^^^^^^^^^^
{
*Array = new char**[size1];
for ( int t = 0; t < size1; t++ )
^^^^^^^^^
{
( *Array )[t] = new char *[size2];
for ( int r = 0; r < size2; r++ )
^^^^^^^^^
{
( *Array )[t][r] = new char [size3];
}
}
}
That is the first parameter should be passed indirectly using pointer.
A function call might look like
initialize_array_3d( &Failed, 5, 4, 2);
^^^^^^^
Otherwise parameter char ***Array is a local variable of the function and the function deals with a copy of the argument. Any changes of the local variable do not influence on the original argument.
Or you could declare the parameter as reference to the pojnter. For example
void initialize_array_3d( char *** &Array, int size1, int size2, int size3 )
^^^^^^^^^^^^^^
{
Array = new char**[size1];
for ( int t = 0; t < size1; t++ )
^^^^^^^^^
{
Array[t] = new char *[size2];
for ( int r = 0; r < size2; r++ )
^^^^^^^^^
{
Array[t][r] = new char [size3];
}
}
}
In this case the function call might look like
initialize_array_3d( Failed, 5, 4, 2);
^^^^^^
As for function deinitialize_array_3d then it should look like
void deinitialize_array_3d(char ***Array, int size1, int size2)
{
for ( int t = 0; t < size1; t++ )
^^^^^^^^^
{
for ( int r = 0; r < size2; r++ )
^^^^^^^^^
{
delete[] Array[t][r];
}
delete[] Array[t];
}
delete[] Array;
}
You are using size1 + 1 to loop through your array instead of size1, but because the array is of size1, you can't access element size1. In the initialization you're getting away with it (it's undefined behavior), but you can't delete memory you don't own, so your program crashes.
Consider using a std::vector instead.
I've this piece of code which is working by now(at least):
int** constructSparseMatrix(int totalRow, int totalCol, int totalEl) {
int** arr = new int*[totalEl];
//int x = 0;
for(int i = 0; i < totalEl; i++) {
arr[i] = new int[totalCol];
for (int k = 0; k < totalCol; k++) {
if(k == totalCol - 1) {
arr[i][totalCol - 1] = rand () % 101;
} else {
arr[i][k] = rand () % totalRow + 1;
}
}
}
return arr;
}
and I'm trying to access the element inside by using pointer instead of array and here is my try:
int** constructSparseMatrix(int *totalRow, int *totalCol, int totalEl) {
int** arr = new int*[totalEl];
//int x = 0;
for(int i = 0; i < totalEl; i++) {
arr[i] = &totalCol [0];
for (int k = 0; k < *totalCol; k++) {
if(k == *totalCol - 1) {
arr[i][*totalCol - 1] = rand () % 101;
} else {
arr[i][k] = rand () % *totalRow + 1;
}
}
}
return arr;
}
But when I initialize the same way as the working function:
int** arr = constructSparseMatrix(5,3,totalEl);
then I receive this error:
argument of type "int" is incompatible with parameter of type "int *"
First of all, is my conversion from array to pointer version coded properly? If it's correct, how can I initialize it to avoid above error?
First of all it is obvious that this statement in the function
arr[i] = &totalCol [0];
is wrong. It does not make sense.
You declared the function
int** constructSparseMatrix(int *totalRow, int *totalCol, int totalEl);
as having the first and second parameters as pointers but are trying to pass integer literals 5 and 3 to it in the call
int** arr = constructSparseMatrix(5,3,totalEl);
You could write for example
int totalRow = 5;
int totalCol = 3;
//...
int** arr = constructSparseMatrix( &totalRow, &totalCol, totalEl );
But I do not see a sense to declare these parameters like pointers because they are in fact constants within the function that is they are not changed.
And the variable names confuse the reader. For example I would expect that totalRow is used within the function in statement
int** arr = new int*[totalRow];
instead of totalEl
If you want to use pointers within the function then the function can look like
int** constructSparseMatrix( int totalRow, int totalCol, int totalEl )
{
int** arr = new int*[totalEl];
for ( int **p = arr; p < arr + totalEl; ++p )
{
*p = new int[totalCol];
for ( int *q = *p; q < *p + totalCol; ++q )
{
if ( q == *p + totalCol - 1 )
{
*q = rand () % 101;
}
else
{
*q = rand () % totalRow + 1;
}
}
}
return arr;
}
I think you are passing constants 5 and 3 as arguments which causes this error.
you can't pass constant value as it is by reference.Try passing 5 and 3 by storing them in a variable.
I'm pretty new to C++ and I have some problems with getting into all that pointer stuff. Basically I am passing a pointer to a Function, creating an Array at that pointer. Back in the main function I can't access this array.
Here's my code:
#include <iostream>
using namespace std;
void createArray(char** dict, int* arraysize)
{
*arraysize = 26*26*26*26;
delete dict;
dict = 0;
//Initialisiere character array of character
//char **wortliste = 0;
dict = new char*[*arraysize];
for(int i = 0; i < *arraysize; i++)
dict[i] = new char[5];
int ctr = 0;
//Erstelle Einträge (sortiert)
for (char i = 'A'; i <= 'Z'; i++)
{
for (char j = 'A'; j <= 'Z'; j++)
{
for (char k = 'A'; k <= 'Z'; k++)
{
for (char l = 'A'; l <= 'Z'; l++)
{
dict[ctr][0] = i;
dict[ctr][1] = j;
dict[ctr][2] = k;
dict[ctr][3] = l;
dict[ctr][4] = '\0';
ctr++;
}
}
}
}
}
int main(void)
{
char** dict = 0;
int arraysize;
createArray(dict, &arraysize);
cout << dict[0] << endl << dict[arraysize-1] << endl;
return 0;
}
I can't figure out my error thank you very much in advance.
In C++ parameters are pass by value (unless explicitly marked as being reference parameters), so when you pass dict, a pointer (to a pointer to char) to createArray, the dict inside your function is a different object, albeit with the same initial value, as the dict in main. If you want to see changes to dict in main you would have to pass it by reference, or pass the address of it into a function taking a char ***.
E.g.
void createArray(char**& dict, int* arraysize)
or
void createArray(char*** pdict, int* arraysize)
{ // use (*pdict) instead of dict ...
and
// ...
createArray(&dict, &arraysize);
A more "C++" way to achieve what you want would be to have:
void createArray( std::vector<std::string>& dict );
and to simply have createArray resize the vector to the required size. Using standard containers like vector and string also frees you of the obligation to explicity deallocate that memory that you allocate which is currently missing from your code.
There are a couple of mistakes.
To delete an array:
char **array = /* new with whatever */;
/* do your work */
for (i = 0; i < array_size; ++i)
delete[] array[i];
delete[] array;
To new an array:
char **array = new char *[array_size];
for (i = 0; i < array_size; ++i)
array[i] = new char[array_size_2];
When deleteing, to make sure you don't iterate over a not-newed array, check it against NULL:
for (i = 0; i < array_size; ++i)
{
if (array[i] != NULL) /* Or simply if (array[i]) */
delete[] array[i];
array[i] = NULL;
}
if (array != NULL)
delete[] array;
array = NULL;
alternatively, since delete makes a check for NULL anyway, you can simplify this to:
if (array != NULL)
for (i = 0; i < array_size; ++i)
delete[] array[i]; /* no need to set to NULL after if going to delete the array */
delete[] array;
array = NULL;
Note: delete deletes a single object while delete[] deletes an array.
I can't imagine what you would do with such data, but you can at least use modern techniques.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<string> create() {
vector<string> result;
for (char i = 'A'; i <= 'Z'; ++i) {
for (char j = 'A'; j <= 'Z'; ++j) {
for (char k = 'A'; k <= 'Z'; ++k) {
for (char l = 'A'; l <= 'Z'; ++l) {
result.push_back(string() + i + j + k + l);
}
}
}
}
return result;
}
int main() {
vector<string> data = create();
cout << data.front() << endl << data.back() << endl;
}
I want to allocate memory and fill it to the pointer, that are one of the function parameter, but I think that I don't get some important thing, help me please.
So, If I do that everything works fine:
void alloc(char **p, int n)
{
*p = new char[n];
}
int main() {
char * ptr = NULL;
int n = 10;
alloc(&ptr, n);
for(int i = 0; i<(n - 1); i++)
ptr[i] = '1';
ptr[n - 1] = '\0';
printf("%s", ptr);
return 0;
}
Now I want to initialize the allocated memory also into the function
void alloc(char **p, int n)
{
*p = new char[n];
for(int i = 0; i<(n - 1); i++)
*p[i] = '1';
*p[n - 1] = '\0';
}
int main() {
char * ptr = NULL;
int n = 10;
alloc(&ptr, n);
printf("%s", ptr);
return 0;
}
Program crashes. I don't get why. Please, can someone explain?
Try (*p)[i] and (*p)[n - 1] instead. The precedence rules cause *p[i] to be evaluated as *(p[i]).
Try this:
((*p)[i]) = '1';
you have a problem with operator's evaluation order.
Probably because this:
*p[i]
is interpreted as if you had written this:
*(p[i])
not as if you had written this, which is what you probably meant:
(*p)[i]
This should do the trick,
void
alloc( char*& p, int n )
{
p = new char[n];
std::fill_n( p, n - 1, '1' );
p[n - 1] = '\0';
}
If you insist on using a pointer as argument, change all of the p
in the function to (*p) (and don't forget to check for a null pointer
and do something reasonable if you're passed one.
All things considered, however, you'd be better off using
std::string—this function wouldn't even be necessary, as you
could write std::string( n, '1' ).
Try this
#include <stdio.h> // for NULL
void alloc(char **p, int n)
{
*p = new char[n];
for(int i = 0; i<(n - 1); i++)
*((*p) + i) = '1';
*((*p) + n - 1) = '\0';
}
int main() {
char * ptr = NULL;
int n = 10;
alloc(&ptr, n);
printf("%s", ptr);
// never forget delete pointers
delete [] ptr;
return 0;
}
Its a precedence of operators issue. Try this in your alloc function:
for(int i = 0; i<(n - 1); i++)
(*p)[i] = '1';
(*p)[n - 1] = '\0';