segmentation fault calloc in C - c

I am making a C program that takes the average time of a calloc, malloc, and alloca process. I got everything to compile, but when I run it I get a segmentation fault. The first thing it runs is calloc so I am going to assume the problem starts there.
Here is my calloc function, the malloc and alloca are basically the same so I figure there is no reason to post them yet.
double calloctest(int objectsize, int numberobjects, int numberoftests)
{
double average = 0;
for (int i = 0; i < numberoftests; i++)
{
clock_t begin = clock();
int *objectsize = calloc(numberobjects, sizeof(char) * *objectsize);
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
average = average + time_spent;
printf("%f", time_spent);
free(objectsize);
}
double totalAverage;
totalAverage = average / numberoftests;
return totalAverage;
}

You have a local variable objectsize that shadows the function argument with the same name and you dereference it before storing the return value of calloc():
int *objectsize = calloc(numberobjects, sizeof(char) * *objectsize);
You probably meant to write:
int *object = calloc(numberobjects, objectsize);
...
free(object);

Related

program doesen't return nothing if there isn't printf in for

I am trying to generate random arrays in my program.
If in the for i put printf("%d", i); my programs run and take his time to printf all of the values, and then print "end", but if i comment the printf in the for, when i execute the program after 1-2 sec it end without giving any type of result back (no error, no printf).
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
//Costanti
static double E = 0.001; // Errore relativo massimo ammissibile
static int nMin = 100; // A = nMin
static int nMax = 5000000;
double B;
double duration(struct timespec start, struct timespec end) {
return end.tv_sec - start.tv_sec
+ ((end.tv_nsec - start.tv_nsec) / (double)1000000000.0);
}
double getResolution() {
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
do {
clock_gettime(CLOCK_MONOTONIC, &end);
} while (duration(start, end) == 0.0);
return duration(start, end);
}
int main() {
// Inizializzazione variabili per il calcolo del tempo
double tMin = getResolution() * ((1 / E) + 1);
B = exp((log(nMax) - log(nMin)) / 99);
srand(time(NULL));
// Generazione Input per l'algoritmo
//struct timespec inizio, fine;
//clock_gettime(CLOCK_MONOTONIC, &inizio);
for (int j = 0; j < 100; j++) {
int n = nMin * pow(B,j);
int array[n];
for (int i = 0; i < n; i++) {
array[i] = rand();
//printf("%d", i);
}
}
//clock_gettime(CLOCK_MONOTONIC, &fine);
//double quantodura = duration(inizio, fine);
//printf("generation time: %f", quantodura);
printf("ciao");
return 0;
}
Even if I comment all of the struct timespec inizio,fine; clock_gettime ecc. it doesen't work
It didn't return nothing. A program always returns an exit code. (It can be obtained using echo $? if using "sh".) I get 139, indicating a segmentation violation on my system. Using -fsanitize=address identifies a stack overflow as the cause.
AddressSanitizer:DEADLYSIGNAL
=================================================================
==1==ERROR: AddressSanitizer: stack-overflow on address 0x7fff91751728 (pc 0x00000040175f bp 0x7fff92031910 sp 0x7fff91751730 T0)
#0 0x40175f in main /app/example.c:46
#1 0x7f5eafe2c0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x240b2)
#2 0x40117d in _start (/app/output.s+0x40117d)
SUMMARY: AddressSanitizer: stack-overflow /app/example.c:46 in main
==1==ABORTING
Line 46 is int array[n];. This line is creating ever bigger arrays. Eventually, the array to create is so large that it can't be accommodated by the stack. (This happened when n was 2,326,588 in my test. B was 1.115,487, and j was 92.) You'll need to allocate such large arrays on the heap (e.g. using malloc) instead of the stack.

segmentation fault when assigning a value to de-referenced double pointer

