qq4all.
I have a task - write config parser for syntax like this:
[module]
name = first
imitationType = first
[module]
name = second
imitationType = second
etc.
I found pretty config parser - inih, but I can't force it to work as I want. Here my code, writed over inih example:
typedef struct {
const char* name;
const char* imitation_type;
} module_config;
int module_count = 0;
static int handler(void* user, const char* section, const char* name,
const char* value)
{
module_config* pconfig = (module_config*)user;
pconfig = (module_config *) malloc(module_count*sizeof(module_config));
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
if (strcmp(section, "module") == 0) {
if (MATCH("module", "name")) {
pconfig[module_count]->version = strdup(value);
} else if (MATCH("module", "imitationType")) {
pconfig[module_count]->name = strdup(value);
} else {
return 0; /* unknown section/name, error */
}
++module_count;
pconfig = (module_config *) realloc(pconfig, module_count * sizeof(module_config));
}
return 1;
}
But, when I trying to compile this, I get next error:
Error! Expression for '->' must be 'pointer to struct or union'
for those lines:
pconfig[module_count]->version = strdup(value);
pconfig[module_count]->name = strdup(value);
I'm newbie in programming, and don't understand, why this happens. Please, help :-)
pconfig is a pointer to an instance of the module_config struct.
Using pconfig[module_count] on this pointer is equivalent to dereferencing the pointer (pconfig+module_count), i.e. it is equivalent to *(pconfig+module_count).
Thus, pconfig[module_count] is no longer a pointer. You need to use pconfig[module_count].version or (pconfig+module_count)->version.
Related
I am making my own JSON interpreter, but when adding values to a struct, as soon as I increment pairsSize, the whole thing crashes as a segmentation fault. I tried looking through my code and it turns out, as soon as i reallocate in case KEYPAIR_DELIM with json->pairsSize++ uncommented, the whole thing will crash. Here is my code.
Part of my json.c interpret() function
size_t jsonContentLen = strlen(jsonContent);
char * placeHolder = malloc(1);
unsigned int placeHolderPtr = 0;
bool isArray = false;
for (size_t i = 0; i < jsonContentLen; i++) {
switch(jsonContent[i]) {
case KEYPAIR_DELIM: {
placeHolder[placeHolderPtr] = '\0';
// When `json->pairsSize++` is uncommented, this will immediately result in segmentation fault
json->pairs = realloc(json->pairs, sizeof(struct Pair *) * (json->pairsSize+1));
json->pairs[json->pairsSize] = init_pair();
json->pairs[json->pairsSize]->key = realloc(json->pairs[json->pairsSize]->key, placeHolderPtr+1);
strncpy(json->pairs[json->pairsSize]->key, placeHolder, placeHolderPtr+1);
printf("%s\n", json->pairs[json->pairsSize]->key);
placeHolder[0] = '\0';
placeHolderPtr = 0;
}
case NEXT_PAIR: {
placeHolder[placeHolderPtr] = '\0';
size_t placeHolderLen = strlen(placeHolder);
json->pairs[json->pairsSize]->values = realloc(json->pairs[json->pairsSize]->values, sizeof(void *) * (json->pairs[json->pairsSize]->valuesSize+1));
// OMG THIS LOOKS SO BAD AHHHHHHH
json->pairs[json->pairsSize]->values[json->pairs[json->pairsSize]->valuesSize] = malloc(placeHolderLen+1);
strncpy(json->pairs[json->pairsSize]->values[json->pairs[json->pairsSize]->valuesSize], placeHolder, placeHolderLen+1);
printf("%s\n", json->pairs[json->pairsSize]->values[json->pairs[json->pairsSize]->valuesSize]);
placeHolder[0] = '\0';
placeHolderPtr = 0;
json->pairs[json->pairsSize]->valuesSize++;
if (isArray == false) {
// Causes a segmentation fault for some reason, can't figure it out
//json->pairsSize++;
}
break;
}
}
}
Part of my json.h code
#define DOUBLE_QUOTE '"'
#define KEYPAIR_DELIM ':'
#define OBJECT_OPEN '{'
#define OBJECT_CLOSE '}'
#define ARRAY_OPEN '['
#define ARRAY_CLOSE ']'
#define NEXT_PAIR ','
struct Pair {
char * key;
void ** values;
unsigned int valuesSize;
};
struct JSON {
char * objectName;
struct Pair ** pairs;
unsigned int pairsSize;
struct JSON ** objects;
unsigned int objectsSize;
struct JSON * prev;
};
struct JSON * init_json();
struct Pair * init_pair();
I know the code looks like a mess, and you are probably gonna ask that I should just use a pointer to the memory address in another variable to make the code more readable, but I tried that, and it keeps giving me errors. If you can help with this end of the code as well, that would be greatly appreciated.
I have a function that returns a pointer to pointers of chars (char**). The function takes 2 arguments
int* num_paths: a pointer to an integer to indicate the number of strings to be returned.
int* errno: a pointer to an integer to indicate an error code. This can contain different values, so I cannot simply check if NULL is returned in case of error.
Some example code is written below (with the majority of error checks omitted for simplicity):
char** get_paths(int* num_paths, int* errno) {
char* path1 = NULL;
char* path2 = NULL;
char** paths = NULL;
if(errno == NULL) {
printf("Set errno in case of error, but cannot dereference NULL pointer\n");
goto exit;
}
path1 = calloc(1, strlen("foo") + 1);
path2 = calloc(1, strlen("bar") + 1);
strcpy(path1, "foo");
strcpy(path2, "bar");
*num_paths = 2;
paths = calloc(1, *num_paths*sizeof(char *));
paths[0] = path1;
paths[1] = path2;
*errno = 0;
exit:
return paths;
}
int main(void) {
char** paths = NULL;
int num_paths = 0;
int errno = 0;
paths = get_paths(&num_paths, &errno);
if(errno != 0) {
return -1;
}
for(int i = 0; i < num_paths; i++) {
printf("%s\n", paths[i]);
free(paths[i]);
}
free(paths);
}
The problem I have with this is that I can't set the error code in case a NULL pointer is passed as argument for errno. You could argue that this is a user error, but I would still like to avoid this situation in the first place.
So my question is: can I rewrite my get_paths function such that it returns an integer as error code, but also returns a char** through the function arguments without resorting to char*** like in the following example:
int get_paths_3(char*** paths, int* num_paths) {
char* path1 = NULL;
char* path2 = NULL;
path1 = calloc(1, strlen("foo") + 1);
path2 = calloc(1, strlen("bar") + 1);
strcpy(path1, "foo");
strcpy(path2, "bar");
*num_paths = 2;
*paths = calloc(1, *num_paths*sizeof(char *));
(*paths)[0] = path1;
(*paths)[1] = path2;
return 0;
}
This is pretty much the only case where "three star" pointers are fine to use. It's fairly common practice in API design to reserve the return value for error codes, so this situation isn't uncommon.
There are alternatives, but they are arguably not much better. You could abuse the fact that void* can be converted to/from char** but it isn't much prettier and less type safe:
// not recommended
int get_paths_4 (void** paths, size_t* num_paths)
{
char* path1 = calloc(1, strlen("foo") + 1);
char* path2 = calloc(1, strlen("bar") + 1);
strcpy(path1, "foo");
strcpy(path2, "bar");
*num_paths = 2;
char** path_array;
path_array= calloc(1, *num_paths*sizeof(char *));
path_array[0] = path1;
path_array[1] = path2;
*paths = path_array;
return 0;
}
...
void* vptr;
size_t n;
get_paths_4 (&vptr, &n);
char** paths = vptr;
for(size_t i=0; i<n; i++)
{
puts(paths[i]);
}
A more sound alternative might be wrap all your parameters into a single struct type and pass that one as a pointer.
Unfortunately you cannot mixing return types in C is a terrible mistake and should not be done, you can either:
return the pointer you need and add a error code to a variable
return the error code and add the pointer to a variable
Both are valid strategies and I'd pick the one that matches the rest of your code to have consistency.
I use a script with the SDK Gracenote (GNSDK) for audio recognition of a file (fingerprint).
The script works correctly, I just want to escape the double quotes in a variable.
I found this function:
void str_replace(char *target, const char *needle, const char *replacement)
{
char buffer[1024] = { 0 };
char *insert_point = &buffer[0];
const char *tmp = target;
size_t needle_len = strlen(needle);
size_t repl_len = strlen(replacement);
while (1) {
const char *p = strstr(tmp, needle);
// walked past last occurrence of needle; copy remaining part
if (p == NULL) {
strcpy(insert_point, tmp);
break;
}
// copy part before needle
memcpy(insert_point, tmp, p - tmp);
insert_point += p - tmp;
// copy replacement string
memcpy(insert_point, replacement, repl_len);
insert_point += repl_len;
// adjust pointers, move on
tmp = p + needle_len;
}
// write altered string back to target
strcpy(target, buffer);
}
I want to use here, I have the impression that the variable type is different but I do not know how (gnsdk_cstr_t at the start and char in function str_replace). Here is the part of the code or want to escape the double quotes of "VALUE" :
static void
_display_track_gdo(
gnsdk_gdo_handle_t track_gdo
)
{
gnsdk_error_t error = GNSDK_SUCCESS;
gnsdk_gdo_handle_t title_gdo = GNSDK_NULL;
gnsdk_cstr_t value = GNSDK_NULL;
/* Track Title */
error = gnsdk_manager_gdo_child_get(track_gdo, GNSDK_GDO_CHILD_TITLE_OFFICIAL, 1, &title_gdo);
if (GNSDK_SUCCESS == error)
{
error = gnsdk_manager_gdo_value_get(title_gdo, GNSDK_GDO_VALUE_DISPLAY, 1, &value);
if (GNSDK_SUCCESS == error)
{
char s[1024] = value;
str_replace(s, "\"", "\\\"");
printf("\"%s\": \"%s\"", "track", s);
}
else
{
_display_last_error(__LINE__);
}
gnsdk_manager_gdo_release(title_gdo);
}
else
{
_display_last_error(__LINE__);
}
}
During compilation , the error is:
main.c: In function '_display_track_gdo':
main.c:756:4: error: invalid initializer
char s[1024] = value;
How can I solve my problem? Or use another method to escape the double quotes ?
Thank you in advance.
Cordially.
PS: I do not know much the langage C, so it is possible that I make mistakes in how to code .
value is of type gnsdk_cstr_t, you cannot use it to initialize a char array. Use the API the library provides, likely you can copy or convert it into a char array. But you cannot assign to an array.
Looking the source of the library, in fact, gnsdk_cstr_t is defined as typedef const gnsdk_char_t* gnsdk_cstr_t;, and gnsdk_char_t is defined as typedef char gnsdk_char_t;, this means, gnsdk_cstr_t is the same as null-terminated strings, can just call standard string functions on it:
char s[1024];
strcpy(s, value);
reference:
https://github.com/tingled/jambox/blob/master/rpi_gnsdk/include/gnsdk/gnsdk_defines.h
In my binary search tree I want to create a function that can get all words starting with a prefix and store all words in an array called results
this is my tree
struct BinarySearchTree_t
{
char *mot,*def;
struct BinarySearchTree_t *left;
struct BinarySearchTree_t *right;
};
typedef struct BinarySearchTree_t BinarySearchTree;
my function :
size_t findWordsByPrefix(BinarySearchTree* tree, char* prefix, char*** results)
{
BinarySearchTree *tmp;
tmp=tree;
static int size=0;
if (!tmp)
return 0;
else if (strncmp(tmp->mot,prefix,strlen(prefix))==0)
{
(*results)= realloc(*results,(1+size)*sizeof(*(*results)));
(*(*results+size))= malloc(strlen(tmp->mot)*sizeof(char));
strcpy((*results)[size],tmp->mot);
size++;
return (1 + findWordsByPrefix(tmp->left,prefix, &results) + findWordsByPrefix(tmp->right,prefix, &results));
}
else
return (strncmp(tmp->mot,prefix,strlen(prefix))<0)?findWordsByPrefix(tmp->right,prefix, &results):findWordsByPrefix(tmp->left,prefix, &results) ;
}
This function should return a number of words starting with the given prefix.
my problem is that the program crash when it is run , and I don't how to resize my array results
so every time I found a word I should increase the size of the results array .
and I would know how exacly manipulate the pointer of pointer of pointer given in arg of this function (char ***results) : what exactly means?
If I simply compile your code, I get severe compiler warnings including:
1>binarysearchtree.c(98) : warning C4047: 'function' : 'char ***' differs in levels of indirection from 'char ****'
1>binarysearchtree.c(98) : warning C4024: 'findWordsByPrefix' : different types for formal and actual parameter 3
This alone will cause a crash -- you are calling your own function recursively with the wrong arguments.
Next, I believe you need to allocate one more than the length of the string, to hold a copy of a string:
malloc((strlen(tmp->mot) + 1 )*sizeof(char))
Next, you're passing around an array of strings of variable size -- and storing the size in a static variable. It's impossible to know if this will work, so don't do it.
Instead, if you want to use a dynamic array of strings, I suggest extracting out a struct to hold them, like so:
struct ResultTable_t
{
int size;
char **results;
};
typedef struct ResultTable_t ResultTable;
void InitializeResults(ResultTable *p_table)
{
p_table->size = 0;
p_table->results = NULL;
}
void AddResult(ResultTable *p_table, char *result)
{
if (result == NULL)
return;
p_table->size++;
p_table->results = realloc(p_table->results, p_table->size * sizeof(*p_table->results));
p_table->results[p_table->size-1] = malloc((strlen(result) + 1) * sizeof(**p_table->results));
strcpy(p_table->results[p_table->size-1], result);
}
void FreeResults(ResultTable *p_table)
{
if (p_table->results != NULL)
{
int i;
for (i = 0; i < p_table->size; i++)
{
free(p_table->results[i]);
}
free(p_table->results);
}
p_table->size = 0;
p_table->results = NULL;
}
(As an improvement, you might consider using geometric growth instead of linear growth for your table of results.)
Then your function becomes:
size_t findWordsByPrefix(BinarySearchTree* tree, char* prefix, ResultTable *p_table)
{
if (!tree)
return 0;
else if (strncmp(tree->mot,prefix,strlen(prefix))==0)
{
AddResult(p_table, tree->mot);
return (1 + findWordsByPrefix(tree->left,prefix, p_table) + findWordsByPrefix(tree->right,prefix, p_table));
}
else if (strncmp(tree->mot,prefix,strlen(prefix))<0)
{
return findWordsByPrefix(tree->right,prefix, p_table);
}
else
{
return findWordsByPrefix(tree->left,prefix, p_table);
}
}
And you would use it like:
ResultTable results;
InitializeResults(&results);
// Get some prefix to search for.
char prefix = GetSomePrefix();
int size = findWordsByPrefix(tree, prefix, &results);
// Do something with the results
// Free all memory of the results
FreeResults(&results);
Update
If the ResultTable is distasteful for some reason, you can pass the dynamic array and array sizes in directly:
void AddResult(char ***p_results, int *p_size, char *word)
{
if (word == NULL)
return;
(*p_size)++;
(*p_results) = realloc(*p_results, ((*p_size)+1) * sizeof(**p_results));
(*p_results)[(*p_size)-1] = malloc((strlen(word) + 1) * sizeof(***p_results));
strcpy((*p_results)[(*p_size)-1], word);
}
void FreeResults(char ***p_results, int *p_size)
{
int i;
if (p_results == NULL || *p_results == NULL)
return;
for (i = 0; i < (*p_size); i++)
{
free ((*p_results)[i]);
}
free (*p_results);
*p_results = NULL;
*p_size = 0;
}
size_t findWordsByPrefix(BinarySearchTree* tree, char* prefix, char ***p_results, int *p_size)
{
if (!tree)
return 0;
else if (strncmp(tree->mot,prefix,strlen(prefix))==0)
{
AddResult(p_results, p_size, tree->mot);
return (1 + findWordsByPrefix(tree->left,prefix, p_results, p_size) + findWordsByPrefix(tree->right,prefix, p_results, p_size));
}
else if (strncmp(tree->mot,prefix,strlen(prefix))<0)
{
return findWordsByPrefix(tree->right,prefix, p_results, p_size);
}
else
{
return findWordsByPrefix(tree->left,prefix, p_results, p_size);
}
}
and use like:
char **results = NULL;
int tablesize = 0;
// Get some prefix to search for.
char prefix = GetSomePrefix();
int size = findWordsByPrefix(tree, prefix, &results, &tablesize);
// Do something with the results
// Free all memory of the results
FreeResults(&results, &tablesize);
I'm writing code to compare two input files in standard C, using the Xcode IDE. I keep getting this error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0). I've done some reading on this and believe it to be a memory issue, but no matter what I try I can't seem to fix it (I've also tried making the structures dynamically using malloc and listed that at the bottom of the code). It's strange because it writes all of the data and then spits out that error at the end. The file format is something like this:
start(int)..stop(int) id(+ or -) now some stuff I don't care about for the rest of the line
I've just been testing this on a file with only + id's so the "-" aspect isn't part of the issue. Anyway I'm quite tired and have been staring at this for a few hours, so please forgive me if it doesn't make sense, I will update it after a few hours of sleep.
typedef struct
{
int start;
int stop;
char *strandID;
} location;
int main(int argc, const char * argv[])
{
if (argc != 4)
{
fprintf(stderr,
"Usage is ./a.out windowfile.txt genefile.txt outputFileName");
exit(-1);
}
//const vars
const char *windowInput = argv[1];
const char *geneInput = argv[2];
const char *outputfile = argv[3];
const int windowHeader = 9;
const int geneHeader = 3;
//get size of structures -- I have debugged and these work correctly, returning the size of my structure
const int posWsize = getSize(windowInput, "+", windowHeader);
const int negWsize = getSize(windowInput, "-", windowHeader);
const int posGsize = getSize(geneInput, "+", geneHeader);
const int negGsize = getSize(geneInput, "-", geneHeader);
//declare structs
location posWindow[posWsize];
location negWindow[negWsize];
location posGene[posGsize];
location negGene[negGsize];
//extract data here
getLocations(posWindow, negWindow, windowInput, windowHeader);
return 0;
}
void getLocations(location *posL, location *negL, const char *input,
const int header)
{
FILE *fileptr = NULL;
fileptr = fopen(input, "r"); //open file
if (fileptr == NULL)
{ //check for errors while opening
fprintf(stderr, "Error reading %s\n", input);
exit(-1);
}
char tmpLoc[20];
char tmpID[2];
int eofVar = 0;
int lineCount = 0;
while (lineCount < header)
{ //skip header and get to data
eofVar = fgetc(fileptr);
if (eofVar == '\n')
lineCount++;
}
int pCount = 0;
int nCount = 0;
while (eofVar != EOF)
{
fscanf(fileptr, "%s %s", tmpLoc, tmpID); //scan in first two strings
if (!strcmp(tmpID, "+"))
{ //if + strand
char *locTok = NULL;
locTok = strtok(tmpLoc, ".."); //tok and get values
posL[pCount].start = atoi(locTok);
locTok = strtok(NULL, "..");
posL[pCount].stop = atoi(locTok); //ERROR IS SHOWN HERE
posL[pCount].strandID = tmpID;
printf("start=%d\tstop=%d\tID=%s\tindex=%d\n", posL[pCount].start,
posL[pCount].stop, posL[pCount].strandID, pCount);
pCount++;
}
else if (!strcmp(tmpID, "-"))
{ //if - strand
char *locTok = NULL;
locTok = strtok(tmpLoc, ".."); //tok and get values
negL[nCount].start = atoi(locTok);
locTok = strtok(NULL, "..");
negL[nCount].stop = atoi(locTok);
negL[nCount].strandID = tmpID;
nCount++;
}
while ((eofVar = fgetc(fileptr)) != '\n')
{
if (eofVar == EOF)
break;
}
}
fclose(fileptr);
}
//dynamic way...same issue -- just replace this with the above if statement and use the create location function
if (!strcmp(tmpID, "+"))
{ //if + strand
int locStart;
int locStop;
locStart = atoi(strtok(tmpLoc, ".."));//tok and get values
locStop = atoi(strtok(NULL, ".."));
posL[pCount] = *createlocation(locStart, locStop, tmpID);
pCount++;
}
location *createlocation(int start, int stop, char *strandID)
{
location *tmp = NULL;
tmp = (location *) malloc(sizeof(location) * 1);
tmp->start = start;
tmp->stop = stop;
tmp->strandID = (char *) malloc(sizeof(char) * 2);
strcpy(tmp->strandID, strandID);
return tmp;
}
Check the return value of strtok.
In your code here
locTok = strtok(NULL, "..");
posL[pCount].stop = atoi(locTok); //ERROR IS SHOWN HERE
strtok is returning a NULL pointer and according to documentation,
A null pointer is returned if there are no tokens left to retrieve.
which matches my original guess that because the address code is 0x0 there's a NULL pointer deference somewhere.
Obviously, the following call to atoi is expecting a non-NULL pointer and crashes.
You Can Also Use Exception Breakpoint in Xcode.
An exception breakpoint tells the debugger to pause whenever a problem is encountered anywhere in your program, so you can evaluate your program's state before it crashes.
Go to the Breakpoint Navigation (Cmd+8), then click the + button in the bottom left and choose Add Exception Breakpoint. You can leave it there.
In my case, I was using the wrong block type. For some reason, a developer had marked a block as const id blockName = ^(Type variableName) { /* code */ } but unfortunately the Type mismatched . Because blockNamewas declared as typeid, the compiler could not warn me properly when I passed blockName` as an argument somewhere else, and this error happened at runtime instead.
For example:
const id callback = ^(ARTPaginatedResult<ARTMessage *> * _Nullable paginatedResult, ARTErrorInfo * _Nullable error) { /* code */
[channel setOptions:channelOptions callback:callback];
The block above has 3 parameters, but channel:setOptions:callback: defines 1 argument called callback, which must be a block which takes 1 argument, and is declared as
- (void)setOptions:(ARTRealtimeChannelOptions *_Nullable)options callback:(nullable void (^)(ARTErrorInfo *_Nullable))cb;
for xc
in your main() function, try to remove char*argv[] or both arguments.
You should delete the arguments of you main function. And it will work.