1

I have looked at several examples here and I am still coming up empty with this. I have an extern int *sharedarray[] in my header file. I define it as int *sharedarray[] in my three .c files. In my first .c file I allocate how much memory it is going to need with malloc. It must be dynamically allocated at that point. After that I set the values. For the purpose of testing I am just incrementing them by 1. Then, another .c file, I go to print the array. What was 0,1,2,3 has become 0,3,2,2 ?

I am at a total loss for what is going on here. I am sure that this is something simple, but it has been blocking me for over 4 hours now. Does anyone know what I should do?

header.h

extern int *array[];

file1.c

int *array[];

file2.c

int *array[];

My make files uses:

file1: $(proxy_obj) file2.o, header.o
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
Burdell
  • 60
  • 7
  • 1
    You define it only one file not multiple. You include header to get declaration in many files. Can you show the relevant codes in all `.c` and `.h` files? – Mohit Jain Jul 08 '15 at 05:47
  • By layout do you mean how I declare the array in each file? Each `.c` has an `#include header.h` – Burdell Jul 08 '15 at 05:49
  • 2
    do you mean three modules (that compile to make a single program) or three programs. – Jasen Jul 08 '15 at 05:49
  • @user3593210 Yes, show relevant (4-5 lines) from each file and how do you compile it. – Mohit Jain Jul 08 '15 at 05:50
  • 1
    Note that `extern int *sharedarray[];` doesn't match `int *sharedarray;` — the types are radically different (one is an array of pointers to `int`, the other is a single pointer to `int`). So, which compilation warnings are you not heeding? Or are you not compiling with `-Wshadow` (assuming you have GCC) — which isn't as unusual as all that; most people don't compile with `-Wshadow` most of the time, myself included. But I know how to [declare and define external variables](http://stackoverflow.com/questions/1433204) — read cautiously and stop at the first stopping point. – Jonathan Leffler Jul 08 '15 at 05:53
  • I mis-typed earlier, it is `int *sharedarray[]` – Burdell Jul 08 '15 at 05:57
  • 2
    Are you looking for something like http://linux.die.net/man/3/shm_open ... but it sounds like you don't actually mean "between programs" as stated. Separate .c files != separate program necessarily - only if they have separate main()s. – technosaurus Jul 08 '15 at 05:58
  • 1
    OK; `header.h` is fine (for a statically allocated array). Only one of the other two files should define `sharedarray`, and it needs to specify the size. Oh, futz! You want to dynamically allocate the array. Do you really want an array of integer pointers, or do you want an array of integers? Most likely the latter, so you need `extern int *sharedarray;` in the header, and then one of the file defines `int *sharedarray;` at file scope and initializes it with dynamic allocation in a function. The other files can then use `sharedarray`. – Jonathan Leffler Jul 08 '15 at 06:01
  • Technosaurus, you get an upvote. Thanks for pointing out that I was looking at this the wrong way. Using shmget to pass around memory and it seems to be working now. Thanks. – Burdell Jul 08 '15 at 06:21
  • If you want **to share data between different running [programs](https://en.wikipedia.org/wiki/Computer_program)** (not routines or translation units), that is different [processes](https://en.wikipedia.org/wiki/Process_%28computing%29), it **is operating system specific**; on Linux see e.g. [shm_overview(7)](http://man7.org/linux/man-pages/man7/shm_overview.7.html) – Basile Starynkevitch Jul 08 '15 at 09:07

1 Answers1

2

Please do the following ....

header.h

extern int* array;

file1.c

// ... in code
// declared globally  
int* array;

// in your function...
// numInts contain the number of ints to allocate...
array = (int*)malloc( numInts, sizeof( int ));

// set the values...
array[ 0 ] = 0; 
array[ 1 ] = 1; 
array[ 2 ] = 2; 

file2.c

#include "header.h"

//... in your function, print out values
printf( "Values are %d,%d,%d\n", array[ 0 ], array[ 1 ], array[ 2 ] );

From reading your request, you want an array of ints and to be able to use them between two source files.

Hope this helps you sort out your problem, thanks

PS - Please use the above code as example only - also I have written this quickly at work - so forgive any typo's or errors.

Neil
  • 1,036
  • 9
  • 18