I have wrote this function to calculate average of values at odd or even indices of a uint32 array:
void average_uint32_t(uint32_t * begin, uint32_t *end,
uint8_t skip, uint32_t **result){
//safety check
if(begin == end || begin > end) {
printf("begin and end pointer does not belong to an array\n");
*result = 0; //set pointer to null
return;
}
uint64_t sum = 0;
uint32_t count = 0;
while(begin <= end) {
sum += *begin;
count++;
printf("count=%d,value=%d,sum=%lu\n", count, *begin, sum);
begin += skip;
}
**result = ((uint32_t)(sum/count)); //segmentation fault here
printf("result=%d\n", **result);
}
I test the function like this:
//BUFF_SIZE = 8
uint32_t buffer[BUFF_SIZE] = {0,1,2,3,4,5,6,7};
uint32_t * average = 0; //memory to get result back
//even indices => 0,2,4,6
average_uint32_t(buffer, buffer + BUFF_SIZE - 1, 2, &average);
printf("average of even elements = %d\n", *average);
//odd indices => 1,3,5,7
average_uint32_t(buffer + 1, buffer + BUFF_SIZE, 2, &average);
printf("average of odd elements = %d\n", *average);
But the program hits a segmentation fault when hitting the assignement of the result to the average (as I commented in the function code). Here is the output:
count=1,value=0,sum=0
count=2,value=2,sum=2
count=3,value=4,sum=6
count=4,value=6,sum=12
Signal: SIGSEGV (Segmentation fault)
What am I doing wrong? I guess a single pointer for the average function can fix the problem? but why the double pointer does not work?
That's because uint32_t *average is not pointing to valid memory area.
Try this:
uint32_t uiAverage = 0;
uint32_t *average = &uiAverage;
More better will be to use double instead of uint_32_t since average can be a floating point number. So if you are planning to use double then you also need to change the line
**result = ((uint32_t)(sum/count));
to
**result = ((double)sum/(double)count));
You define average as a null pointer. It is Undefined Behaviour to dereference a null pointer, and in most Unix flavours, it gives a segmentation violation.
You must either use a single pointer pointing to a real variable, or if you want to use a double pointer, average must point to an existing variable.

Assigning value calculated through pthread

I have a function that calculates an integral like this:
/* Complete this function to perform the trapezoidal rule using pthreads. */
void *compute_using_pthreads(void *inputs)
{
double integral;
int k;
threadParams *args = (threadParams *) inputs;
float a = args->a;
float b = args->b;
int n = args->n;
float h = args->h;
integral = (f(a) + f(b))/2.0;
for (k = 1; k <= n-1; k++) {
integral += f(a+k*h);
}
integral = integral*h;
printf("Solution computed using pthreads = %f \n", integral);
}
It's called within main like this:
int i;
for(i = 0; i < NUM_THREADs; i++) {
trapThread = (threadParams *) malloc(sizeof(threadParams));
trapThread->a = a;
trapThread->b = b;
trapThread->n = n;
trapThread->h = (b - a) / (float) n;
if (pthread_create(&slaveThread[i], NULL, *compute_using_pthreads, (void *) trapThread) != 0) {
printf("Looks like something went wrong..\n");
return -1;
}
}
My problem is, since I am running 4 threads, the results string Solution computed using pthreads = is printed four times.
My question is, how do I, within main, call compute_using_pthreads and save its return data into a double variable?
Add another variable res to the struct threadParams and store the result in it.
integral = integral*h;
args->res = integral;
Now, from main(), you'll be able to read this integral calculated by each thread.
Currently, you have no identifier to the access malloc'ed memory since you are using the same variable trapThread. Instead, use an array or malloc'ed list of pointers so that you'll be able to access it later.
Obviously your main thread will have to wait for the other threads to complete i.e. if it exits then the whole process will die.

efficiency int versus long long assignment

