Why does this method for reading in a value from a file in C causes a segfault?

A

Anton Nørgaard

Guest
Anton Nørgaard Asks: Why does this method for reading in a value from a file in C causes a segfault?
The background for my problem

I am presently writing a video game in C as a personal project. Due to the increasing number of hard-coded values, it was becoming impractical to store them directly inside the source code. Instead, it was decided that the values should be stored in files and then be dynamically read in, when relevant. Several options were available for this, but all of them were too tedious to set up and overkill for the desired usage. Therefore, I decided to write my own implementation of a file reader that returns values stored in files.

Initial approach to the solution

To make my code as generalizeable as possible, it was decided that the strategy i.e function that reads the file and returns the value, should first of all, return a void pointer, because it can store arbitrary values. The way that you then retrieved a value from a specific file, was that you would provide an argument specifying the desired type of the return value e.g by using an enum. The function would then based on the enum, store the value as the corresponding data type in the void pointer and it would be your responsibility to cast the returned void pointer to the correct type.

Expected file format

The files that the code should read, would be simple text files. The files in question, would consist of lines of variable names with a value assigned to them. Here is an example of how a file for my project would look like.

Code:
name=main_character
money=1000
damage_multiplier=1.2

So for example, the variable name would have the char* value of "main_character", the variable money would have the int value of 1000 and damage_multiplier, the float value of 1.2

Current code

Obviously including the entire code repository of my game won't be useful, so instead I have attempted to the best of my abilities to provide a minimum reproducible example.

We define the enum Return_Type that we pass as an argument to the function in order to retrieve a variable as a specific type

Code:
typedef enum{integer, string, floating,struct_limb,struct_attributes}Return_Type;

So if we want to return a variable as an integer, we pass the value integer to the method

The file i am presently trying to read from is a file that represents a creature struct. Its contents looks like this.

Code:
species=0
name=Short-faced bear
representation=a
id=0
limb_count=7
limbs=[[0,0,100,100],[1,0,100,100],[2,0,100,100],[2,0,100,100],[3,0,100,100],[3,0,100,100],[4,0,100,100]]
weight=900
height=3.4
attributes=[12,12,12,12,12,12,12,12]
max_health=100
max_carry=100
preferred_attack_type=1

The method used to read the file is this function

Code:
void *ir_readin_data(char *file_path, char *variable, Return_Type expected_type){
void *value;
FILE *fp = fopen(file_path, "r");
char * line = NULL;
size_t len = 0;
while((getline(&line, &len, fp)) != -1){
  char *variable_pointer = strstr(line, variable);
  if(variable_pointer != NULL){
    char *value_as_str = strtok(strchr(line, '=')+1, "\n");
   if(expected_type == integer){
    /*Deprecated, but still used for sake of ease, strtol doesn't                                                                         
     work either */
    int value_as_integer = atoi(value_as_str);
    value = &value_as_integer;
    fclose(fp);
    return value;
   }
  }
 }
}

For sake of readability, I have reduced the function to only consider the case when we want to retrieve the variable as an integer.

For the minimum reproducible example, i am trying to read the variable species from the file and i want it to be retrieved as an integer

Code:
*(int *)ir_readin_data(creature_file_path,"species", integer);

I would have gone on to demonstrate all the different approaches I have tried to remedy the problem, but I feel that this question is already dragging on quite a bit. I have tried approaches like passing a void pointer as an argument and store the value in that instead, I have tried to return the value as a char pointer, return the value in a union and so on. None of the approaches work. I feel like there is some subtlety in reading files that I haven't caught up on yet. Can you pinpoint me as to what the error is?

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.
 

Unreplied Threads

Continuous morphism in function fields with extra conditions

joaopa Asks: Continuous morphism in function fields with extra conditions
Let $\Omega$ be the completion of $\overline{\mathbb F_q\left(\left(\frac1T\right)\right)}$ for the topology induced by the valuation $-\deg$ on $\mathbb F_q\left(\left(\frac1T\right)\right)$. Consider $s$ distinct non zero elements $\alpha_1,\cdots,\alpha_s$ of $\overline{\mathbb F_q(T)}$. One assumes that $\deg(\alpha_1)\le\cdots\le\deg(\alpha_s)$. Does there exist a non zero morphism $\sigma$ of $\Omega$ such that $\sigma\left(\overline{\mathbb F_q(T)}\right)\subset\overline{\mathbb F_q(T)}$ and $\deg(\sigma(\alpha_i))<\deg(\sigma(\alpha_{i+1}))$ for any $i\in[1,s-1]$

