This question already has answers here:
Does using locals as arguments in pthread_create() work?
(1 answer)
What is the proper way to pass dynamically allocated pointers to pthreads
(1 answer)
pthread_create argument is lost
(2 answers)
Closed 3 months ago.
I want to implement bucket sort with multi-threading. So I create an array and separate it into several subsequence, the subsequence is then passed to the pthread_create() by passing the starting address of each subsequence and its length into threadFunc(). But I always got the values in first few subsequences skipped when I tried to print then out, what is the reason for this? Any help would be appreciated!
Following is part of my code:
struct thread_argv
{
unsigned int * startAddr;
long fragSize;
pthread_t * tid;
};
unsigned int * ptr = intarr;
thread_argv1.fragSize = fragSize;
for (i=0; i<numThread; i++){
thread_argv1.startAddr = ptr;
thread_argv1.tid = tids[i];
pthread_create(tids[i], NULL, threadFunc, (void *) &thread_argv1);
ptr += fragSize;
}
void * threadFunc(void * thread_argv){
struct thread_argv * argv = (struct thread_argv *) thread_argv;
for(int i=0; i<argv->fragSize; i++){
printf("Tid: %ld, Address: %ld, Value: %u\n", argv->tid, argv->startAddr, *argv->startAddr);
argv->startAddr++;
}
}
I have tried test the value in each address (starting addr of each subsequence) before passing into the pthread_create(), and the values are correct. But when I want to print them out in the child thread, the values are as expected (the values of the first two subsequences are skipped).Output
Related
This question already has answers here:
c - passing pointers to a function
(3 answers)
Closed 8 years ago.
I know this is a common problem, however in my case the answer is not that straightforward (or at least I think so).
What I want is to allocate memory for 10 integers (as seen in the scheme below).
Scheme:
_____HEAP_____
| |
**numbers -----> *number ---|--> int |
*number ---|--> int |
*number ---|--> int |
.. | |
*number ---|--> int |
|______________|
Code:
int** numbers;
void malloc_number(int* number){
number = malloc(sizeof(int));
}
int main(){
numbers = malloc(10*sizeof(int*));
int n;
for (n=0; n < 10; n++){
//numbers[n] = malloc(sizeof(int)); // THIS WORKS
malloc_number(numbers[n]); // THIS DOESN'T
free(numbers[n]);
}
free(numbers);
}
I don't seem to understand why this isn't working. In my mind numbers[n] that I pass to malloc_number is a pointer to some unallocated number. Then I use number = malloc(sizeof(int)); (number = numbers[n]) to allocate memory to that pointer.
What am I doing wrong?
void malloc_number(int* number){
number = malloc(sizeof(int));
}
This function leaks memory. Remember C is pass-by-value and number argument is an object with automatic storage duration that got destroyed when malloc_number returns. If you want to modify a pointer through a function you have to pass a pointer to the pointer.
void malloc_number(int* number){
number = malloc(sizeof(int));
}
This code is wrong. You're setting the value of the var "number" which is an argument and is lost after the function call.
The right code would be:
void malloc_number(int** number){
*number = malloc(sizeof(int));
}
...
malloc_number(&numbers[n]);
Please also note that the convention for loop increments to be "i" or "j", n being kind of reserved for the maximum value or limit.
This question already has answers here:
How come an array's address is equal to its value in C?
(6 answers)
Closed 9 years ago.
I tried a code to see what is the difference between &i and i if i is an array. My assumption was that i represents the starting address of the array, and I had no idea what will happen if I print &i as well.
The result was surprising (to me), as both i and &i was the starting address of the array.
#include<stdio.h>
int main()
{
char str[25] = "IndiaBIX";
int i[] = {1,2,3,4};
printf("%i\n", &i);
printf("%i\n", i);
return 0;
}
The result will be:
2686692
2686692
This is the same address.
But if I use a pointer to int:
#include<stdio.h>
#include <stdlib.h>
int main()
{
int *j = (int *) malloc(sizeof(int));
*j = 12;
printf("%i %i %i\n", *j,j,&j);
return 0;
}
The result will be:
12
5582744
2686748
Here I assume the first is the value of the memory area pointed to by j,the second is the address of the memory area pointed to by j, the third is the memory address of the pointer itself. If I print i and &i, why does &i not mean the memory address of the pointer i?
int i[] = {1,2,3,4};
The difference is their type, i has a type of integer array, &i has a type of a pointer of an integer array.
Yeah, both i and &i leads to print the same answer but they are not exactly same,
-> i represents the address of the first element in an array named i.
-> &i represents the address of the whole array(though values of both are same, their types are different)
For more info, please refer this [link]http://publications.gbdirect.co.uk/c_book/chapter5/arrays_and_address_of.html
int ar[10];
ip = ar; /* address of first element */
ip = &ar[0]; /* address of first element */
ar10i = &ar; /* address of whole array */
This question already has answers here:
Why isn't the size of an array parameter the same as within main?
(13 answers)
Array length counting anomaly
(4 answers)
Closed 8 years ago.
I use a macro to get the number of the elements of an integer array, and I could get the right result of the number of the integer array in the main function, but I got the wrong answer if I use a getData function and send the pointer of the integer array as a parameter. I want to know why I got this wrong answer. Thank you!
the code of my program as follow:
#include <stdio.h>
#define LENGTHOFINTARRAY(intArray) ((int)(sizeof(intArray)/sizeof(int)))
int main (int argc, char *argv[])
{
int a[] = {5,8,9,4,11,7,15,25,1};
int getData(int *data);
printf("%d\n", LENGTHOFINTARRAY(a));
getData(a);
return 0;
}
int getData(int *data)
{
int i = 0;
for(i; i < LENGTHOFINTARRAY(data); i++)
{
printf("%d, %d\n", LENGTHOFINTARRAY(data), data[i]);
}
return 1;
}
and the result of my program is:
9
1, 5
I use gcc as my compiler.
The type of int* data is just int* and not int[9] like in main. The size of int* is is the size of any other pointer (4 or 8 bytes usually). There is no way to get the size of an array from the pointer.
And since arrays can't be passed by value in C (unless inside a struct or something) you have to pass the length of the array.
getData() sees data as an int*. It does not know how long it is.
You cannot determine the size of an array that is passed as a pointer to a function.
As you have defined
int a[] = {5,8,9,4,11,7,15,25,1};
int getData(int *data);
printf("%d\n", LENGTHOFINTARRAY(a));
getData(a);
so when you call "getData(a)" then it means you are passing the address of the very first element as &a[0];
so inside you function getData() as
int getData(int *data)
{
int i = 0;
for(i; i < LENGTHOFINTARRAY(data); i++)
{
printf("%d, %d\n", LENGTHOFINTARRAY(data), data[i]);
}
return 1;
}
the data is just a pointer to integer & it gets the pointer or address of the a[0];
so your macro now sees data as pointer to int. which causes the result as u have gotten.
This question already has answers here:
Why isn't the size of an array parameter the same as within main?
(13 answers)
Closed 9 years ago.
When I run the following program, I get different array sizes. I tired different ways but the result is the same, what could io be doing wrong ?
#include<stdio.h>
void array_size(char *a[])
{
printf("Func Array Size: %d\n", sizeof(a));
}
int main()
{
char *str_array[]={"one", "two", "three"};
printf("Array Size: %d\n", (int)sizeof(str_array));
array_size(str_array);
return 0;
}
In function main str_array is an array with three char *.
The parameter a of function array_size is just a pointer. The compiler does not dynamically pass array length information when calling array_size.
The size of one pointer is not equal the size of three char * pointers.
This is because sizeof is a compiler built-in and not a runtime function. It is hard-coded into the binary.
`sizeof((char *)[]) = sizeof(a) = sizeof(void *)`
`sizeof(str_array) = sizeof({"one", "two", "three"}) = 3 * sizeof(char *)`
This question already has answers here:
What is array to pointer decay?
(11 answers)
Closed 9 years ago.
void func(char *s[]){
printf("s: %d\n", sizeof(s));
}
void caller(){
char *a[2];
for(int i = 0; i < 2; i++){
a[i] = (char *)malloc(50 * sizeof(char));
}
strcpy(a[0], "something");
strcpy(a[1], "somethingelse");
printf("a: %d\n", sizeof(a));
func(a);
}
This outputs
a: 16
s: 8
Why is the output of sizeof() different in caller() and func()? Also, is it possible for func to get the number of char * in the array via sizeof?
One is an array of two character pointers. The other is a pointer to an array of pointers. They're not the same size.
In C/C++,Array decays into a pointer.char *s[] is a pointer, the size of pointer is always fixed 4 or 8 (depends upon machine).