If I need to assign zeros to a chunk of memory. If the architecture is 32bits can assignment of long long (which is 8 bytes on particular architecture) be more efficient then assignment of int (which is 4 bytes), or will it be equal to two int assignments? And will the assignment of int be more efficient then assignment using char for the same chunk of memory since I would need to loop 4 times as many times if I use char versus int
Why not use memset() ?
http://www.elook.org/programming/c/memset.html
(from above site)
Syntax:
#include <string.h>
void *memset( void *buffer, int ch, size_t count );
Description:
The function memset() copies ch into the first count characters of buffer, and returns buffer. memset() is useful for intializing a section of memory to some value. For example, this command:
memset( the_array, '\0', sizeof(the_array) );
is a very efficient way to set all values of the_array to zero.
To your questions, the answers would be yes and yes, if the compiler is smart/optimizes.
Interesting note that on machines that have SSE we can work with 128 bit chunks :) still, and this is just my opinion, always try to emphasize readability balanced with conciseness so yeah ... I tend to use memset, its not always perfect, and may not be the fastest but it tells the person maintaining the code "hey Im initializing or setting this array"
anyway here some test code, if it needs any corrections let me know.
#include <time.h>
#include <xmmintrin.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NUMBER_OF_VALUES 33554432
int main()
{
int *values;
int result = posix_memalign((void *)&values, 16, NUMBER_OF_VALUES * sizeof(int));
if (result)
{
printf("Failed to mem allocate \n");
exit(-1);
}
clock_t start, end;
int *temp = values, total = NUMBER_OF_VALUES;
while (total--)
*temp++ = 0;
start = clock();
memset(values, 0, sizeof(int) * NUMBER_OF_VALUES);
end = clock();
printf("memset time %f\n", ((double) (end - start)) / CLOCKS_PER_SEC);
start = clock();
{
int index = 0, total = NUMBER_OF_VALUES * sizeof(int);
char *temp = (char *)values;
for(; index < total; index++)
temp[index] = 0;
}
end = clock();
printf("char-wise for-loop array indices time %f\n", ((double) (end - start)) / CLOCKS_PER_SEC);
start = clock();
{
int index = 0, *temp = values, total = NUMBER_OF_VALUES;
for (; index < total; index++)
temp[index] = 0;
}
end = clock();
printf("int-wise for-loop array indices time %f\n", ((double) (end - start)) / CLOCKS_PER_SEC);
start = clock();
{
int index = 0, total = NUMBER_OF_VALUES/2;
long long int *temp = (long long int *)values;
for (; index < total; index++)
temp[index] = 0;
}
end = clock();
printf("long-long-int-wise for-loop array indices time %f\n", ((double) (end - start)) / CLOCKS_PER_SEC);
start = clock();
{
int index = 0, total = NUMBER_OF_VALUES/4;
__m128i zero = _mm_setzero_si128();
__m128i *temp = (__m128i *)values;
for (; index < total; index++)
temp[index] = zero;
}
end = clock();
printf("SSE-wise for-loop array indices time %f\n", ((double) (end - start)) / CLOCKS_PER_SEC);
start = clock();
{
char *temp = (char *)values;
int total = NUMBER_OF_VALUES * sizeof(int);
while (total--)
*temp++ = 0;
}
end = clock();
printf("char-wise while-loop pointer arithmetic time %f\n", ((double) (end - start)) / CLOCKS_PER_SEC);
start = clock();
{
int *temp = values, total = NUMBER_OF_VALUES;
while (total--)
*temp++ = 0;
}
end = clock();
printf("int-wise while-loop pointer arithmetic time %f\n", ((double) (end - start)) / CLOCKS_PER_SEC);
start = clock();
{
long long int *temp = (long long int *)values;
int total = NUMBER_OF_VALUES/2;
while (total--)
*temp++ = 0;
}
end = clock();
printf("long-ling-int-wise while-loop pointer arithmetic time %f\n", ((double) (end - start)) / CLOCKS_PER_SEC);
start = clock();
{
__m128i zero = _mm_setzero_si128();
__m128i *temp = (__m128i *)values;
int total = NUMBER_OF_VALUES/4;
while (total--)
*temp++ = zero;
}
end = clock();
printf("SSE-wise while-loop pointer arithmetic time %f\n", ((double) (end - start)) / CLOCKS_PER_SEC);
free(values);
return 0;
}
here are some tests:
$ gcc time.c
$ ./a.out
memset time 0.025350
char-wise for-loop array indices time 0.334508
int-wise for-loop array indices time 0.089259
long-long-int-wise for-loop array indices time 0.046997
SSE-wise for-loop array indices time 0.028812
char-wise while-loop pointer arithmetic time 0.271187
int-wise while-loop pointer arithmetic time 0.072802
long-ling-int-wise while-loop pointer arithmetic time 0.039587
SSE-wise while-loop pointer arithmetic time 0.030788
$ gcc -O2 -Wall time.c
MacBookPro:~ samyvilar$ ./a.out
memset time 0.025129
char-wise for-loop array indices time 0.084930
int-wise for-loop array indices time 0.025263
long-long-int-wise for-loop array indices time 0.028245
SSE-wise for-loop array indices time 0.025909
char-wise while-loop pointer arithmetic time 0.084485
int-wise while-loop pointer arithmetic time 0.025277
long-ling-int-wise while-loop pointer arithmetic time 0.028187
SSE-wise while-loop pointer arithmetic time 0.025823
my info:
$ gcc --version
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ uname -a
Darwin MacBookPro 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun 7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386
memset is quite optimize probably using inline assembly though again this varies from compiler to compiler ...
gcc seems to be optimizing quite aggressively when giving -O2 some of the timings start converging I guess I should take a look at the assembly.
If you are curios just call gcc -S -msse2 -O2 -Wall time.c and the assembly is at time.s
Always avoid additional iterations in higher-level programming languages. Your code will be more efficient if you just iterate once over the int, instead of looping over its bytes.
Assignment optimizations are done on most architectures so they are aligned to the word size which is 4 bytes for 32 bit x86. So assigning memory of the same size doesn't matter (no difference between memset of 1MB worth of longs and 1MB worth of char types).
1. long long(8 bytes) vs two int(4 bytes) - Its better to go for long long. Because performance will be good in assigning one 8byte element rather than two 4 byte element.
2. int (4 bytes) vs four char(1 bytes) - Its better to go for int here.
If you are declaring only one element then you can directly assign zero like below.
long long a;
int b;
....
a = 0; b = 0;
But if you are declaring array of n elements then go for memeset function like below.
long long a[10];
int b[20];
....
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
If you want initalize during declaration itself, then no need of memset.
long long a = 0;
int b = 0;
or
long long a[10] = {0};
int b[20] = {0};

