1

I am trying to dynamically allocate memory for some integers and am getting a segfault. This code runs fine on native MacOS, but fails when I try to run it on my Ubuntu virtual machine? What's the difference?

Code

// Create stuff to add
int* a = malloc(sizeof(int));
int* b = malloc(sizeof(int));
int* c = malloc(sizeof(int));
int* d = malloc(sizeof(int));
*a = 0;
*b = 1;
*c = 2;
*d = 3;

Error

Breakpoint 1, test_add_4_and_check () at test.c:125
125     int* a = malloc(sizeof(int));
(gdb) n
126     int* b = malloc(sizeof(int));
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a8b48a in malloc_consolidate (
    av=av@entry=0x7ffff7dd1b20 <main_arena>) at malloc.c:4175
4175    malloc.c: No such file or directory.
Henry
  • 564
  • 3
  • 22
  • 5
    Provide a [mcve], please. The reason is likely outside the code you've posted. – Fred Larson Apr 13 '18 at 15:33
  • 1
    Can you provide the entire file? Or at least the code that gets called before it – Michael Apr 13 '18 at 15:34
  • usually it is a result of memory corruption somewhere else in your program. you need to run a memory checker, i.e. valgrind. – Serge Apr 13 '18 at 15:37
  • It may be really surprising, but maybe one of your malloc return NULL, and this can explain why you have a SIGSEV,. That's the only possibility that I see. – Tom's Apr 13 '18 at 15:42
  • Do check the return value of `malloc()`. If you still see an error, then you'll have to provide a [mcve]. – machine_1 Apr 13 '18 at 15:45
  • https://stackoverflow.com/questions/9220853/call-to-malloc-failing-in-gdb-session?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Abhishek Keshri Apr 13 '18 at 15:45
  • [mcve] is a must. Both if you want to get your answer or you want to keep your question open. – Eugene Sh. Apr 13 '18 at 15:51
  • 2
    "What's the difference" The difference seems to be that you are lucky on MacOS – Déjà vu Apr 13 '18 at 16:07
  • 1
    After running valgrind, I found that the error was in a totally unrelated realloc call. So, @RingØ was correct, I guess I just was getting lucky. Why could this be? – Henry Apr 13 '18 at 16:45

1 Answers1

1

You didn't verify that malloc didn't fail:

int *a = malloc(sizeof *a);
int *b = malloc(sizeof *b);
int *c = malloc(sizeof *c);
int *d = malloc(sizeof *d);
if (!a || !b || !c || !d) {
  exit(EXIT_FAILURE);
}
*a = 0;
*b = 1;
*c = 2;
*d = 3;

Virtual machine could not have enough ram I suppose.


Complete code:

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

int main(void) {
    int *a = malloc(sizeof *a);
    if (!a) {
        fprintf(stderr, "a allocation fail\n");
        goto a;
    }
    int *b = malloc(sizeof *b);
    if (!b) {
        fprintf(stderr, "b allocation fail\n");
        goto b;
    }
    int *c = malloc(sizeof *c);
    if (!c) {
        fprintf(stderr, "c allocation fail\n");
        goto c;
    }
    int *d = malloc(sizeof *d);
    if (!d) {
        fprintf(stderr, "d allocation fail\n");
        goto d;
    }

    *a = 0;
    *b = 1;
    *c = 2;
    *d = 3;

    free(d);
    free(c);
    free(b);
    free(a);

    return EXIT_SUCCESS;

d:
    free(c);
c:
    free(b);
b:
    free(a);
a:
    return EXIT_FAILURE;
}
Stargateur
  • 24,473
  • 8
  • 65
  • 91
  • 6
    Uh, if Ubuntu cannot allocate (likely) ~16 bytes, the system has other problems... – Déjà vu Apr 13 '18 at 15:48
  • 1
    @machine_1 if a process calls `exit` it's not leaking memory. – davmac Apr 13 '18 at 15:49
  • That's fine, but likely not *the* problem. – Eugene Sh. Apr 13 '18 at 15:50
  • @davmac If `!c` is true, then at least `a` and `b` are valid. A call to `exit()` without `free()` is not a memory leak? – machine_1 Apr 13 '18 at 15:54
  • @EugeneSh. yeah, I answer without read valgrind output, need a [mcve] for better answer, * fly away * – Stargateur Apr 13 '18 at 15:58
  • @machine_1 On Linux at least, leaving the process with `exit()` has the OS [reclaim](https://stackoverflow.com/questions/12092606/does-exit-free-allocated-memory-on-both-success-and-failure) the allocated and non-freed memory. – Déjà vu Apr 13 '18 at 15:59
  • @RingØ Hmm. I didn't know about that; seems interesting. – machine_1 Apr 13 '18 at 16:03
  • if memory allocation fails, does malloc crash? as per the log, it crashed when he called malloc. I think crash may have something to do with lines 1-124 which is not shown – user2962885 Apr 13 '18 at 17:46