C 分段故障(堆芯转储)-找不到原因

C 分段故障(堆芯转储)-找不到原因,c,segmentation-fault,C,Segmentation Fault,我知道分段错误是由于试图访问无法访问的内存造成的。我似乎找不到为什么会发生这种情况的问题。任何帮助都会很棒!发生的情况如下: Enter the number of elements: 3 Enter the next number: 11 Enter the next number: 22 Enter the next number: 33 How would like to sum the elements? (1) iteratively (2) recursivel

我知道分段错误是由于试图访问无法访问的内存造成的。我似乎找不到为什么会发生这种情况的问题。任何帮助都会很棒!发生的情况如下:

Enter the number of elements:  3
Enter the next number:  11
Enter the next number:  22
Enter the next number:  33

How would like to sum the elements?
  (1)  iteratively
  (2)  recursively
1
Segmentation fault (core dumped)
下面是我的代码:
main.c

使用Valgrind,我得到以下结果:

==3681== Use of uninitialised value of size 4
==3681==    at 0x8048A57: st_createParam (in /home/admin/Desktop/a3/a3)
==3681==    by 0x8048718: sumIterative (in /home/admin/Desktop/a3/a3)
==3681==    by 0x8048618: main (in /home/admin/Desktop/a3/a3)
==3681== 
==3681== 
==3681== Process terminating with default action of signal 11 (SIGSEGV)
==3681==  Bad permissions for mapped region at address 0x40A2F49
==3681==    at 0x8048A57: st_createParam (in /home/admin/Desktop/a3/a3)
==3681==    by 0x8048718: sumIterative (in /home/admin/Desktop/a3/a3)
==3681==    by 0x8048618: main (in /home/admin/Desktop/a3/a3)
==3681== 
==3681== HEAP SUMMARY:
==3681==     in use at exit: 2,216 bytes in 2 blocks
==3681==   total heap usage: 2 allocs, 0 frees, 2,216 bytes allocated
==3681== 
==3681== LEAK SUMMARY:
==3681==    definitely lost: 2,180 bytes in 1 blocks
==3681==    indirectly lost: 0 bytes in 0 blocks
==3681==      possibly lost: 0 bytes in 0 blocks
==3681==    still reachable: 36 bytes in 1 blocks
==3681==         suppressed: 0 bytes in 0 blocks
==3681== Rerun with --leak-check=full to see details of leaked memory
==3681== 
==3681== For counts of detected and suppressed errors, rerun with: -v
==3681== Use --track-origins=yes to see where uninitialised values come from
==3681== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)

