I have been running Xcode3 in Xcode4. This morning I started a new project in Xcode4; copied the code; made some changes to the prototyping to avoid warnings and ran the code. I got different results.
I'm I crazy?
I'm I crazy?
Yes as far as the code goes, at the moment the SIGABRT is the issue. But I am also interested in the more basic question of how can I cut and paste error free, warning free code and come up with a run time error.
OMG. You consort with the evil empire!!!?
HUH? Added a print statement to the code to see how many times I can run the failing procedure. It ran. Took the print statement out. It ran again. Closed the code; opened it; it failed; print statement back in, it ran. Closed code. Open ed it with print statement. The offending procedure failed the second time it ran. Same error about pointer not being freed.
Just because you say you are not working for the EVIL EMPIRE... Send ticket to Quebec so that I can come verify the facts.
Of course, I should check index values. They are already checked all over the code but how hard is it for a compiler to know that an index value has gone negative?
I have been running Xcode3 in Xcode4. This morning I started a new project in Xcode4; copied the code; made some changes to the prototyping to avoid warnings and ran the code. I got different results.
I'm I crazy?
So if I allocate 10 spots in memory for an array, I can do this
for (i = -5; i < 5; i++)
array = ... ?
who would have thunk?
I speak 2 languages and can write code in 4. My goal is to speak 4 languages and to write code in 8.I don't expect you to be impressed that I know three languages but that's enough.
Appreciate the lesson. Unfortunately your student is not advanced enough to understand it all. I never would have guessed that c allow negative indicies.
So if I allocate 10 spots in memory for an array, I can do this
for (i = -5; i < 5; i++)
array = ... ?
who would have thunk?
$ cat test.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
{
int array[] = { 1, 2, 3, 4, 5 };
int * arrayp = array;
int i = 0;
while(i < 5)
{
printf("%d ", *arrayp);
arrayp++;
i++;
}
printf("\n");
for(i = -5; i < 0; i++)
{
printf("%d ", arrayp[i]);
}
printf("\n");
return EXIT_SUCCESS;
}
$ gcc -o test -Wall test.c
$ ./test
1 2 3 4 5
1 2 3 4 5
Appreciate the lesson. Unfortunately your student is not advanced enough to understand it all. I never would have guessed that c allow negative indicies.
So if I allocate 10 spots in memory for an array, I can do this
for (i = -5; i < 5; i++)
array = ... ?
who would have thunk?
I would need a better reason for learning a new language than run time safety. I'm not doing national security. I don't expect you to be impressed that I know three languages but that's enough. (actually one I'm rusty at, and nobody really knows the other one no matter what they say)
Some what afraid to ask. But is all this an intellectual exercise in an unintended consequence of C's design or can it serve a real purpose?
Some what afraid to ask. But is all this an intellectual exercise in an unintended consequence of C's design or can it serve a real purpose?
So in the sense that I asked the question, it is an unintended consequence. Pointer arithmetic is an important part of C - used it myself on occasion; but what code might actual require a negative index; or I have missed something in this discussion.
arrayp++; *arrayp++;
movl %eax, -4(%ebp)
addl $4, -4(%ebp)
addl $4, -4(%ebp)
i++; array[i]++;
addl $1, -4(%ebp)
movl -4(%ebp), %eax
movl -24(%ebp,%eax,4), %eax
leal 1(%eax), %edx
movl -4(%ebp), %eax
movl %edx, -24(%ebp,%eax,4)