Yes, local variables like that and pointers to them don't remain valid after the function call they exist inside returns. That it works at all is simply a coincidence. Make a few more function calls and the memory pointed to will probably be overwritten with garbage.
The compiler is probably warning you about this mistake, but as a warning, not an error.
You should pass a pointer to that function instead of getting one from it. Don't let it give you its local variables, give yours to it; perfectly valid when done before your function returns.
You could also use a static variable, like:
But this has several caveats. Static variables act like global variables; it will be pointing to the same pointer every time, so you can't keep the pointer around and expect the value to stay the same when other things use it. Also, this isn't thread-safe since multiple threads would be competing for the same variable. Probably best to pass a pointer.
Last edited by Corona688; 07-06-2010 at 07:04 PM..
hello all,
I tried a program on an array to intialise array elements from the standard input device.it is an integer array of 5 elements.but after entering the 4th element it throws a message called "Segmentation Fault" and returns to the command prompt without asking for the 5th element.
... (3 Replies)
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)
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)
Hi ,
During execution a backup binary i get following error
"Program error 11 (Segmentation fault), saving core file in '/usr/datatools"
Riyaz (2 Replies)
Hi,
Why I don't receive a segmentation fault in the following sample.
int main(void)
{
char buff;
sprintf(buff,"Hello world");
printf("%s\n",buff);
}
If I define a buffer of 10 elements and I'm trying to put inside it twelve elements, Should I receive a sigsev... (22 Replies)
Hi,
While comparing primary key data of two tables thr bteq script I am getting this Error. This script is a shell script.
*** Error: The following error was encountered on the output file.
Script.sh: 3043492 Segmentation fault(coredump)
Please let me know how to get through it.
... (5 Replies)
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)
this is a network programming code to run a rock paper scissors in a client and server.
I completed it and it was working without any error. After I added the findWinner function to the server code it starts giving me segmentation fault.
-the segmentation fault is fixed
Current problem -Also... (3 Replies)
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)
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
LEARN ABOUT MINIX
pthread_key_create
PTHREAD_SPECIFIC(3) Library Functions Manual PTHREAD_SPECIFIC(3)NAME
pthread_key_create, pthread_key_delete, pthread_setspecific, pthread_getspecific - management of thread-specific data
SYNOPSIS
#include <pthread.h>
int pthread_key_create(pthread_key_t *key, void (*destr_function) (void *));
int pthread_key_delete(pthread_key_t key);
int pthread_setspecific(pthread_key_t key, const void *pointer);
void * pthread_getspecific(pthread_key_t key);
DESCRIPTION
Programs often need global or static variables that have different values in different threads. Since threads share one memory space, this
cannot be achieved with regular variables. Thread-specific data is the POSIX threads answer to this need.
Each thread possesses a private memory block, the thread-specific data area, or TSD area for short. This area is indexed by TSD keys. The
TSD area associates values of type void * to TSD keys. TSD keys are common to all threads, but the value associated with a given TSD key
can be different in each thread.
For concreteness, the TSD areas can be viewed as arrays of void * pointers, TSD keys as integer indices into these arrays, and the value of
a TSD key as the value of the corresponding array element in the calling thread.
When a thread is created, its TSD area initially associates NULL with all keys.
pthread_key_create allocates a new TSD key. The key is stored in the location pointed to by key. There is a limit of PTHREAD_KEYS_MAX on
the number of keys allocated at a given time. The value initially associated with the returned key is NULL in all currently executing
threads.
The destr_function argument, if not NULL, specifies a destructor function associated with the key. When a thread terminates via
pthread_exit or by cancellation, destr_function is called with arguments the value associated with the key in that thread. The destr_func-
tion is not called if that value is NULL. The order in which destructor functions are called at thread termination time is unspecified.
Before the destructor function is called, the NULL value is associated with the key in the current thread. A destructor function might,
however, re-associate non-NULL values to that key or some other key. To deal with this, if after all the destructors have been called for
all non-NULL values, there are still some non-NULL values with associated destructors, then the process is repeated. The LinuxThreads
implementation stops the process after PTHREAD_DESTRUCTOR_ITERATIONS iterations, even if some non-NULL values with associated descriptors
remain. Other implementations may loop indefinitely.
pthread_key_delete deallocates a TSD key. It does not check whether non-NULL values are associated with that key in the currently executing
threads, nor call the destructor function associated with the key.
pthread_setspecific changes the value associated with key in the calling thread, storing the given pointer instead.
pthread_getspecific returns the value currently associated with key in the calling thread.
RETURN VALUE
pthread_key_create, pthread_key_delete, and pthread_setspecific return 0 on success and a non-zero error code on failure. If successful,
pthread_key_create stores the newly allocated key in the location pointed to by its key argument.
pthread_getspecific returns the value associated with key on success, and NULL on error.
ERRORS
pthread_key_create returns the following error code on error:
EAGAIN PTHREAD_KEYS_MAX keys are already allocated
pthread_key_delete and pthread_setspecific return the following error code on error:
EINVAL key is not a valid, allocated TSD key
pthread_getspecific returns NULL if key is not a valid, allocated TSD key.
AUTHOR
Xavier Leroy <Xavier.Leroy@inria.fr>
SEE ALSO pthread_create(3), pthread_exit(3), pthread_testcancel(3).
EXAMPLE
The following code fragment allocates a thread-specific array of 100 characters, with automatic reclaimation at thread exit:
/* Key for the thread-specific buffer */
static pthread_key_t buffer_key;
/* Once-only initialisation of the key */
static pthread_once_t buffer_key_once = PTHREAD_ONCE_INIT;
/* Allocate the thread-specific buffer */
void buffer_alloc(void)
{
pthread_once(&buffer_key_once, buffer_key_alloc);
pthread_setspecific(buffer_key, malloc(100));
}
/* Return the thread-specific buffer */
char * get_buffer(void)
{
return (char *) pthread_getspecific(buffer_key);
}
/* Allocate the key */
static void buffer_key_alloc()
{
pthread_key_create(&buffer_key, buffer_destroy);
}
/* Free the thread-specific buffer */
static void buffer_destroy(void * buf)
{
free(buf);
}
LinuxThreads PTHREAD_SPECIFIC(3)