I am trying to use atof() to convert a string to a double (obviously), but the results are not what I expect. Here is the code and debug information about the variable values BEFORE atof():
d = atof (arg);
next.db = d;
*debug info*
arg = 0x0034f7b0 "10.0"
d = 0.0000000000000000
Once the program has stepped through atof(). the results are as follows:
arg = 0x0034f7b0 "ôþSÄ÷4"
d = 0.0000000000000000
As you can see, before the command, the arg variable does hold a valid double. Though, the return value is 0. What confuses me, is why does the value of arg change?
Also, I do have stdlib.h included. As well, arg is declared as:
char *arg;
If it helps at all, the "10.0" was read from a file.
More code:
void read_instructions()
{
char *str;
char *arg;
int n;
char c;
double d = 0;
instruction next = {0};
while (!feof(datafile)) {
// Fetch the next string
// if (push or pop), get the next argument
// create instructiwn and add to instruction array
str = get_next_string();
if (strncmp (str, "P", 1) == 0) {
if (strncmp (str, "PUSH", 4) == 0) {
next.func = pushFunc;
}
else {
next.func = popFunc;
}
arg = get_next_string();
n = arg[0];
if (n > 64 && n < 71)
next.ch = arg[0];
else {
d = atof (arg);
next.db = d;
}
instr[instr_count] = next;
instr_count++;
}
else {
c = str[0];
switch (c) {
case 'A' :
next.func = addFunc;
break;
case 'S' :
next.func = subFunc;
break;
case 'M' :
next.func = multFunc;
break;
case 'D' :
next.func = divFunc;
break;
case 'H' :
next.func = haltFunc;
default :
printf ("Invalid instruction");
}
instr[instr_count] = next;
instr_count++;
}
}
fclose (datafile);
}
This is the given code to open and access the file:
FILE *datafile;
int main(int argc, char *argv[])
{
if (argc != 2) {
printf("error, incorrect number of arguments");
haltFunc(instr[0]);
}
open_file(argv[1]);
read_instructions();
execute_instructions();
return 0;
}
void open_file(char* argv)
{
char buf[1024];
char cwd[512];
getcwd(cwd, sizeof cwd);
sprintf(buf, "%s\\%s", cwd, argv);
if (!(datafile = fopen(buf, "r"))) {
printf("Error: Make sure your file is located here:\n%s", buf);
}
}
char* get_next_string()
{
char str[15];
fscanf(datafile, "%s", &str);
return str;
}
The header file:
#ifndef MAIN_HEADER_H
#define MAIN_HEADER_H
#define INSTR_SIZE 30
typedef struct {
void (*func)(instruction);
union {
double db;
char ch;
};
} instruction;
int main(int, char*);
char* get_next_string();
void open_file(char*);
void read_instructions();
void execute_instructions();
void pushFunc(instruction instr);
void popFunc(instruction instr);
void addFunc(instruction instr);
void subFunc(instruction instr);
void multFunc(instruction instr);
void divFunc(instruction instr);
void haltFunc(instruction instr);
#endif
And this is the test file:
PUSH 10.0
PUSH 4.0
PUSH 7.0
PUSH 5.0
POP D
POP E
POP
PUSH D
ADD
PUSH 5.0
POP B
PUSH 17.0
POP E
PUSH B
PUSH E
SUB
HALT
Your problem is probably caused by the get_next_string() function returning a pointer to a temporary local character array. As soon as the function returns, the stack memory once used by str[] is being overwritten by some other automatic variable. That would explain why arg is being corrupted.
There are several possible fixes.
The caller could allocate memory to hold the string and pass this pointer to the function for it to fill with data.
The callee could allocate memory for the string and return that pointer. That also returns ownership to the caller and responsibility for calling free() when done.
The str[] array could be declared static inside the function. It would no longer be temporary but be aware that every time you call the function the previous string will be overwritten.
Related
I am trying to find a way to store the return value of this function...
int createDirectory() {
int buffer = 21; //variable holds buffer count
int pid = getpid(); //variable calls builtin C function to get process id
char* directory = malloc(buffer * sizeof(char)); //dynamically creates array of chars
char* prefix = "schmcory.rooms." ; //prefix is set to my user id and rooms
//counts characters and stores in buffer
snprintf(directory, buffer, "%s%d", prefix, pid);
//printf("%s\n", directory); //for testing purposes
struct stat st = {0}; //initialize system struct
//if statement checks if directory exists
if(stat(directory, &st) == -1) {
mkdir(directory, 0755); //creates directory
}
free(directory); //free dynamically allocated memory
return *directory;
}
...into this variable char* directory = createDirectory();
I am getting the below error:
cast to pointer from integer of different size
I have tried the following:
char* directory = (void*)createDirectory();
char* directory = (int)createDirectory();
char directory = createDirectory(); //compiles w/o warning but prints nothing
I am trying to print the results in my main function.
printf(directory);
2 issue with the code :
return value of the function is char * not int - char * createDirectory();
you do free before you return the pointer
here is the code working (Linux environment)
//free(directory);
return directory;
}
int main( int argc, const char* argv[] )
{
char* directory = createDirectory();
printf ("main says %s \n", directory);
free(directory);
}
If I understand correctly, you want to return the result of mkdir, not the string containing the path, switch to
int ret = -1;
if (stat(directory, &st) != -1) // Notice `!=` instead of `==`
{
ret = mkdir(directory, 0755);
if (ret == -1)
{
perror("mkdir")
}
}
free(directory);
return ret;
and the caller should do something like:
int dir;
if ((dir = createDirectory()) != -1)
{
... do your stuff with dir
}
else
{
... raise exception
}
EDIT:
It seems that you want to return something like: schmcory.rooms.49111
In this case, the prototype should be
char *createDirectory(void) { // Return a pointer to `char`
instead of
int createDirectory() {
But do not call free or you end up deleting the result:
free(directory); // Remove this line
and
return *directory;
should be
return directory; // Do not dereference, you don't want the first char, you want the whole string
I had this problem for a while. I cannot create a gets(int maxChar) function that allows the user to get a char* using a get_char() function.
My current code:
char* gets(int maxChar) {
char a;
char* b;
int i;
for(i = 0; i<maxChar; i = i + 1){
a = getchar();
if (a != 0x0D) {putchar(a);} else {puts("\r\n");break;}
b[sizeof(b)] = a;
}
if (sizeof(b) > maxChar-1) {puts("\r\n");}
return b;
//if (b[sizeof(b)-1] != '')
}
the get_char() function works perfectly.
Full kernel.c: https://pastebin.com/3bqTbHsv
Change
char* b;
to
char* b = (char *)malloc(maxChar);
and
b[sizeof(b)] = a;
to
b[i] = a;
Also, change
if (sizeof(b) > maxChar-1) {puts("\r\n");}
to
if (i > maxChar-1) {puts("\r\n");}
The changes made are:
You have created a pointer, but have not allocated any memory. Hence the malloc statement.
sizeof(b) will always be 4 for a 32-bit compiler. You need the array index, given by i.
Same as 2.
These are the basic changes you need to make, without any change to your logic.
The following is an educated guess at what you were attempting to do. It includes some explanations, and a calling example:
char* _gets(int maxChar) // change name to avoid conflict with library 'gets'
{
//char a; //addresses concern pointed out in comments.
int a; //variable is used by function that can return EOF (-1)
int i=0; //
char* b = calloc(maxChar + 1, 1);//point pointer to memory (that it owns)
if(b) //test for success before going on
{
for(i = 0; i<maxChar-3; i = i + 1) // -3 to leave room for \r\n\0
{
a = getchar();
if ((a != 0x0D) && (a != EOF))
{
putchar(a);
b[i]=a;// place char into string accumulator
}
else break;// either EOF or <ENTER> occurred
} // no need for else in this implementation as we can handle
// the clean up once the conditions are met by outputting
// user's entries, then preparing your string (array) for return
puts("\r\n");
b[i++]='\r';
b[i++]='\n';
b[i++]='\0';
}
else return NULL;//failed to allocate memory. leave
return b; // return b (now a string)
//if (b[sizeof(b)-1] != '') // not sure what this was for
}
int main(void)
{
char *strArray = _gets(10);//creates memory for strArray
if(strArray) //use only if not NULL
{
printf("The array contains: %s", strArray );
free(strArray );
}
return 0;
}
I've got a string stack implemented and I'm trying to convert rpn to infix using it. This is how the stack should look as the infix function works, for example if I entered 2 3 + 5 - 8 * :
2 //push 2
2,3 //push 3
(2+3) //reach operator, pop 2 and 3, format it, put result back on the stack
(2+3),5 //push 5
((2+3)-5) //reach operator, pop (2+3) and 5, format it, put result back
((2+3)-5),8 //push 8
(((2+3)-5)*8) //reach operator, pop ((2+3)-5) and 8, format, put result back
Sadly, this is not how the program has been working for me, and I think its something technical, not my algorithm.
When entering "2 5 A", it works, giving a result of "(2+5)". Trying "40 50 A", I get "(40+50)". However, when trying "50 100 A" I get "(+)". Longer expressions like "1 2 A 3 S" also gives me just "(-)". I just can't seem to figure out what's causing this. Please feel free to give a jab at it. Here is my code:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
/*options to later implement (-e for evaluate, -c for infix conversion, -g for mock assembly */
#define OP_EVAL "-e"
#define OP_INFX "-c"
#define OP_GENI "-g"
/* begin stack of strings implementation */
struct stack_entry {
char *data;
struct stack_entry *next;
};
struct stack_t
{
struct stack_entry *head;
size_t stackSize;
};
struct stack_t *newStack(void)
{
struct stack_t *stack = malloc(sizeof *stack);
if (stack)
{
stack->head = NULL;
stack->stackSize = 0;
}
return stack;
}
char *copyString(char *str)
{
char *tmp = malloc(strlen(str) + 1);
if (tmp)
strcpy(tmp, str);
return tmp;
}
void push(struct stack_t *theStack, char *value)
{
struct stack_entry *entry = malloc(sizeof *entry);
if (entry)
{
entry->data = copyString(value);
entry->next = theStack->head;
theStack->head = entry;
theStack->stackSize++;
}
else
{
printf("FAILURE\n");
}
}
char *top(struct stack_t *theStack)
{
if (theStack && theStack->head)
return theStack->head->data;
else
return NULL;
}
void pop(struct stack_t *theStack)
{
if (theStack->head != NULL)
{
struct stack_entry *tmp = theStack->head;
theStack->head = theStack->head->next;
free(tmp->data);
free(tmp);
theStack->stackSize--;
}
}
/*end stack of strings implementation */
/*begin argument handling for -c, -e and -g */
enum method
{
EVAL,
INFX,
GENI
};
int
is_option(const char *str)
{
return (strcmp(str, OP_EVAL) == 0) ||
(strcmp(str, OP_INFX) == 0) ||
(strcmp(str, OP_GENI) == 0);
}
enum method
manip_method(const char *arg)
{
if (strcmp(arg, OP_EVAL) == 0)
return EVAL;
else if (strcmp(arg, OP_INFX) == 0)
return INFX;
else if (strcmp(arg, OP_GENI) == 0)
return GENI;
else
return 0;
}
/* end of argument handling code (again, not even being used yet) */
/* gets operator, converts it, if not A, S, X, D, or M returns NULL */
char*
get_oper(char *op){
if(strcmp(op, "A") == 0)
return "+";
if(strcmp(op, "S") == 0)
return "-";
if(strcmp(op, "X") == 0)
return "*";
if(strcmp(op, "D") == 0)
return "\\";
if(strcmp(op, "M") == 0)
return "%";
else
return NULL;
}
/* formats an infix expression */
char*
formats(char *s1, char *s2, char* op){
int length = strlen(s1) + strlen(s2) + strlen(op) + 3;
char *buf = malloc(length);
strcpy(buf, "(");
strcat(buf, s2);
strcat(buf, op);
strcat(buf, s1);
strcat(buf, ")");
return buf;
}
/* 1) reads tokens and puts them on stack
2) when operator is reached, pops two elements off
the stack and formats it, then pushes the result onto the stack
3) after the loop ends, there should only be one element left on the
stack: the final infix expression */
char*
infix(int argc, char *argv[], int x){
int i;
char *format, *result, *s1, *s2, *op;
struct stack_t *stack = newStack();
for(i = x; i < argc; i++){
if(!get_oper(argv[i])){
push(stack, argv[i]);
} else {
s1 = top(stack);
pop(stack);
s2 = top(stack);
pop(stack);
op = get_oper(argv[i]);
format = formats(s1, s2, op);
push(stack, format);
}
}
result = top(stack);
pop(stack);
return result;
}
int
main(int argc, char *argv[])
{
char *result;
enum method method;
int x;
if (argc >= 4){
if (is_option(argv[1])){
if (argc < 5){
printf("not a valid calculatable expression, exiting now...\n");
exit (EXIT_FAILURE);
}
x = 2;
method = manip_method(argv[1]);
}
else {
x = 1;
method = EVAL;
}
} else {
printf("need option and or a valid reverse polish notation expression, exiting now...\n");
exit (EXIT_FAILURE);
}
result = infix(argc, argv, x);
printf("result: %s\n", result);
exit (EXIT_SUCCESS);
}
Edit: Here are some more examples of the programs behavior
mesquite$ rpcalc 1 5 A
result: (1+5)
mesquite$ rpcalc 1 100 S
result: (1-100)
mesquite$ rpcalc 1 2 A 3 D
result: (\)
mesquite$ rpcalc 10 100 M
result: (%)
Bluepixy has it: You free the data associated with the stack when you pop off elements, so this:
s1 = top(stack); // return handle to topmost item
pop(stack); // invalidate that handle
is a recipe for disaster, especially since you malloc in formats, which is likely to give you a handle to recetly freed memory. Your string concatenation might have the same source and destination buffers.
So remove the deallocation of the data, free(tmp->data), from pop and delegate it to the caller:
s1 = top(stack);
pop(stack);
if (s1 == NULL) exit(1); //Stack underflow
s2 = top(stack);
pop(stack);
if (s2 == NULL) exit(1); //Stack underflow
op = get_oper(argv[i]);
format = formats(s1, s2, op);
free(s1);
free(s2);
push(stack, format);
Of course, you must also free the final result:
result = infix(argc, argv, x);
printf("result: %s\n", result);
free(result);
Your current implementation doesn't guard against malformed expressions (e.g. "A 2 3", "1 2 A 3", "12 A 44"). These occur with stack underflow (which I've tried to repeir very crudely above) or when there is more than one item on the stack after procesing the RN expression.
You have mentioned a BST in a comment, although I think you mean an AST. It is probably a good idea to create a syntax tree from the RPN expression first. With that representation it is easy to generate infix notation, evaluate the expression and generate assembly.
That shouldn't be too difficult, because your formats function already creates a tree-like structure - left node, operator, right node - where the child nodes or strings max have further subexpressions.
I'm new to C / pointers / memory management and am having trouble implementing a few functions for a project I'm working on.
In my builtins.c file, I have a function called printalias that is called to print all the alias names and corresponding values stored in my program. At the end, I want to print one of the alias names by retrieving it via another function called getal.
int x_printalias(int nargs, char *args[]) {
int i = 0;
// Loop through, print names and values
for(i = 0; i< 100; i++)
{
if(alias_names[i][0]!='\0' && !alias_disabled[i])
{
char * var = alias_names[i];
char * val = alias_vals[i];
fprintf(stderr,"%s = %s\n", var, val );
}
}
// This is where I want to retrieve the string from another function
char * hello = "brett";
hello = getal(hello);
fprintf(stderr,"Got alias for brett --> %s",hello);
return 0;
}
My getal function exists in my shellParser.c file and looks like this, generally performing the same looping and returning when it is found:
const char * getal(int nargs, char *args[])
{
fprintf(stderr,"\nRetrieving alias...\n");
int i = 0;
fprintf(stderr, "check1\n" );
fprintf(stderr,"Got args[0]: %s\n", args[0]);
while (alias_names[i][0]!='\0' && i < MAX_ALIAS_LENGTH ) // Find empty slot in variables array
{
fprintf(stderr, "check2\n" );
fprintf(stderr,"I is currently %i and current varible in slot is %s\n",i,alias_names[i]);
//strncpy(hello, variables[i], MAX_VAR_LENGTH); // Variable at current slot
if(strcmp(alias_names[i], args[0]) == 0) // If we have an entry, need to overwrite it
{
fprintf(stderr,"Found alias %s = %s at spot %i\n",args[0],alias_vals[i], i); // Not at end if here
return alias_vals[i];
}
i++;
}
fprintf(stderr, "check3\n" );
// Elided....
return '\0';
}
In the end of my printalias function, I want to test that this getal function is working by calling it on a hardcoded string "brett". However, when I call my printalias function from the command line, it makes it to the "Check 1" print statement and then simply quits without error or return value.
I think this has something to do with my memory management or incorrect declaration of variables with pointers. Can anybody spot something (or a lot of things) that I'm doing wrong here?
You must declarete list of argument for to call getal and it call with these
list.
And pointer of return values getal must be const char*
//....
// This is where I want to retrieve the string from another function
char * hello[] = {"brett"}; // this list argument for getal function
const char *strGetal;
strGetal = getal(1,hello);
fprintf(stderr,"Got alias for brett --> %s",strGetal);
return 0;
}
Example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** get_all(int argc, char **argv)
{
char *value;
char **values = NULL;
int i;
values = (char**) malloc(sizeof (char) * argc);
if (values == NULL) {
perror("malloc");
return NULL;
}
for (i = 0; i < argc; i++, argv++) {
value = strchr(*argv, ':');
values[i] = (value + 1);
}
return values;
}
int main()
{
char *args[] = {"key:a", "key:b", "key:c"};
char **values;
int i;
values = get_all(3, args);
for (i = 0; i < 3; i++) {
puts(values[i]);
}
return 0;
}
In my main, I define a pointer to an array of filename strings (that are to be determined):
char **commands = NULL;
I then call a function which populates the array based on argc & argv using getopt(). Before I return from the function, I print the list of files in the array and it works fine.
Back in my main, I attempt to print the same list (as confirmation) but I get a segmentation error which I can't figure out how to fix. I've included my code below.
Can someone please help me understand why commands in my main is not able to access the file list.
Cheers,
Nap
Here is the function that builds the list of file names:
int getInput (int argc, char **argv, const char *optionList, int *number, int *showEnds, char **commands, int *numberCommands) {
char c;
opterr = 0; // Turn off automatic error messages.
while ( (c = getopt(argc, argv, optionList)) != -1) {
switch (c) {
case 'n':
*number = TRUE;
break;
case 'E':
*showEnds = TRUE;
break;
case '?':
printf("Invalid switch used \"%c\"\n", optopt);
return EXIT_FAILURE;
break;
default:
printf("Input error occurred");
return EXIT_FAILURE;
}
}
printf("optind = %d,%d\n",optind, argc);
commands = malloc(sizeof(char*) * (argc-1));
if (optind < argc) {
*numberCommands = (argc - optind);
int ndx = 1;
while (optind < argc) {
int arraySize = (ndx+1)*sizeof(char*);
commands = realloc (commands,arraySize);
if (commands == NULL) {
fprintf(stderr,"Realloc unsuccessful");
exit(EXIT_FAILURE);
}
int stringSize = strlen(argv[optind])+1;
commands[ndx] = malloc(stringSize);
if(commands[ndx]==NULL){
fprintf(stderr,"Malloc unsuccessful");
exit(EXIT_FAILURE);
}
strcpy(commands[ndx],argv[optind]);
ndx++;
optind++;
}
printf ("Done With Input\n");
for (int i=1; i<=*numberCommands; i++) {
printf ("%s\n",commands[i]);
}
}
return EXIT_SUCCESS;
}
Here is my main:
int main(int argc, char **argv) {
FILE *myInput;
FILE *myOutput;
int result;
const char availOptions[] = "nE";
// Option flags
int number = FALSE; // (Option n) number all output lines
int showEnds = FALSE; // (Option E) Show Ends
char **commands = NULL;
int numberCommands = 0;
// Set default to stdin/stdout.
myInput = stdin;
myOutput = stdout;
if (argc > 1) {
result = getInput(argc,argv, availOptions, &number, &showEnds, commands, &numberCommands);
if (result != EXIT_SUCCESS) {
exit(EXIT_FAILURE);
}
}
printf ("Number of files = %d\n", numberCommands);
for (int i=1; i<=numberCommands; i++) {
printf ("%s\n",commands[i]);
}
echoInput(myInput, myOutput);
}
You're passing in a copy of the double-pointer
char **commands;
into the function. When you malloc() memory for it, it's not updating the pointer in your main function, only your local copy of the double-pointer. Hence, you're attempting to deference a NULL pointer in main. You could instead pass in a pointer to the double-pointer (i.e. the function would take a triple-pointer), your code would work then.
You would need to dereference the triple-pointer inside the function like so:
*commands = malloc(sizeof(char*) * (argc-1));
Alternatively, make a new double-pointer inside your function, do your work (leaving the code in your function roughly as-is) and before you return, assign the memory back into the pointer passed in from main:
*commands = local_commands;
In this function:
int getInput (/* ... */, char **commands, int *numberCommands) {
/* ... */
commands = malloc(sizeof(char*) * (argc-1));
}
you accept a pointer-to-pointer-to-char parameter by value. Looking at the call site, we can see that value will always be (char**)NULL. The parameter is a local variable inside your function. Even though you change it, and even though the caller happens to have a variable with the same name, you never change the caller's copy.
If you want to change the caller's copy of commands, you need to pass the address of that variable to your function:
int getInput (/* ... */, char ***commands, int *numberCommands) {
/* ... */
*commands = malloc(sizeof(char*) * (argc-1));
}
Now the call site looks like this:
char **commands = NULL;
int numberCommands = 0;
/* ... */
if (argc > 1) {
result = getInput(/*...*/, &commands, &numberCommands);
if (result != EXIT_SUCCESS) {
exit(EXIT_FAILURE);
}
}
note that commands and numberCommands are the two out-parameters - that is, they're the parameters where the function should change your local variable - so you're using & to pass pointers to both of them.