in c programming , adding function with #pragma omp parallel for, cause other variable to be changed - c

hi i have struct which contains a string in c. i malloc memory for string and when i printf its value it is empty as i expect.
after adding new function which uses open mp tag (#pragma omp parallel for) above of a dummy loop, value of my string changes.
how can i stop changing of my string value?
whats the reason of this behavior?
here is my code and output before adding function
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>
#define RESULT_LENGTH 100000
struct SimpleSearch {
char* ids;
};
int main () {
struct SimpleSearch search;
search.ids = malloc((RESULT_LENGTH) * sizeof(char));
printf("search id now is: %s\n",search.ids);
return 0;
}
output is:
search id now is:
then i add new function and complete code will be:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>
#define RESULT_LENGTH 100000
struct SimpleSearch {
char* ids;
};
void x(){
#pragma omp parallel for
for(int i=0; i <(10+1); i++) {
printf("%d \n",i);
}
}
int main () {
struct SimpleSearch search;
search.ids = malloc((RESULT_LENGTH) * sizeof(char));
printf("search id now is: %s\n",search.ids);
return 0;
}
now ouput will be:
search id now is: ���

Related

C: Include a file which includes a file that also needs to be included in the main program

I have a header file (generalfunctions.h):
#ifndef GENERALFUNCTIONS_H
#define GENERALFUNCTIONS_H
//functionsdeclartion for example
int getInt(char* text);
#endif /* GENERALFUNCTIONS_H */
and a C file generalfunctions.c where I include this headerfile (so I can use some of the functions within each other and don't have bother with their order) and code out the functions.
generalfunctions.c:
#include "generalfunctions.h"
#include <stdlib.h>
#include <stdio.h>
//functions implentaion for example
int getInt(char* text){
int i;
printf("%s\n", text);
if(scanf("%d", &i)==EOF){
printf("INT_ERROR\n");
exit(1);
}
while (fgetc(stdin) != '\n');
return i;
}
//...
Now I need some of these functions in a file called project_objects.c that together with project_objects.h defines a couple of structs, unions, variables and functions with these things I need for my project.
project_objects.h:
#ifndef POINT_H
#define POINT_H
typedef struct point{
int x;
int y;
} point;
point create_point(void);
void print_point(point *p);
//...
#endif /* POINT_H */
project_objects.c:
#include <stdlib.h>
#include <stdio.h>
#include "project_objects.h"
#include "generalfunctions.h"
point create_point(void){
point p;
p.x=getInt("Give my a x");
p.y=getInt("Give my a y");
return p;
}
void print_point(point *p){
printf("x: %d\n", p->x);
printf("y: %d\n", p->y);
}
//..
However I also need some of the functions described in generalfunctions.h directly in my main program:
#include "generalfunctions.c"
#include "project_objects.c"
#include <stdlib.h>
#include <stdio.h>
int main(void){
int i=getInt("How many points would you like to create?");
while(i<1){
i=getInt("Cannot create a negative number of points. How many points would you like to create?");
}
point pointarray[i];
for(int j=0; j<i; j++){
pointarray[j]=create_point();
}
for(int k=0; k<i; k++){
printf("Point %d:\n", k+1);
print_point(pointarray+k);
}
return EXIT_SUCCESS;
}
This seems to work. If I just include the h-files than I get the error that getInt() isn't defined when I link. And before when I included the C file for general functions in project_object.c I got errors for duplication. But now the files seem more dependent on each other than I planned. I also don't understand why this works.
Do not include .c-files. Write function protytypes in .h-files and include them.
project_object.h
typedef int faa;
foo.h
include "project_object.h"
faa foo( faa x ); // prototype for function "foo"
foo.c
#include "foo.h"
faa foo( faa x ) // implementation of function "foo"
{
return x + 666;
}
main.c
#include "project_object.h"
#include "foo.h" // include .h-file with prototype of function "foo"
int main( void )
{
faa x;
x = foo(0); // call function "foo"
return 0;
}

srand(time(nul)) not working properly [duplicate]

This question already has answers here:
C program - srand() [duplicate]
(6 answers)
Closed 7 years ago.
I have a function where I used srand(time(null)) and it worked fine, generating different numbers on each iteration. Then, I added a while inside my function, and now it generates the same two numbers during each iteration of the loop. Here is my code:
void readgraph()
{
int i,vi=0,vj=0;
for(i=0; i<nrVertices; i++)
G[i]=NULL;
for(i=0; i<nrVertices; i++)
{
srand(time(NULL));
while(checkInsert(vi,vj)==false) //this is the while that I added
{
vi=rand()%nrVertices;
vj=rand()%nrVertices;
while(vj==vi)
{
vj=rand()%nrVertices;
}
}
printf("%d %d\n",vi,vj);
insert(vi,vj);
insert(vj,vi);
}
}
Any idea about how could I fix this? Thank you.
#milleniumbug my "short compilable code":
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define MAX 10000
#include <sys/time.h>
typedef struct node
{
struct node *next;
int vertex;
} node;
int a[10000][10000];
int nrVertices=100;
int nrEdges=1000;
node *G[10000]; //heads of the linked list
void readgraph()
{
int i,vi=0,vj=0;
for(i=0; i<nrVertices; i++)
G[i]=NULL;
for(i=0; i<nrEdges; i++)
{
while(checkInsert(vi,vj)==false)
{
vi=(rand()+i-2)%nrVertices;
vj=(rand()+i+7)%nrVertices;
while(vj==vi)
{
vj=rand()%nrVertices;
}
}
printf("%d %d\n",vi,vj);
//insert(vi,vj);
//insert(vj,vi);
}
}
int main()
{
srand(time(NULL));
readgraph();
return 0;
}
You should run srand once - otherwise you're just reinitializing the state every time, which given you use time(NULL), the generated number changes only every next second and is completely predictable.

Struct Issue in C, using same Struct Multiple Source Files

I have a 'struct' which I would like to use in multiple sources files. I have declared the struct in the Header File and then have included in the sources files. It would be great if someone can assist me in this problem.
I am posting Header, Source and Error
#ifndef DATABASE_H
#define DATABASE_H
struct dataBase
{
char modelName;
float capacity;
int mileage;
char color;
};
extern struct dataBase Inputs;
#endif /* DATABASE_H */
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "dataBase.h"
struct dataBase Inputs = NULL;
//size_t Inputs_Size = 0;
int main (void)
#include "hw4_asharma_display.h"
#include <stdio.h>
#include "dataBase.h"
void printLow(int size)
{
// Declaring Variables
int i;
for(i=0; i<size; i++)
{
printf("%s %f %d %s\n",
Inputs[i].modelName,
Inputs[i].capacity,
Inputs[i].mileage,
Inputs[i].color);
}
hw4_asharma_display.c:14:23: error: subscripted value is not an array, pointer, or vector
Inputs[i].modelName,
~~~~~~^~
hw4_asharma_display.c:29:23: error: subscripted value is not an array, pointer, or vector
Inputs[i].modelName,
Inputs is not an array, so you can't just use the [i] index notation. You'll have to change its declaration from:
struct dataBase Inputs = NULL;
(btw the NULL part is pointless) to
struct dataBase Inputs[N];
Instead, if you meant to have only one element, keep the declaration:
struct dataBase Inputs;
but remove the [i] part:
printf("%c %f %d %c\n",
Inputs.modelName,
Inputs.capacity,
Inputs.mileage,
Inputs.color);
Also, you will have to fill each element before printing or you will get all zeroes and blanks.

integer in C Get Random value

i want write program in C via bluez API
I have used this site for tutorial :
and this is my code :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
int main(int argc, char **argv)
{
int dev_id = hci_get_route(NULL);
int res_scan=NULL;
int count;
inquiry_info *device_info=NULL;
res_scan = hci_inquiry(dev_id,3,255,NULL,&device_info,IREQ_CACHE_FLUSH);
printf("%i\n",res_scan);
for(count = 0;count < res_scan;count++)
{
char *name;
printf("count Before : %i\n",count);
ba2str(&(device_info+count)->bdaddr,&name);
printf("count After : %i\n",count);
printf("%s\n",&name);
}
}
and out console :
2
count Before : 0
count After : 1111833143
00:17:EB:5D:1B:86
why count value after ba2str(&(device_info+count)->bdaddr,&name); get random value ?
in that source i linked this issue wont occur !?
instead of
char *name;
...
printf("%s\n",&name);
use
char name[248] = { 0 };
...
printf("%s\n",name);
You need allocate memory before pass the variable as reference, and the best option is to do that out of the loop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
int main(int argc, char **argv)
{
int dev_id = hci_get_route(NULL);
int res_scan=NULL;
int count;
char *name = (char *) malloc(248*sizeof(char));
inquiry_info *device_info=NULL;
res_scan = hci_inquiry(dev_id,3,255,NULL,&device_info,IREQ_CACHE_FLUSH);
printf("%i\n",res_scan);
for(count = 0;count < res_scan;count++)
{
printf("count Before : %i\n",count);
ba2str(&(device_info+count)->bdaddr,name);
printf("count After : %i\n",count);
printf("%s\n",name);
}
free(name);
}
doing that your code will be faster because you will allocate memory only one time.

Struct Array Bug

Hi All,
from the above image.
I am able to compile, but the program crashes at runtime.
Please advise me what could be the resolution to solve this?
Thank you
// structArray.h:
#ifndef __STRUCTARRAY_H_
#define __STRUCTARRAY_H_
typedef struct _vector{
int* str;
int maskSize;
// etc...
}__attribute__((__packed__)) _vector_t;
#endif /* _STRUCTARRAY_H_ */
**// do_structArray.c**
#include "structArray.h"
extern struct _vector_t t;
void do_structArray (void) {
int plaintext[2] = {0x05, 0x08};
_vector_t t[] = {
{plaintext, sizeof(plaintext)},
//{},
};
printf("Content: \n%x \n", t[1].str[1]);
}
// main : just calling do_structArray
#include <stdio.h>
#include <stdlib.h>
#include "structArray.h"
extern struct _vector_t t;
int main(int argc, char *argv[]) {
do_structArray();
system("PAUSE");
return 0;
}
You are accessing t[1] but only have one item in t. Try printf("Content: \n%x \n", t[0].str[1]).
Array indices begin from 0 in C. You're accessing an array element past the end of the array. Change the index to 0:
printf("Content: \n%x \n", t[0].str[0]);

Resources