Dynamic memory alloction for my array of struct

This is a discussion on Dynamic memory alloction for my array of struct within the C Programming forums, part of the General Programming Boards category; OK, new to the forums, and I have a problem with allocating memory for my struct 1 array cell at ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    9

    Dynamic memory alloction for my array of struct

    OK, new to the forums, and I have a problem with allocating memory for my struct 1 array cell at a time. Yes it has to be this way, stupid uni specifications, and is the last thing I need to get sorted.

    my struct is as follows:

    Code:
    struct event
    {
    	int year;
    	int month;
    	int date;
    	int start_time;
    	int finish_time;
    	char event_description[50];
    }*entry;
    the current allocation setup is as follows:

    Code:
    if((entry[entry_num]=(struct event*)malloc(sizeof(struct event))) == NULL)
    printf("Error: memory for event description has not been allocated\n");
    sorry if this is unclear in any way, in a bit of a ruch, and getting a bit excited about this working (its worth a fair bit of this module).

    Thanx for your help in advance and dont be scared to ask any Q's about the prog, so long your not on my course and want to see all my source code, thats is just wrong!

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    I think....

    You're declaring a pointer to a type struct event.... eg:

    Code:
    struct event {
     int one;
     int two;
     char three[60];
    }*event;
    is the same as say:
    Code:
    int bah;
    So the storage is already set aside. Try to print the sizeof *entry without trying to allocate memory.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    This would not be true for an array. If the array is set asside using a pointer, it is all the more important to allocate the space in memory, it can run out fast when the computer decideds to make enough spcae for an infinite number of entrys, so I am certain that i need to allocate the space in memory. I am, however unsure if this is the right way, as all information i have found on the net, in my course notes, and my text book seems unclear (to me at least) and was wondering if some 1 could help me. Its only 2 lines of code and its stopping my program of 600 lines form working fully. by the way, I have not given the error statement from visual studio pro .net 2003. The error report is as follows:

    error C2679: binary '=' : no operator found which takes a right-hand operand of type 'event *' (or there is no acceptable conversion).

    Hope this helps clear things up.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    You declare entry as a pointer to the struct and not initializing it, so when you dereference it with entry[entry_num] you're accessing some random memory address and the program is likely to crash.

    I think what you're aiming for is just increasing the size of the entry array every time you want to add a new element to it. The first thing you'll need to do is initialize entry to NULL so realloc() knows what to do with it the first time it's called (shown below). Then add a counter so your program knows how many entries are in the array. Then your allocations would go something like this:
    Code:
    void add_new_entry(void)
    {
      struct event *tmp;
    
      tmp = realloc(entry, sizeof(struct event) * (num_entries + 1));
      if(tmp == NULL)
      {
        // Memory allocation error, take whatever actions are necessary
      }
    
      entry = tmp;
      num_entries++;
    }
    You'd call that function when you need to create a new entry, and then you'd get the data and fill in the entry at the last index of the array. Is that what you're trying to do?
    If you understand what you're doing, you're not learning anything.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > error C2679: binary '=' : no operator found
    This pretty much tells me that you're using a C++ compiler to compile your C code.

    Compile with C, and remove those malloc casts
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    Salme, I have no choice in the matter, my lecture is been alkward and will only let us use vis stdio .net. Dont need to tell me, u need to tell him, and he wont listen.

    Istem, that code throws up the same error as I already have, so that is of no use to me.
    Last edited by pears0; 03-11-2005 at 10:10 AM.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    The problem isn't with the code, the problem is with the compiler. You asked for C code so that's what I gave you. I can't help it if your instructor's a moron.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    No need to tell me, any 1 on my course, or even my mates that can program, I'm getting narked off, cos this is yet another program I can't compleate cos of my lecturers idiocy and not my programming capability!!!

    Can some 1 suggest a decent C only compiler for me to test this in???

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Just so you can see that it really is correct code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct event
    {
      int year;
      int month;
      int date;
      int start_time;
      int finish_time;
      char event_description[50];
    } *entry = NULL;
    int num_entries = 0;
    
    void add_new_entry(void)
    {
      struct event *tmp;
    
      tmp = realloc(entry, sizeof(struct event) * (num_entries + 1));
      if(tmp == NULL)
      {
        // Memory allocation error, take whatever actions are necessary
        exit(1);
      }
    
      entry = tmp;
      num_entries++;
    }
    
    int main(void)
    {
      int i;
    
      for(i = 0;i < 5;++i)
        add_new_entry();
      return 0;
    }
    Code:
    itsme@dreams:~/C$ gcc -Wall entry.c -o entry
    itsme@dreams:~/C$
    Not even a warning...
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    OK, I might sound a bit like a dumb ass now, but how do i then access each entry, just to work out if i need to change my code. is it :

    entry[entry_num].(struct attribute name)????

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > Salme, I have no choice in the matter, my lecture is been alkward and will only let us use vis stdio .net
    So just make sure you call your files prog.c rather than prog.cpp, and you should be OK

    > entry[entry_num].(struct attribute name)????
    Yes, same notation you would use if entry were an array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    ok, now i get a new error with

    tmp = realloc(entry, sizeof(struct event) * (num_entries + 1));

    the error is:
    error C2440: '=' : cannot convert from 'void *' to 'event *'

    Think this might be another compiler error?

    Also, that can't be done in Visual Studio .NET pro 2003, it can't read C code, only C++. And yes, this is another idiot Bill Gate idea! Which means it cant compile .c files! Annoying, aint it?!
    Last edited by pears0; 03-11-2005 at 10:51 AM.

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    It's still compiling it as C++ for some reason. A quick work around is to typecast the return value of realloc() to struct entry *

    Example:
    tmp = (struct entry *)realloc(entry, sizeof(struct event) * (num_entries + 1));

    That's a bastardization of C, but what can you do when you're forced to use a retarded compiler? I don't agree with tying a language in with an IDE or a specific compiler at all.
    If you understand what you're doing, you're not learning anything.

  14. #14
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    Finnaly, it builds (again) now to test and see if it all works. Thank you for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  3. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. 2D array in dynamic memory
    By VanJay011379 in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2003, 07:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21