You need to remember that pointers and arrays are not the same, no matter what anybody tells you. (There are some conveniences provided where you can treat an array as a pointer (and vice versa), but they hide what's really going on.) Your declaration as a pointer with "extern int *arr" makes the sizeof(void*) bytes at location "arr" get treated as if they point to something, when in fact they contain garbage since you've defined an array there, not a pointer. The first few bytes of the array (i.e. the garbage) are dereferenced as if they were a pointer, and a segfault is the likely result.
Solution: Make both the declaration and the definition the same. Additionally, put the declaration in a header and make sure that File1.c #includes it, and that will prevent this sort of thing from happening.
Last edited by JohnGraham; 07-21-2010 at 09:19 AM..
More specifically: A pointer is an integer variable that holds a memory address. It takes a small and fixed number of bytes. You're using it to point to memory stored elsewhere.
When you define it as an array, you're telling it that all the memory for that array is stored at the memory address of the pointer, not the memory address stored in the pointer. It expects 80 bytes of data right there, not where it points to.
You could do "extern int arr[]" perhaps. That way you're telling it it's an array, as you'd expect, but the size at least wouldn't have to be known.
1. A pointer is an L-value (a variable in memory that you can assign a result to) that points to an address in memory.
2. An array is an area of memory. The "naked" name of an array is interpreted as the address of the zeroth element of the array. The "naked" name of an array is not an L-value and you can't assign anything to it. Trying to do so is a syntax error.
Thus you can substitute the naked name of an array when a pointer of the same type is expected.
Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1...
I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one.
Below are two very condensed snippets of which I have added the results inside the each code section.
IMPORTANT!... (11 Replies)
I keep getting this fault on a lot of the codes I write, I'm not exactly sure why so I'd really appreciate it if someone could explain the idea to me.
For example this code
#include <stdio.h>
main()
{
unsigned long a=0;
unsigned long b=0;
int z;
{
printf("Enter two... (2 Replies)
Hi Guys,
I just installed and booted a zone called testzone. When I logged in remotely and tried changing to root user I get this error:
"Segmentation fault"
Can someone please help me resolve this?
Thanks alot (2 Replies)
I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this :
i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
i have this code
int already_there(char *client_names, char *username) {
int i;
for(i = 0; i<NUM; i++) {
printf("HERE\n");
if (strcmp(client_names, username)==0) return(1);
}
return(0);
}
and i get a segmentation fault, whats wrong here? (7 Replies)
Hi,
I am having this segmentation fault not in the following program, bt. in my lab program . My lab program is horrible long so cannot post it here bt. I am using the following logic in my program which is giving the segmentation fault. Bt. if I run this sample program as it is it dosen't give... (3 Replies)
Hi,
on a linux Red HAT(with Oracle DB 9.2.0.7) I have following error :
RMAN> delete obsolete;
RMAN retention policy will be applied to the command
RMAN retention policy is set to redundancy 2
using channel ORA_DISK_1
Segmentation fault
What does it mean ? And the solution ?
Many thanks. (0 Replies)
Hi ,
During execution a backup binary i get following error
"Program error 11 (Segmentation fault), saving core file in '/usr/datatools"
Riyaz (2 Replies)
hello all,
I tried a program on an array to intialise array elements from the standard input device.it is an integer array of 5 elements.but after entering the 4th element it throws a message called "Segmentation Fault" and returns to the command prompt without asking for the 5th element.
... (3 Replies)
sometimes for this code i get a segmentation fault for codes llike this
:
int main{
int * a= 0;
int b;
a = (int*)malloc(sizeof(int));
///some code using these variable but no freeing of a
if(a){
free(a);
a = 0;
}
return... (3 Replies)