-7

I am getting SIGSEGV error in this code and I am unable to figure out it.So please someone help me.I am unable to understand that if I allocate memory through malloc function to an array ,I have to start it with zero index or 1.

#include<iostream>
#include<stdio.h>
using namespace std;
#include<malloc.h>

long long int gold_coins(long long int[],long long int);

int main()
{
    long long int n,i,d;
    long long int* m;
    cin>>n;
    while(n!=EOF)
    {
        m = (long long int*) malloc(n+1);
        for(i=0;i<=n;i++)
           m[i]=i;
        d=gold_coins(m,n);
        cout<<d<<endl;
        cin>>n;
    }
    return(0);
}

long long int gold_coins(long long int m[],long long int n)
{
    if(n<4)
        return m[n];
    else
    {
        long long int q=gold_coins(m,n/2)+gold_coins(m,n/3)+gold_coins(m,n/4);
        if(q>m[n])
            m[n]=q;
        return(m[n]);
    }
}

Thanks in advance.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user2796705
  • 53
  • 1
  • 4
  • 1
    How much memory do you reserve with malloc? – this Jun 15 '14 at 21:04
  • You didn't provide enough information to replicate the problem. – ikegami Jun 15 '14 at 21:05
  • 5
    You probably meant `malloc(sizeof(long long int)*(n+1))` – Andro Jun 15 '14 at 21:05
  • 1
    You're using C++ I/O, which makes this a C++ question. In C++, you probably shouldn't be using `malloc()` et al, which makes it poor C++ code. (Ultimately, you'll need a `free()` too, to be writing code that could be used as part of a bigger program.) Don't tag a question with both C and C++; it annoys the locals in both tags. – Jonathan Leffler Jun 15 '14 at 21:09

1 Answers1

5

I am getting SIGSEGV error in this code and I am unable to figure out it.

Let us first explain what segmentation fault is.

Segmentation fault

The access violation is a trap (system exception/fault) that results when process tries to access invalid memory address: either it tries to write read-only memory or process is not allowed to address this memory at all, including dereferencing null pointers and addressing non-existent memory address (this can be manipulated using for example mmap). A SIGSEGV signal ( 11) is sent on such occasions to offending process.

On the hardware level segmentation fault is implemented as an action raised by a memory management unit as a part of memory protection feature. This computer hardware unit can be a separate integrated circuit or can be placed on same IC as CPU, as in modern computers, microprocessors.

To find a line in program code that causes SIGSEGV we should look into stack trace / stack window, etc. We can put also a breakpoint before a line that caused this and investigate the program state. If the core has been dumped we can also look at this. There is a grsecurity set of patches for Linux that increases protection against buffer overflows, stack overflows, etc. for web servers.


malloc

Standard function malloc(), which on my implementation is declared as

/* Allocate SIZE bytes of memory.  */
extern void *malloc (size_t __size) __THROW __attribute_malloc__ __wur;

takes as its argument the number of bytes to allocate.

C Standard § 7.20.3.3 The malloc function

Synopsis 1

include stdlib.h

void *malloc(size_t size);

Description

2 The malloc function allocates > space for an object whose size is specified by size and whose value is indeterminate.

Returns

3 The malloc function returns either a null pointer or a pointer to the allocated space.


This means that here

m = (long long int*) malloc(n+1);

you are allocating n+1 bytes, but you need (n+1)*sizeof(long long int) bytes to store n+1 variables of type long long int, therefore you should write:

m = malloc( ( n + 1) * sizeof( long long int));

or better

m = malloc( ( n + 1) * sizeof *m)
   ^
   // in C cast should be omitted ( it is still needed if you want your code
   // to compile with a C++ compiler)
   // sizeof *m can be used as () are needed only for a type names

https://stackoverflow.com/a/605858/1141471

Community
  • 1
  • 1
4pie0
  • 29,204
  • 9
  • 82
  • 118