-1

I have searched for a way to do this on my own, but I haven't found a situation that exactly matches mine and I'm not experienced enough to derive what to do from similar situations. So I'm hoping to get some help with my specific situation.

I have a struct, and I need to create 3 arrays of them. But when I allocate the memory using [], I run out of memory. So I think I need to use malloc; but I cannot figure out how to do it. Here is my code:

struct key {

char symbol[10];
int quantity;
char GroupID[10];

};  

Then in main I have:

struct key PrevKeys= malloc(345000*sizeof(struct key));
struct key ActivityKeys= malloc(345000*sizeof(struct key));
struct key CurKeys= malloc(345000*sizeof(struct key));

But I keep getting "invalid initializer" error from the compiler.

Earlier I tried this, which compiled just fine but gave me a Seg Fault when I ran it (I'm assuming because I don't have enough memory in the stack):

 struct key PrevKeys[345000];
 struct key ActivityKeys[345000];
 struct key Curkeys[345000];

Any ideas on how I can allocate these 3 arrays of structs would be very, very appreciated.

Hopper06
  • 89
  • 2
  • 10
  • for one thing, you want this to be "struct key *PrevKeys = malloc()" or similar - arrays and pointers are equivalent for most purposes, but NOT equivalent to the underlying type – Arlie Stephens Aug 12 '14 at 21:08

3 Answers3

5
struct key* PrevKeys= malloc(345000*sizeof(struct key));
struct key* ActivityKeys= malloc(345000*sizeof(struct key));
struct key* CurKeys= malloc(345000*sizeof(struct key));
gnasher729
  • 51,477
  • 5
  • 75
  • 98
2

You're trying to assign the result of malloc, which is a pointer, to a struct, which is not possible. You have to declare them as pointers:

struct key *PrevKeys = malloc(345000 * sizeof(struct key));
...

and don't forget to free the memory:

free(PrevKeys);

If you're still getting errors, try to allocate just 10 elements, and if it works, you can be sure 345000 elements is too much.

Also, I'd prefer to not use sizeof(struct key) but sizeof(*PrevKeys) since it will always return the right size for PrevKeys, even if you change the type of PrevKeys.

Kapichu
  • 3,346
  • 4
  • 19
  • 38
  • 1
    Auto-allocation is usually done on the stack, which is often rather restricted in terms of maximum size (something like 1MB). `malloc` OTOH uses the heap, which is only limited by the OS (can be in the order of GB). Thus, `malloc` can work where stack allocation cannot. – Drew McGowen Aug 12 '14 at 21:20
  • Thank you for the explanation for the REASON why it wasn't working. I know malloc returns a pointer but, even after a few years of C programming, pointers are still difficult for me to understand. So thank you. Also, I'm sure my heap can handle a little 30-ish bytes * 345000 * 3. I'm working on a modern machine, after all. :) – Hopper06 Aug 13 '14 at 17:01
0

Standard C function malloc returns a pointer of type void *. In your definitions you attempt to assign this pointer to objects of type struct key. Use instead

struct key *PrevKeys = ( struct key * )malloc( 345000 * sizeof(s truct key ) );
struct key *ActivityKeys = ( struct key * )malloc( 345000 * sizeof( struct key ) );
struct key *CurKeys = ( struct key * )malloc( 345000 * sizeof( struct key ) );

As for the array definitions then you should define them with the static storage duration that is either outside any function or inside a function (including main) but specifying keyword static. For example

struct key PrevKeys[345000];
struct key ActivityKeys[345000];
struct key Curkeys[345000];

int main( void )
{
//...

or

int main( void )
{
   static struct key PrevKeys[345000];
   static struct key ActivityKeys[345000];
   static struct key Curkeys[345000];
   //...

Also it would be a good idea if you would define a mnemonic name for magic number 345000

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I'm going with the answer above, because 1) you should never cast the results of malloc, it can hide errors that you want to know about, and 2) I already explained that I tried allocating on the stack, and there isn't enough stack memory, so while struct key PrevKeys[345000] will compile, it will not run, I get a Seg Fault error (because there isn't enough stack memory). I HAVE to malloc, to get the memory I need from the heap. – Hopper06 Aug 13 '14 at 16:56
  • @user3636742 1) You ALWAYS should use cast for the result of malloc. otherwise you will INDEED HIDE errors because the result of the malloc can be assigned to a pointer of any type. Such errors are difficult to find. 2) Using keyword static provides that the array will not be allocated in the stack. So you simply did not understand what I wrote. Arrays with static storage duration are allocated in static storage. – Vlad from Moscow Aug 13 '14 at 20:53