In main:
#include stdio.h
#include stdlib.h
#include string.h
#include dictionary.h
int main( int argc, char ** argv ){
dictionary_t dictionary = NULL;
dictionary->entries = 1;
return 0;
}
//In header
#ifndef DICTIONARY_H
#define DICTIONARY_H
struct dictionary_s{
char * name;
llist_t content;
int entries;
};
typedef struct dictionary_s* dictionary_t;
#endif
//It compiles but shows Segmentation Fault (Core dump) in the console screen.
I have tried almost everything I can think of and checked several posts, but I've been unable to solve this problem.
In main:
#include stdio.h
#include stdlib.h
#include string.h
#include dictionary.h
int main( int argc, char ** argv ){
//dictionary_t dictionary = NULL;//This was your old line that leads to a null pointer voilation..
dictionary_t dictionary = (dictionary_t *) malloc(sizeof(dictionary_t));
if( NULL == dictionary){
//malloc failed, what do you wanna do now?
printf("Malloc failed\n");
//exit(-1);
while(1){} //just spin forever so you can see the error i suppose?
}
dictionary->entries = 1;
return 0;
}
Here is a malloc example, the stack example is similar but different.
https://en.wikibooks.org/wiki/C_Programming/stdlib.h/malloc
Related
I'm relatively new to C and cannot figure out why this program seg faults.
It could be a stupid error on my behalf but cannot seem to figure it out.
I also know its unusual using the embedding method I am, but this was down for sheer familiarity with Python3 and the ease of use.
#define PY_SSIZE_T_CLEAN
#define PAM_SM_AUTH
#define PAM_SM_ACCOUNT
//#define PAM_SM_SESSION
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <security/pam_appl.h>
#include <security/pam_modules.h>
#include </usr/include/python3.6m/Python.h>
/* expected hook */
/*
PAM_EXTERN int pam_sm_setcred( pam_handle_t *pamh, int flags, int argc, const char **argv ) {
return PAM_SUCCESS;
}
PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv) {
printf("Acct mgmt\n");
return PAM_SUCCESS;
}
*/
/* expected hook, this is where custom stuff happens */
PAM_EXTERN int pam_sm_authenticate( pam_handle_t *pamh, int flags,int argc, const char **argv )
{
chdir("../code/facial"); // this changes it to the correct directory to execute
dlopen("/usr/lib/x86_64-linux-gnu/libpython3.6m.so",RTLD_LAZY | RTLD_GLOBAL);
Py_Initialize(); // Starts python interpreter
PyRun_SimpleString("import os\nimport sys\nsys.path.append(os.getcwd())"); // lets python know where we are
PyObject *mymod, *func1, *ret1;
mymod = PyImport_ImportModule("pam_detect"); // This is the .py
if (mymod != 0){ // check if the file file was loaded
func1 = PyObject_GetAttrString(mymod, "detect"); // hel is the function name in the file you declared earlier
ret1 = PyObject_CallObject(func1, NULL); // Null because the function doesnt take an argument.
if (ret1 == 1){
Py_Finalize();
return PAM_SUCCESS;
}
else{
Py_Finalize();
return PAM_AUTH_ERR;
}
}
else{
//printf("Error: can't find file!\n");
return 1;
}
Py_Finalize();
return 0;
}
You have defined the pointers but haven't assigned them to a memory address.
PyObject *mymod, *func1, *ret1;
This line in your code makes a pointer named mymod which can point to a memory containing PyObject, but you haven't given the memory address to it yet.
I don't know if calling the functions will return pointers correctly or not, So when you try to put anything there, it gives segmentation fault if you are trying to assign a variable to a pointer without a memory address.
I can only say this much without knowing where the fault occurred. try putting printf statement before assigning of all 3 pointers and see.
Here is a sample c code related to stat.h. bits/stat.h that mentioned "Never include <bits/stat.h> directly; use <sys/stat.h> instead.". However struct stat is defined in bits/stat.h, and int __xstat (...) is defined in sys/stat.h. The code won't compile with any one of headers or even both of them. How to make it copiled while only changing the #include ... without changing any one of the functions?
#include <stdio.h>
#include <bits/stat.h>
#include <sys/stat.h>
int stat_1(char *filename, struct stat *stat_buf)
{
return __xstat(1, filename, stat_buf); // extern int __xstat (...) defined in sys/stat.h
}
char * test(const char *filename) {
char *result;
stat stat_buf; // struct stat defined in bits/stat.h
printf("DO something here");
if ( stat_1(filename, &sbuf) == -1 ) {
printf("DO something here");
}
return result;
}
int main() {
const char *fileName = "file.txt";
test(fileName);
return 0;
}
You should be calling stat see https://linux.die.net/man/2/stat. Not __xstat.
Interacting with names that start with __ is almost always a sign you are doing something wrong. They are under the hood implementation things
For stat and its associated struct, you should likely be includeing:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
I have 3 different files: main.c, module.h and module.c
The module.c should "transmit" 2 text messages to the main:
One "info" message
And one "error" message.
Those 2 messages are generated within the module.c
The idea is passing both messages using pointer to struct. Unfortunately I am missing something about pointer because only the first message ("This is info") goes through... The second one gets lost somewhere in between.
/*file:main.c (gcc -o test main.c module.c)*/
#include <stdio.h>
#include <stdlib.h>
#include "module.h"
static struct message *text = NULL;
int main(int argc, char **argv)
{
text = (struct message *) malloc(sizeof(struct message));
text->info_text="toto";
text->error_text="tutu";
text->id = 55;
text = moduleFcn();
printf("message->info_text: %s\n", text->info_text);
printf("message->error_text: %s\n", text->error_text);
printf("message->id: %u\n", text->id);
return 0;
}
And the module
/*module.h*/
struct message
{
char *info_text;
char *error_text;
int id;
};
extern struct message* moduleFcn(void);
/*module.c*/
#include <stdio.h>
#include "module.h"
static struct message *module_text = NULL;
struct message* moduleFcn(void)
{
struct message dummy;
module_text = &dummy;
module_text->info_text = "This is info";
module_text->error_text = "This is error";
module_text->id = 4;
return module_text;
}
Thank you in advance for helping me.
Stephane
Make changes in your module code and main functions. Allocate struct on heap in module section and return that structure. In main function why you're allocating a struct and overwriting it with return struct from moduleFcn()?
/*module.h*/
struct message
{
char *info_text;
char *error_text;
int id;
};
extern struct message* moduleFcn(void);
/*module.c*/
#include <stdio.h>
#include "module.h"
struct message* moduleFcn(void)
{
struct message *dummy = (struct message*)malloc(sizeof(struct message));
dummy->info_text = "This is info";
dummy->error_text = "This is error";
dummy->id = 4;
return dummy;
}
In main() do the following changes.
/*file:main.c (gcc -o test main.c module.c)*/
#include <stdio.h>
#include <stdlib.h>
#include "module.h"
int main(int argc, char **argv)
{
struct message *text = moduleFcn();
printf("message->info_text: %s\n", text->info_text);
printf("message->error_text: %s\n", text->error_text);
printf("message->id: %u\n", text->id);
free(text);
return 0;
}
This is part of a program where I call a function that reads components from a ".dat" file and save the input to members of a Struct. When I try calling the function from my main.c it gives various errors depending on what I try. Most notably: conflicting types of 'ReadFile' and too few arguments to function 'ReadFile'. I also get a warning "passing argument from 'ReadFile' makes integer from pointer without cast" and some infos.
This is main.c
#include "MyData.h"
#include "NodalA.h"
#include "FileHandling.h"
#include <stdio.h>
#include "windows.h"
int main(){
ComponentType *CircuitData;
int numComp = 6;
int numEl = 0;
int numNodes = 0;
CircuitData = malloc((numComp)*sizeof(ComponentType));
ReadFile(CircuitData, &numEl, &numNodes);
return 0;
}
This is FileHandling.c:
#include "FileHandling.h"
#include "stdio.h"
void ReadFile(ComponentType *CircuitData, int *numEl, int *numNodes){
numEl = 0;
numNodes = 0;
int index = 0;
FILE *data;
data = fopen("mydata.dat", "r");
if (data == NULL){
printf("Error: \"mydata.dat\" could not be opened");
}
else {
while(!feof(data)){
fscanf(data, "%s, %s, %s, %f", CircuitData[index].name, CircuitData[index].node1, CircuitData[index].node2, CircuitData[index].value);
*CircuitData[index].node1 = extractInteger(CircuitData[index].node1);
*CircuitData[index].node2 = extractInteger(CircuitData[index].node2);
if(*CircuitData[index].node1 > *numNodes)
*numNodes = *CircuitData[index].node1;
if(*CircuitData[index].node2 > *numNodes)
*numNodes = *CircuitData[index].node2;
numEl++;
index++;
}
}
fclose(data);
}
And this is MyData.h
#ifndef MYDATA_H_
#define MYDATA_H_
typedef struct Comp{
char name[5]; //Name of circuit component
char node1[5], node2[5]; //2 nodes
float value[5]; //value
}ComponentType;
#endif /* MYDATA_H_ */
Any help would be appreciated. There are more code but I think this is the most important part.
The ReadFile function name used in the program is the same as a ReadFile function in "windows.h". The error "too few arguments to function 'ReadFile'" is most likely caused by the program trying to call the the function from windows with the wrong arguments. Removing "windows.h" or renaming the function ReadFile to something else solves the problem.
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]);