Sponsored Content
Top Forums Programming Thread parameter in ANSI C makes a segmentation fault Post 302519951 by Corona688 on Thursday 5th of May 2011 12:39:10 PM
Old 05-05-2011
Try fprintf(stderr, "%p\n", v); to see if the pointer value has become corrupted somehow.

If all you're passing is one integer, why allocate memory at all?

void *value=(void *)42;
int ivalue=(int)(value);

If that's not all you're doing, post a more complete portion of your code so we can see what's actually going on.
This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. Programming

Hi! segmentation fault

I have written a program which takes a directory as command line arguments and displays all the dir and files in it. I don't know why I have a problem with the /etc directory.It displays all the directories and files untill it reaches a sub directory called peers which is in /etc/ppp/peers.the... (4 Replies)
Discussion started by: vijlak
4 Replies

2. Programming

segmentation fault

ive written my code in C for implementation of a simple lexical analyser using singly linked list hence am making use of dynamic allocation,but when run in linux it gives a segmentation fault is it cause of the malloc function that ive made use of????any suggestions as to what i could do??? thank... (8 Replies)
Discussion started by: rockgal
8 Replies

3. AIX

Segmentation fault

Hi , During execution a backup binary i get following error "Program error 11 (Segmentation fault), saving core file in '/usr/datatools" Riyaz (2 Replies)
Discussion started by: rshaikh
2 Replies

4. Programming

segmentation fault

If I do this. Assume struct life { char *nolife; } struct life **life; // malloc initialization & everything if(life->nolife == 0) Would I get error at life->nolife if it is equal to 0. wrong accession? (3 Replies)
Discussion started by: joey
3 Replies

5. Programming

segmentation fault.