这是一个巨大的代码量总和3个数字。
StackType*stk
是未初始化的指针,因此
void st_init(StackType*stk){
应该是
void st_init(StackType**stk){
StackType*st_init(){
。在调试器中运行程序,或者在valgrind等内存检查器的控制下运行程序。@WeatherVane是的,我知道,这是为学校准备的,所以不幸的是,它必须这样做。编译所有警告和调试信息(
gcc-Wall-Wextra-g
)。然后使用调试器(
gdb
),可能是对
核心
进行验尸
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "stackDefs.h"

void st_init(StackType *stk){
        stk->numFrames = 0;
        stk = calloc(stk->numFrames, sizeof(StackType));
}

void st_dump(StackType *stk)
{
  int i, j, k;

  printf("     -- STACK --\n");

  for (i=0; i<stk->numFrames; ++i) {
    printf("     ---- FRAME #%d:  %s \n", i,
             stk->frames[i].funcName);

    for (j=0; j<stk->frames[i].numParams; j++) {
      printf("     ------ param %d: %4d \n", j, stk->frames[i].params[j]);
    }
  }
  printf("     -- END OF STACK --\n\n");
}

void st_push(StackType *stk, char *fname, int numP, ParamType **paramArr){
        strcpy(stk->frames[stk->numFrames].funcName, fname);
        stk->frames[stk->numFrames].numParams = numP;

        int i = 0;
        for(i = 0; i < numP; i++){
                stk->frames[stk->numFrames].params[i] = (ParamType *) paramArr[i];
        }

        stk->numFrames++;

        st_dump(stk);
}

void st_pop(StackType *stk){
        st_dump(stk);

        stk->numFrames--;

        free(stk->frames->params);

}


void st_createParam(char *name, int *valuePtr, ParamType **newParam) {
        printf("made it 1!");
        ParamType *temp;
        temp = (ParamType*) malloc(sizeof(ParamType));
        strcpy(temp->name, name);
        temp->value = valuePtr;
        *newParam = temp;
        printf("made it 2!");
}
#include <stdio.h>
#include <string.h>
#include "stackDefs.h"

void sumIterative(StackType* stk, int numElements, int *intArray, int *sum){
  ParamType **temp0;
  ParamType **temp1;
  ParamType **temp[2];
  st_createParam("numElements", &numElements, temp0);
  st_createParam("sum", sum, temp1);
  temp[0] = temp0;
  temp[1] = temp1;

  st_push(stk, "sumIterative", 2, *temp);

  int i;
  *sum = 0;

  for (i=0; i<numElements; ++i)
    *sum += intArray[i];


  st_pop(stk);

}


void sumRecursive(StackType* stk, int numElements, int *intArray, int *sum){
  ParamType **temp0;
  ParamType **temp1;
  ParamType *temp[2];
  st_createParam("numElements", &numElements, temp0);
  st_createParam("sum", sum, temp1);
  temp[0] = *temp0;
  temp[1] = *temp1;

  st_push(stk, "sumRecursive", 2, temp);

  if (numElements == 0) {
    *sum = 0;
    st_pop(stk);
    return;
  }

  sumRecursive(stk, numElements-1, intArray, sum);
  *sum += intArray[numElements-1];


  st_pop(stk);
}
#define MAX_FRAMES    32
#define MAX_PARAMS     8
#define MAX_ARR_SIZE  16
#define MAX_STR       32
#define C_OK           0
#define C_NOK         -1

typedef struct {
  char name[MAX_STR];
  int  *value;
} ParamType;

typedef struct {
  char      funcName[MAX_STR];
  int       numParams;
  ParamType *params[MAX_PARAMS];
} FrameType;

typedef struct {
  int numFrames;
  FrameType frames[MAX_FRAMES];
} StackType;

/*  Stack functions  */
void st_init(StackType*);
void st_push(StackType*, char*, int, ParamType**);
void st_pop(StackType*);
void st_dump(StackType*);
void st_createParam(char*, int*, ParamType**);

/*  Loop functions  */
void sumIterative(StackType*, int, int*, int*);
void sumRecursive(StackType*, int, int*, int*);
int getArrayData(int*, int*);
==3681== Use of uninitialised value of size 4
==3681==    at 0x8048A57: st_createParam (in /home/admin/Desktop/a3/a3)
==3681==    by 0x8048718: sumIterative (in /home/admin/Desktop/a3/a3)
==3681==    by 0x8048618: main (in /home/admin/Desktop/a3/a3)
==3681== 
==3681== 
==3681== Process terminating with default action of signal 11 (SIGSEGV)
==3681==  Bad permissions for mapped region at address 0x40A2F49
==3681==    at 0x8048A57: st_createParam (in /home/admin/Desktop/a3/a3)
==3681==    by 0x8048718: sumIterative (in /home/admin/Desktop/a3/a3)
==3681==    by 0x8048618: main (in /home/admin/Desktop/a3/a3)
==3681== 
==3681== HEAP SUMMARY:
==3681==     in use at exit: 2,216 bytes in 2 blocks
==3681==   total heap usage: 2 allocs, 0 frees, 2,216 bytes allocated
==3681== 
==3681== LEAK SUMMARY:
==3681==    definitely lost: 2,180 bytes in 1 blocks
==3681==    indirectly lost: 0 bytes in 0 blocks
==3681==      possibly lost: 0 bytes in 0 blocks
==3681==    still reachable: 36 bytes in 1 blocks
==3681==         suppressed: 0 bytes in 0 blocks
==3681== Rerun with --leak-check=full to see details of leaked memory
==3681== 
==3681== For counts of detected and suppressed errors, rerun with: -v
==3681== Use --track-origins=yes to see where uninitialised values come from
==3681== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)