-2

So I have narrowed the code to exactly what I need help with. Sorry for the wall of text and scrappy code before.

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

// Function Protype
void test_function(int **ptr2);

// Main Function
int main(void) {
    int *ptr1;
    int i = 0; // Counter

    test_function(&ptr1); // Calling function

    for (i = 0; i < 10; i++) {
        printf("%d", ptr[i]); // Should print numbers from 0-9
    }

    return 0;
}

// Custom Function
void test_function(int **ptr2) {
    // Variables
    int i = 0; // Counter
    int j = 0; // Dynamic allocator

    *ptr2 = malloc(sizeof(int) * j); // Allocates 10 slots of size int

    for (i = 0; i < 10; i++) {
        *ptr2[i] = i; // <----- This line gives a segmentation fault error
    }

    return;
}

So basically, this program should iterate through an array using malloc to assign a dynamic size of j, input the numbers 0-9 and display the 0-9 to the user. However the line in the function test_function that is commented is giving me a segmentation fault error.

MoMoe0
  • 65
  • 8
  • 1
    Use a debugger. At a minimum it will tell you which line of code is triggering the seg fault and this is info you should share with us. Also, try to make your question concise: the code, what it is supposed to do and what it is actually doing. For example, why do you show two versions of the code? What are we supposed to take from that and which one are you really asking about? – kaylum Oct 04 '16 at 23:24
  • install GDB (GNU debugger) it will take you 10 mins to figure out your mistake – tesseract Oct 04 '16 at 23:27
  • How are we supposed to debug this entire program? Reduce it. – Ryan Oct 04 '16 at 23:46
  • The lines `spacePos = malloc(sizeof(*spacePos[j]));` and `printf("%p", spacePos[j]);` should be removed. Also `*spacePos[j]` should be `(*spacePos)[j]` – M.M Oct 04 '16 at 23:50
  • also you only `malloc` space for one `int` but then you index multiple places into that space – M.M Oct 04 '16 at 23:54
  • 1
    in the updated code, change `*ptr2[i]` to `(*ptr2)[i]` – M.M Oct 05 '16 at 02:05
  • You can remove the struck-out lines from your post, anyone who wants to see the old revisions can view the edit history – M.M Oct 05 '16 at 02:05
  • @M.M Awesome! That seemed to have fixed it. Thanks man. Is that just correct pointer array syntax? Thanks again man, really appreciate it. How do I marked this as solved? – MoMoe0 Oct 05 '16 at 02:10
  • I'll post an answer – M.M Oct 05 '16 at 02:10

2 Answers2

1

The line *ptr2[i] = i; should be:

(*ptr2)[i] = i;

The way you had it means *(ptr2[i]) which is an error because ptr2 only points to one thing.

M.M
  • 138,810
  • 21
  • 208
  • 365
0

int *spacePos = NULL;

find_spaces(name, &spaceCount, &spacePos);

SpacePos is already a pointer to an int, and the pointer points to some space in memory where the integer is stored. this pointer address is stored in memory at address &SpacePos somewhere in your data segment in memory Where in memory are my variables stored in c?.

when you pass the address of 'SpacePos' to a function and manipulate it, you are actually changing the pointer value stored in SpacePos and not what its pointing to, and a seg fault will result. (you essentially changed the memory address stored in SpacePos, now you are pointing to invalid memory)

What does "dereferencing" a pointer mean?

try find_spaces(name, &spaceCount, spacePos); Hope it helps.

######################### PART 2

void test_function(int **ptr2) {

int i = 0; // Counter
int j = 10; // Dynamic allocator
*ptr2 = malloc(sizeof(int) * j); // Allocates 10 slots of size int

for (i = 0; i < 10; i++) {
    *((*ptr2)+i) = i; // <----- This line gives a segmentation fault error
}

return;

}

Community
  • 1
  • 1
tesseract
  • 891
  • 1
  • 7
  • 16
  • I think he wants to pass `&spacePos` so that the `find_spaces` function can dynamically allocate an array to store the "return values" – M.M Oct 04 '16 at 23:53
  • yes your right(my mistake) i'll leave my comment since it might be useful for the OP but dont give me any points, whats the standard convection in this case should he say `void *spacepos = NULL` and cast it at a later stage when he allocates memory, since it can be misleading? – tesseract Oct 04 '16 at 23:59
  • No, `int *spacePos` is correct because it is indeed going to point to `int` – M.M Oct 05 '16 at 00:01
  • @M.M Hey sorry guys, I made my coding more concise in an edit. Can you check it out? I appreciate the time you guys have put out for this so far. I still need to learn a lot more about pointers and malloc. – MoMoe0 Oct 05 '16 at 02:02