This code is causing a segmentation fault and I can't figure out why. I'm new to UNIX and I need to learn how to avoid this segmentation fault thing. Thank you so much. Thanks also for the great answers to my last post.:):b: int main() { mysqlpp::Connection conn(false); if... (3 Replies)
Discussion started by: sepoto
3 Replies

6. Programming

Segmentation fault in C

i have this code int already_there(char *client_names, char *username) { int i; for(i = 0; i<NUM; i++) { printf("HERE\n"); if (strcmp(client_names, username)==0) return(1); } return(0); } and i get a segmentation fault, whats wrong here? (7 Replies)
Discussion started by: omega666
7 Replies

7. UNIX for Advanced & Expert Users

segmentation fault with ps

What does this mean and why is this happening? $ ps -ef | grep ocular Segmentation fault (core dumped) $ ps -ef | grep ocular Segmentation fault (core dumped) $ ps aux | grep ocular Segmentation fault (core dumped) $ ps Segmentation fault (core dumped) $ pkill okular $ ps... (1 Reply)
Discussion started by: cokedude
1 Replies

8. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

9. Programming

Segmentation fault

I keep getting this fault on a lot of the codes I write, I'm not exactly sure why so I'd really appreciate it if someone could explain the idea to me. For example this code #include <stdio.h> main() { unsigned long a=0; unsigned long b=0; int z; { printf("Enter two... (2 Replies)
Discussion started by: sizzler786
2 Replies

10. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies
CGLOBALS(3)						     Common Library Functions						       CGLOBALS(3)

NAME
Cglobals - LCG thread-specific global variables interface SYNOPSIS
#include <Cglobals.h> void Cglobals_init( int (*getspec) (int *key, void **addr), int (*setspec) (int *key, void *addr), int (*getTid) (void) ); int Cglobals_get(int *key, void **addr, size_t size); void Cglobals_getTid(int *Tid); int C__serrno(); int C__rfio_errno(); int C__Copterr(); int C__Coptind(); int C__Coptopt(); int C__Coptreset(); char *C__Coptarg(); int C__h_errno(); DESCRIPTION
Cglobals is the interface where are defined all necessary functions that always return a thread-specific value of global variables. Each package of LCG that needs to externalize thread-specific global variables contains in its header, if compiled with threads turned on (e.g. the default), a set of: an extern definition to a function contained in Cglobals a #define macro that replaces all occurences of any global variable that needs to be thread-specific to this Cglobal's function. In order to satisfy packages not compiled with threads turned on, or that do not initialize LCG Thread Interface's Cthread, any such global variable is also explicitly defined in Cglobals. For example, taking the global error variable serrno, Cglobals source code contains: an explicit definition of this variable serrno an explicit definition, with source code, of a function C_serrno() that does only the following: if Cglobals_init was not (successfully) called, return the address of the global variable serrno else return the address of a thread-safe specific memory, instanciated at the first call to this function, that holds the content of the current instance of the thread-specific value of serrno The following description of Cglobals_init function is explaining internals of Cglobals and Cthread. In theory no LCG application need to call Cglobals_init, you can skip if you want the following paragraphs, and concentrate only on the other functions descriptions. Cglobals_init is bundled to work with the LCG Thread Interface's Cthread. That is, any implicit or explicit call to Cthread always makes sure that Cglobals_init is called, with three arguments that are: a getspec function address that, given a static key address, returns the address of a Thread-Specific memory into addr content. This uses an internal structure inside Cthread, allocated on the heap, that is associated bijectively to key address. Cthread always explicitly allocates such internal structure to any key address if it is unknown at the moment of the call to getspec. In such a case it will return a NULL value into addr , and it will be the responsability of Cglobals to allocate memory on the heap and to say to Cthread that this newly allocated memory is the one to associate with key address, using setspec. If the internal structure in Cthread associated bijectively to key yet exists, getspec only returns what it knows about the thread- specific memory associated with it, which is a void * member inside the same internal structure mentionned above. a setspec function address that, given the key address and the addr value, previously instanciated with a getspec call, and possibly allocated on the heap by Cglobals if necessary, will internally explicitly call the Operating System Thread-Specific functions that will put the value of address as something thread-specific, bijectively associated to another member of the internal structure of Cthread, itself bijective to key. a getTid function address that returns an unique integer identifier associated with any thread. Cglobals_get returns in addr content the address of a thread-specific memory, e.g. thread-safe, that is bijectively associated with the address of a *static*, e.g. constant, address key , that is automatically created and filled with zeros if necessary, up to size bytes. If the addr content, at return of Cglobals_get, is not NULL, you can safely fill this memory with any value, provided you does not exceed the size bytes length specified in your previous call to Cglobals_get. Because of applications that are not multi-threaded, the initial value of key has then an importance, that's why it is necessary to always declare it with an initial value of -1. Return code is -1 on error, 0 on success and not the first call for this key , 1 on success and it is the first call for this key. This allows to distinguish when Cglobals_get() initialize the memory with zeros (return code 1) and not (return code 0). Cglobals_getTid uses the third function address, getTid , given as an argument to Cglobals_init, and will return in Tid content the value returned by getTid. C__serrno, C__rfio_errno, C__Copterr, C__Coptind, C__Coptopt, C__Coptreset, C__Coptarg and C__h_errno are all the internal functions that return the address of the thread-specific memory hosting the value of the 'global' variables serrno, rfio_errno, Copterr, Coptind, Coptopt, Coptreset, Coptarg and h_errno, respectively. EXAMPLE
Any application can create its own instance of thread-specific global variable using Cglobals. You need only to use Cglobals_get. Here is how to proceed. /* * The following shows how to define and use a thread-specific * integer, my_var, inside your package */ #include <stdlib.h> #include <stdio.h> #include <Cglobals.h> /* Get Cglobals_get prototype */ static int my_key = -1; /* Our static key, integer, init value -1 */ #define my_var (*C__my_var()) static int my_var_static; /* If Cglobals_get error in order not to crash */ int *C__my_var() { int *var; /* Call Cglobals_get */ Cglobals_get(&my_key, (void **) &var, sizeof(int) ); /* If error, var will be NULL */ if (var == NULL) { fprintf(stderr,"Cglobals_get error0); return(&my_var_static); } return(var); } int main() { fprintf(stdout, "Current my_var value is: %d0, my_var); fprintf(stdout, "Set my_var value to: %d0, 12); my_var = 12; fprintf(stdout, "Current my_var value is: %d0, my_var); return(0); } The following example is the source of the test suite for Cglobals_get(): #include <Cthread_api.h> #include <stdlib.h> #include <stdio.h> #include <Cglobals.h> /* Get Cglobals_get prototype */ #include <serrno.h> static int my_key = -1; /* Our static key, integer, init value -1 */ #define my_var (*C__my_var()) static int my_var_static; /* If Cglobals_get error in order not to crash */ void *doit _PROTO((void *)); int doit_v = 0; #define NTHREAD 100 int *C__my_var() { int *var; /* Call Cglobals_get */ switch (Cglobals_get(&my_key, (void **) &var, sizeof(int) )) { case -1: fprintf(stderr,"[%d] Cglobals_get error0, Cthread_self()); break; case 0: fprintf(stderr,"[%d] Cglobals_get OK0, Cthread_self()); break; case 1: fprintf(stderr,"[%d] Cglobals_get OK and first call0, Cthread_self()); break; default: fprintf(stderr,"[%d] Cglobals_get unknown return code0, Cthread_self()); break; } /* If error, var will be NULL */ if (var == NULL) { fprintf(stderr,"[%d] Cglobals_get error : RETURN static ADDRESS!!!!!!!!!!!!0, Cthread_self()); return(&my_var_static); } return(var); } int main() { int i; fprintf(stdout, "[%d] ---> Before any Cthread call0, -1); fprintf(stdout, "[%d] Current my_var value is: %d0, -1, my_var); fprintf(stdout, "[%d] Set my_var value to: %d0, -1, 12); my_var = 12; fprintf(stdout, "[%d] Current my_var value is: %d0, -1, my_var); fprintf(stdout, "[%d] Testing consistency0, -1); if (my_var != 12) { fprintf(stdout, "[%d] Cglobals_get worked ok0, -1); exit(1); } sleep(1); for (i = 0; i < NTHREAD; i++) { Cthread_create(&doit, &doit_v); doit_v++; } fprintf(stdout, "[%d] ---> After all Cthread_create calls0, -1); fprintf(stdout, "[%d] Current my_var value is: %d0, -1, my_var); fprintf(stdout, "[%d] Set my_var value to: %d0, -1, NTHREAD * 10000 + 12); my_var = NTHREAD * 10000 + 12; fprintf(stdout, "[%d] Current my_var value is: %d0, -1, my_var); fprintf(stdout, "[%d] Testing consistency0, -1); if (my_var != (NTHREAD * 10000 + 12)) { fprintf(stdout, "[%d] Cglobals_get worked ok0, -1); exit(1); } sleep(1); exit(0); } void *doit(arg) void *arg; { int Tid; int doit = * (int *) arg; Cglobals_getTid(&Tid); my_var = (Tid + 1) * 100 + 12; fprintf(stdout, "[%d] my_var value is: %d (should be %d)0, Cthread_self(), my_var, (Tid + 1) * 100 + 12); fprintf(stdout, "[%d] second call -- my_var value is: %d (should be %d)0, Cthread_self(), my_var, (Tid + 1) * 100 + 12); fprintf(stdout, "[%d] Testing consistency0, Cthread_self()); if (my_var != ((Tid + 1) * 100 + 12)) { fprintf(stdout, "[%d] !!!!!!!!! ERROR !!!!!!!!!0, Cthread_self()); exit(1); } else { fprintf(stdout, "[%d] Cglobals_get worked ok0, Cthread_self()); } return(0); } SEE ALSO
Cthread(3), serrno(3), Cgetopt(3) AUTHOR
LCG Grid Deployment Team LCG
$Date: 2010-04-05 09:51:26 +0200 (Mon, 05 Apr 2010) $ CGLOBALS(3)
All times are GMT -4. The time now is 05:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy