Return C 函数的值
Return value of a function in C
我尝试编写一些代码来使用以下函数检查表达式中的括号是否为余额。有人能帮我理解为什么下面的函数在平衡表达式的情况下是 returning 1 当它没有在任何地方指定到 return 1.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
struct Stack
{
int top;
unsigned capacity;
char* array;
};
struct Stack* createStack (unsigned capacity)
{
struct Stack* stack = (struct Stack*) malloc (sizeof(struct Stack));
if(!stack)
return NULL;
stack->top = -1;
stack->capacity = capacity;
stack->array = (char*) malloc(stack->capacity * sizeof(int));
if (!stack->array)
return NULL;
return stack;
}
int isEmpty(struct Stack* stack)
{
return (stack->top == -1);
}
void push(struct Stack* stack, char op)
{
stack->top++;
stack->array[stack->top] = op;
}
int pop(struct Stack* stack)
{
if (!isEmpty(stack))
return (stack->array[stack->top--]);
return '$';
}
int isMatchingPair(char char1 , char char2)
{
if (char1 == '(' && char2 == ')')
return 1;
else if (char1 == '[' && char2 == ']')
return 1;
else if (char1 == '{' && char2 == '}')
return 1;
else
return 0;
}
int paranthesesMatch(char* exp)
{
int i;
struct Stack* stack = createStack(strlen(exp));
for(i = 0; exp[i]; i++)
{
if (exp[i] == '(' || exp[i] == '[' || exp[i] == '{')
push(stack , exp[i]);
if (exp[i] == ')' || exp[i] == ']' || exp[i] == '}')
{
if (stack == NULL)
return 0;
else if ( !isMatchingPair(pop(stack), exp[i]) )
return 0;
}
}
}
int main()
{
char exp[100] = "{()}[)";
printf(" %d\n", paranthesesMatch(exp));
if (paranthesesMatch(exp) == 1)
printf("Balanced \n");
else
printf("Not Balanced \n");
return 0;
}
编辑:添加了完整代码。
您的函数 returning 1
是未定义行为的结果。编译器可以自由地做任何它想做的事情,因为并非函数中的所有执行路径都会产生 return 语句。
它 可能 似乎工作的原因是调用者(不知道函数在没有 return 语句的情况下完成),试图访问 returned 值(可能在指定的寄存器中)。并且你的函数在 returning 到调用者之前修改了所述寄存器。
在构建时提高警告级别,将生成有关它的诊断信息 (like so)。您应该考虑将该特定警告升级为错误,因为省略 return 语句会导致难以发现的错误。
啊作为参考记得这个..
从函数末尾流出与没有表达式的 return 相同。在任何一种情况下,return 值都是未定义的。
why the below function is returning 1 in case of a balanced expression when it's not specified anywhere to return 1.
您的函数包含根本 return 没有值的代码分支。具体来说,当循环到达终点时,您的函数没有指定要 return 的内容。在这种情况下,使用函数的 return 值是未定义的行为:无论函数出现在 "return" 什么是垃圾遗留值,它在不同的计算机上可能不同,甚至在您 运行程序多次
就函数本身而言,NULL
检查堆栈可能是不正确的,除非代码在 stack
为空时删除它(这将是一个相当糟糕的决定)。此外,代码有内存泄漏,因为 stack
永远不会被释放。它还将 stack->array
内容过度分配 sizeof(int)
倍(如今在许多计算机上为 4 倍):
stack->array = (char*) malloc(stack->capacity * sizeof(int));
应该是
stack->array = malloc(stack->capacity);
因为你堆了 char
,而不是堆 int
。
终于不用动态分配stack
了。分配一个 char
数组,并创建一个 "stack pointer" 或堆栈索引以指向堆栈的开头。当您 push/pop 元素进入堆栈时递增和递减 pointer/index 。如果您使用可变长度数组,则不需要清理动态分配的内存。
我尝试编写一些代码来使用以下函数检查表达式中的括号是否为余额。有人能帮我理解为什么下面的函数在平衡表达式的情况下是 returning 1 当它没有在任何地方指定到 return 1.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
struct Stack
{
int top;
unsigned capacity;
char* array;
};
struct Stack* createStack (unsigned capacity)
{
struct Stack* stack = (struct Stack*) malloc (sizeof(struct Stack));
if(!stack)
return NULL;
stack->top = -1;
stack->capacity = capacity;
stack->array = (char*) malloc(stack->capacity * sizeof(int));
if (!stack->array)
return NULL;
return stack;
}
int isEmpty(struct Stack* stack)
{
return (stack->top == -1);
}
void push(struct Stack* stack, char op)
{
stack->top++;
stack->array[stack->top] = op;
}
int pop(struct Stack* stack)
{
if (!isEmpty(stack))
return (stack->array[stack->top--]);
return '$';
}
int isMatchingPair(char char1 , char char2)
{
if (char1 == '(' && char2 == ')')
return 1;
else if (char1 == '[' && char2 == ']')
return 1;
else if (char1 == '{' && char2 == '}')
return 1;
else
return 0;
}
int paranthesesMatch(char* exp)
{
int i;
struct Stack* stack = createStack(strlen(exp));
for(i = 0; exp[i]; i++)
{
if (exp[i] == '(' || exp[i] == '[' || exp[i] == '{')
push(stack , exp[i]);
if (exp[i] == ')' || exp[i] == ']' || exp[i] == '}')
{
if (stack == NULL)
return 0;
else if ( !isMatchingPair(pop(stack), exp[i]) )
return 0;
}
}
}
int main()
{
char exp[100] = "{()}[)";
printf(" %d\n", paranthesesMatch(exp));
if (paranthesesMatch(exp) == 1)
printf("Balanced \n");
else
printf("Not Balanced \n");
return 0;
}
编辑:添加了完整代码。
您的函数 returning 1
是未定义行为的结果。编译器可以自由地做任何它想做的事情,因为并非函数中的所有执行路径都会产生 return 语句。
它 可能 似乎工作的原因是调用者(不知道函数在没有 return 语句的情况下完成),试图访问 returned 值(可能在指定的寄存器中)。并且你的函数在 returning 到调用者之前修改了所述寄存器。
在构建时提高警告级别,将生成有关它的诊断信息 (like so)。您应该考虑将该特定警告升级为错误,因为省略 return 语句会导致难以发现的错误。
啊作为参考记得这个.. 从函数末尾流出与没有表达式的 return 相同。在任何一种情况下,return 值都是未定义的。
why the below function is returning 1 in case of a balanced expression when it's not specified anywhere to return 1.
您的函数包含根本 return 没有值的代码分支。具体来说,当循环到达终点时,您的函数没有指定要 return 的内容。在这种情况下,使用函数的 return 值是未定义的行为:无论函数出现在 "return" 什么是垃圾遗留值,它在不同的计算机上可能不同,甚至在您 运行程序多次
就函数本身而言,NULL
检查堆栈可能是不正确的,除非代码在 stack
为空时删除它(这将是一个相当糟糕的决定)。此外,代码有内存泄漏,因为 stack
永远不会被释放。它还将 stack->array
内容过度分配 sizeof(int)
倍(如今在许多计算机上为 4 倍):
stack->array = (char*) malloc(stack->capacity * sizeof(int));
应该是
stack->array = malloc(stack->capacity);
因为你堆了 char
,而不是堆 int
。
终于不用动态分配stack
了。分配一个 char
数组,并创建一个 "stack pointer" 或堆栈索引以指向堆栈的开头。当您 push/pop 元素进入堆栈时递增和递减 pointer/index 。如果您使用可变长度数组,则不需要清理动态分配的内存。