A
Andreas Vinther
I have a small piece of code that compiles but does not perform like I
want it to.
This code works:
----------------
void *y0;
void *y1;
void *y2;
void *y3;
void *y4;
y0 = ssGetOutputPortSignal(S,0);
y1 = ssGetOutputPortSignal(S,1);
y2 = ssGetOutputPortSignal(S,2);
y3 = ssGetOutputPortSignal(S,3);
y4 = ssGetOutputPortSignal(S,4);
This code doesn't work:
-----------------------
void **y;
int i = 0;
*y = malloc(5 * sizeof(void*));
while (i < 5)
{
*(y+i) = ssGetOutputPortSignal(S,i);
i++;
}
I've also tried to use *y and *(y) instead of *(y+i). They all
compile, but does also violate my segments :]
Can anyone tell me why i get segmentation violation?
To me i seems like the code should perform somewhat similar, but I
regulary mess up when using pointers and pointers-to-pointers.
I know I have to use free(*y) later on. I also know that I ought to
check if malloc returns NULL. But neither seems to generate my problem.
I beg of all of you C gurus out there, please help poor little stupid
me. I know that some of you hardcore C guys probably lived inside a
computercabinet since birth and dream dreams in scrolling green symbols,
which sometimes fork()'s into severel parallel dream-threads.
Andreas
want it to.
This code works:
----------------
void *y0;
void *y1;
void *y2;
void *y3;
void *y4;
y0 = ssGetOutputPortSignal(S,0);
y1 = ssGetOutputPortSignal(S,1);
y2 = ssGetOutputPortSignal(S,2);
y3 = ssGetOutputPortSignal(S,3);
y4 = ssGetOutputPortSignal(S,4);
This code doesn't work:
-----------------------
void **y;
int i = 0;
*y = malloc(5 * sizeof(void*));
while (i < 5)
{
*(y+i) = ssGetOutputPortSignal(S,i);
i++;
}
I've also tried to use *y and *(y) instead of *(y+i). They all
compile, but does also violate my segments :]
Can anyone tell me why i get segmentation violation?
To me i seems like the code should perform somewhat similar, but I
regulary mess up when using pointers and pointers-to-pointers.
I know I have to use free(*y) later on. I also know that I ought to
check if malloc returns NULL. But neither seems to generate my problem.
I beg of all of you C gurus out there, please help poor little stupid
me. I know that some of you hardcore C guys probably lived inside a
computercabinet since birth and dream dreams in scrolling green symbols,
which sometimes fork()'s into severel parallel dream-threads.
Andreas