Multithreading in C, get the average of 4 arrays

I'm new to multithreading had my first lesson yesterday. So I've wrote a program to get the average of 4 big arrays , each array is a thread and the main waits for all the threads and gives the average of the 4 arrays. This is possible because each thread gives the average of one array. The array is just a headerfile with a float array.
It compiles but gives me a segmentation error and I don't see why.
#include "gemiddelde.h"
#include <stdlib.h>
#include <stdio.h>
float *gemiddelde(void *arg)
{
float *a;
int i;
a = (float *)arg;
float * som;
for( i = 0; i < 100000; i++)
*som += a[i];
*som = *som / 100000;
return som;
}
int main()
{
pthread_t t1,t2,t3,t4;
float * som1, * som2, * som3, * som4, *result;
pthread_create(&t1,NULL,gemiddelde,a1);
pthread_create(&t2,NULL,gemiddelde,a2);
pthread_create(&t3,NULL,gemiddelde,a3);
pthread_create(&t4,NULL,gemiddelde,a4);
pthread_join(t1,som1);
pthread_join(t2,som2);
pthread_join(t3,som3);
pthread_join(t4,som4);
usleep(1);
*result = *som1 + *som2 + *som3 + *som4;
printf("Gemiddelde is: %f ", *result);
return 0;
}
Can someone help me?
Kind regards,
In
*result = *som1 + *som2 + *som3 + *som4;
result is used unitialized. Make it a plain float instead of a pointer.
From your current code, segfault occurs because som* aren't initialized -- they are dangling pointers.
Your code is very problematic, because the thread code requires memory to store the result, and as it stands your code is plain wrong because it doesn't have any memory and just dereferences a dangling pointer. But even allocating memory inside the thread is not a great idea, because it's not clear who is responsible for it and who will clean it up. So it's much better to allocate all your required memory in the main function. First some boiler plate to set up the thread argument data:
typedef struct thread_arg_type_
{
float * data;
size_t len;
float retval;
} thread_arg_type;
thread_arg_type * create_thread_arg(size_t n)
{
thread_arg_type * result = malloc(sizeof(thread_arg_type));
if (!result) return NULL;
float * const p = malloc(n * sizeof(float));
if (!p)
{
free(result);
return NULL;
}
result->len = n;
result->data = p;
return result;
}
void free_thread_arg(thred_arg_type * r)
{
if (r) free(r->data);
free(r);
}
Now here's how we use it:
int main()
{
thread_arg_type * arg;
pthread_t t;
arg = create_thread_arg(array1_size);
pthread_create(&t, NULL, getmiddle, arg);
// ...
pthread_join(t, NULL);
printf("The result is: %f.\n", arg->retval);
free_thread_arg(arg);
}
And finally we must adapt getmiddle:
void * getmiddle(thread_arg_t * arg)
{
arg->retval = 0;
for(unsigned int i = 0; i != arg->len; ++i)
arg->retval += arg->data[i];
arg->retval /= arg->len;
return NULL;
}

Resources