segmentation fault for extern


 
Thread Tools Search this Thread
Top Forums Programming segmentation fault for extern
# 1  
Old 07-21-2010
segmentation fault for extern

Why this is happening when both of them compiled together and run?

I am getting segmentation fault SIGSEGV.

File1.c:
Code:
int arr[80];

File2.c:
Code:
extern int *arr;
int main() {
   arr[1] = 100;
}

# 2  
Old 07-21-2010
Quote:
Originally Posted by royalibrahim
File1.c:
Code:
int arr[80];

File2.c:
Code:
extern int *arr;
int main() {
   arr[1] = 100;
}

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..
# 3  
Old 07-21-2010
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.
# 4  
Old 07-22-2010
More precisely:

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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

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)
Discussion started by: wisecracker
11 Replies

2. Programming

Segmentation fault

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)
Discussion started by: sizzler786
2 Replies

3. Solaris

Segmentation fault

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)
Discussion started by: cjashu
2 Replies

4. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

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)
Discussion started by: pooyair
6 Replies

5. Programming

Segmentation fault in C

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)
Discussion started by: omega666
7 Replies

6. Programming

segmentation fault

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)
Discussion started by: mind@work
3 Replies

7. Linux

Segmentation fault

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)
Discussion started by: big123456
0 Replies

8. AIX

Segmentation fault

Hi , During execution a backup binary i get following error "Program error 11 (Segmentation fault), saving core file in '/usr/datatools" Riyaz (2 Replies)
Discussion started by: rshaikh
2 Replies

9. UNIX for Dummies Questions & Answers

Segmentation Fault

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)
Discussion started by: compbug
3 Replies

10. Programming

segmentation fault

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)
Discussion started by: wojtyla
3 Replies
Login or Register to Ask a Question