Be careful for the sign. In the third line, it is $\le$ and the fourth one $<$.

Thanks in advance for any answer.

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.

Modifying a matrix equation

C.Koca Asks: Modifying a matrix equation
I have a matrix equation of the form

$$ Q=SBS+SBA+HSBZ+ABS+ABA+ABZ+ZBS+ZBA+ZBZ. $$

If it weren't for the $H$ matrix in the third term, the whole thing would be

$$ Q^\prime=(S+A+Z)B(S+A+Z). $$

As it is, I was able to reduce it to

$$ Q=(S+A+Z)B(S+A+Z)+(H-I)SBZ, $$ but I want to eliminate the addition, if possible. In other words, I am looking for an expression like

$$ Q=f(S,A,H,Z)Bg(S,A,H,Z), $$ where $f(.)$ and $g(.)$ are matrix functions.

All matrices are square and real. $S$ is symmetric and $H$ is a diagonal, i.e., they commute.

I am looking for some $f(.)$ and $g(.)$. I will be calculating $$ Q^{(n)}=f^n(S,A,H,Z) B g^n(S,A,H,Z), $$ and I will be using SVD to proceed. Thus eliminating the addition helps me a lot.

Thanks!

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.

Invert transform of a generating function

Seth Asks: Invert transform of a generating function
A055887's generating function is given as a transform of another generating function $\frac{1}{1-P(x)}$. It's clear from the definition that this is $\sum_{n=0}^{\infty} \left [ \sum_{i+j+k+...=n} P(i)P(j)P(k)... \right ] x^n$ but I'm struggling to derive this.

I've seen $x$ in $\frac{1}{1-x} = 1+x+x^2+x^3$ be replaced $x$ with $a x$, $x^2$, $x^m$, $-x$ but I only proved these by working backwards. Starting with the result and showing $S - Sx = 1$ then concluding $S = \frac{1}{1-x}$.

This was called "invert transform" in A067687 but that term doesn't seem to be defined or commonly used outside of OEIS.

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.

Shouldn’t vacuous truths be false since they imply false information?

copepod Asks: Shouldn’t vacuous truths be false since they imply false information?
Example: “All the lions I own are healthy”.

In real-world situations, such as court hearings, this sentence will be regarded as a lie if I don’t actually have any lions. Makes sense: what i really say is “I have lions and they’re all healthy”. It’s false, and because it’s synonymous with the original, the original is also false. Logic does agree that the rewording is false, but it doesn’t view it as a synonym with the original, which it treats as a vacuous truth. Why?

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.

Mathematically prove that a Beta prior distribution is conjugate to a Geometric likelihood function

Shivam Dave Asks: Mathematically prove that a Beta prior distribution is conjugate to a Geometric likelihood function
I have to prove with a simple example and a plot how prior beta distribution is conjugate to the geometric likelihood function. I know the basic definition as

'In Bayesian probability theory, a class of distribution of prior distribution $\theta$ is said to be the conjugate to a class of likelihood function $f(x|\theta)$ if the resulting posterior distribution is of the same class as of $f(\theta)$.'

But I don't know how to prove it mathematically.

P.S. - It would really nice of you guys to provide some good material on bayesian statistic and probability theory.

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.

Maximal compact subgroup of $GL_n(\mathbb C_p)$

boxdot Asks: Maximal compact subgroup of $GL_n(\mathbb C_p)$
It is known that the general linear group $GL_n(\mathbb Q_p)$ over the $p$-adic numbers has $GL_n(\mathbb Z_p)$ as a maximal compact subgroup and every other maximal compact subgroup of $GL_n(\mathbb Q_p)$ is conjugated to this one. (Unfortunately, I don't have any reference for this fact and so I don't know any proof.)

Q: Is this also true for $GL_n(\mathbb C_p)$ and $GL_n(\mathcal O)$, where $\mathcal O \subset \mathbb C_p$ is the integer ring?

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.

Value of Product Based on Bitcoins

  • Chuck
  • Finance
  • Replies: 0
Chuck Asks: Value of Product Based on Bitcoins
If I chose to accept Bitcoin as a payment for my product, and the price of the bitcoin goes up or down, then technically I am not getting as much for my product in the long run correct? Because the exchange is based on the dollar as one of the currencies. So If my product cost's 100.00 and that is a certain percentage of a bitcoin then if the bitcoin say its worth 7100.00 that would be .014 bitcoins. But if the bitcoin decreased in price say to 5100.00 then that same product would cost .019 bitcoins. Correct? How dos the fluctuation in price affect the value of my product in dollars?

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.