分段故障(核心转储) - 找不到原因

时间:2015-11-05 19:45:08

标签: 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)  recursively
1
Segmentation fault (core dumped)

以下是我的代码:
main.c中

#include <stdio.h>
#include <stdlib.h>

#include "stackDefs.h"

int main()
{
  int intArray[MAX_ARR_SIZE];
  int numElm = 0;
  int result = 0;
  int choice, rc;

  rc = getArrayData(intArray, &numElm);
  if (rc < 0)
    exit(1);

  printf("\nHow would like to sum the elements?\n");
  printf("  (1)  iteratively\n");
  printf("  (2)  recursively\n");
  scanf("%d", &choice);

  StackType *stk;
  st_init(stk);

  switch (choice) {
    case 1:
      sumIterative(stk, numElm, intArray, &result);
      printf("Sum is %d\n", result);
      break;
    case 2:
      sumRecursive(stk, numElm, intArray, &result);
      printf("Sum is %d\n", result);
  }
  return 0;
}

int getArrayData(int *arr, int *num)
{
  int currCount = 0;

  printf("\nEnter the number of elements:  ");
  scanf("%d", num);

  if (*num >= MAX_ARR_SIZE) {
    printf("Too many elements\n");
    return C_NOK;
  }

  while (currCount < *num) {
    printf("Enter the next number:  ");
    scanf("%d", arr+currCount);
    ++currCount;
  }
  return C_OK;
}

stack.c

#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!");
}

loop.c中

#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);
}

stackDefs.h

#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*);

使用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)

1 个答案:

答案 0 :(得分:0)

在将st_init传递给main之前,您需要为StackType stk_obj; StackType *stk = &stk_obj; st_init(stk); 分配存储空间。最简单的方法可能只是在&中声明它:

main

或者你可以省去指针,但是如果你采用这种方法,你需要在for (f = 0; f < forum.length; f++) { end = columnArray.length - 1; object = forum[f]; for (property in object) { value = object[property]; if (property === columnArray[end]) { tableRowData = "<td>" + value + "</td></tr>"; } else { tableRowData = "<td>" + value + "</td>"; } tableRowData2 += tableRowData; tableRowData = ""; } finalTableData = "<tr>" + tableRowData2; finalTableData2 += finalTableData; tableRowData2 = ""; } 的任何地方添加for (i = 0; i < forum.length; i++) {}