8 Replies - 765 Views - Last Post: 30 October 2011 - 01:11 PM Rate Topic: -----

#1 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Allocating space on heap for struct - C

Posted 30 October 2011 - 12:01 PM

Here is what I need to do:

2. Implement airport flight information data structure that contains the following data for the
planes leaving on a given day:

• Flight number
• Originating airport (three characters)
• Destination airport (three characters)
• Departure time (four characters)

If this definition must be visible to the main method and other methods, place it before the start
of the main method – this makes it globally accessible.

3. Allocate at least five flight information structures on the heap and store appropriate data in
them. The following statement allocates enough space for a single record and returns a
pointer to it in the variable new_ptr, which must be declared to be a pointer to the structure
type described above.

/* Allocate space on heap for one structure, assign pointer to new_ptr */
struct flight_info * new_ptr = (struct flight_info *)malloc(sizeof(struct
flight_info));
/* Put data in structure */
new_ptr->flightNumber = 125;
(void)strcpy((* new_ptr).origAirport, "CHS"); /* copy a string */
(void)strcpy (new_ptr->destAirport, "CLT");
(void)strcpy (new_ptr-> departureTime, "1000");

In addition to <stdio.h>, include <stdlib.h> and <string.h> for malloc and strcpy.



Here is what I have:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct flightInfo {
	int flightNum;
	char departAirport[3];
	char destAirport[3];
	char departTime[4];
}

main() {
	/* Allocate space on heap for one structure.  Then assign pointer to new_ptr. */
	struct flightInfo * new_ptr = (struct flightInfo *)malloc(sizeof(struct flightInfo));
	
	/* Put data in structure. */
	new_ptr -> flightNum = 125;
	(void)strcpy ((* new_ptr).departAirport, "CHS"); /* copy a string */
	(void)strcpy (new_ptr-> destAirport, "CLT");
	(void)strcpy (new_ptr-> departTime, "1000");
}



Here is my error:

Exited: ExitFailure 120



I need help with the error. I can't seem to find any help with google.

I have 2 other questions!
  • Is the memory being allocated for the struct actually being done on the heap?
  • What editor do you use for C programming? My teacher has me on codepad.org doing this assignment.
My question is if I set everything up and the memory for the struct is actually allocated on the heap.

Is This A Good Question/Topic? 0
  • +

Replies To: Allocating space on heap for struct - C

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1530
  • View blog
  • Posts: 5,518
  • Joined: 03-August 09

Re: Allocating space on heap for struct - C

Posted 30 October 2011 - 12:06 PM

(void)strcpy ((* new_ptr).departAirport, "CHS");


your not taking the null termination into consideration there. "CHS" is actually 4 characters 'C', 'H', 'S', and '\0'

also, why are you casting the return value of strcpy to void? that dose nothing and makes your code hard to read.

Quote

Is the memory being allocated for the struct actually being done on the heap?

if you use malloc then yes.

Quote

What editor do you use for C programming? My teacher has me on codepad.org doing this assignment.


it doesn't matter, i like notepad++ or vim depending on the system I'm using. on windows with C and C++ i like using code::blocks(an IDE not an editor)

This post has been edited by ishkabible: 30 October 2011 - 12:08 PM

Was This Post Helpful? 0
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Allocating space on heap for struct - C

Posted 30 October 2011 - 12:18 PM

Quote

My teacher has me on codepad.org doing this assignment.
That's horrible. I can see using codepad in a pinch, but using it as a development environment??

You should setup a proper editor and compiler on your computer. It depends on your operating system. On Windows, setup Visual C++. On Macs, you need to install Xcode. On Linux, grab gcc, which is the compiler, and then an editor of your choice (just Google). If you use an IDE, you can use the IDE to compile code, otherwise, with a plain text editor, you have to open up a terminal and compile from there.
Was This Post Helpful? 0
  • +
  • -

#4 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Allocating space on heap for struct - C

Posted 30 October 2011 - 12:19 PM

Oh yea I forgot about that. But after changing the struct to this:

struct flightInfo {
    int flightNum;
    char departAirport[3];
    char destAirport[3];
    char departTime[4];
}



It still says the same error: Exited: ExitFailure 120

And I am not sure what the casting to a void does. That is part of the snippet that my teacher provided in the instructions and she had it like that. I do not even know what it does casting it to void like that.
Was This Post Helpful? 0
  • +
  • -

#5 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1530
  • View blog
  • Posts: 5,518
  • Joined: 03-August 09

Re: Allocating space on heap for struct - C

Posted 30 October 2011 - 12:32 PM

1) you didn't change the structure.
2) use a debugger, it's a life saver for these kind of things. most IDEs will have some degree of ingratiation for debugging. if you go with editor and compiler route(e.g. no IDE) then you will have to learn to use a debugger like gdb.

Quote

And I am not sure what the casting to a void does. That is part of the snippet that my teacher provided in the instructions and she had it like that. I do not even know what it does casting it to void like that.


it doesn't do anything aside from make your code hard to read...bleh

This post has been edited by ishkabible: 30 October 2011 - 12:33 PM

Was This Post Helpful? 0
  • +
  • -

#6 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1418
  • View blog
  • Posts: 2,681
  • Joined: 30-May 10

Re: Allocating space on heap for struct - C

Posted 30 October 2011 - 12:38 PM

> It still says the same error: Exited: ExitFailure 120
Perhaps if you were explicit with
return 0;
at the end of main, rather than merely falling off the end of a function returning int, you would get a normal exit status.

> I do not even know what it does casting it to void like that.
If the rest of main() is from your teacher, I think they're focussing on the wrong things.

strcpy() always returns the dest pointer. Extreme pedantry over testing the return results of non-void functions would dictate that you must use this result (even though it is always known and invariant). Casting to void is really saying "I'm not interested in this return result".
Was This Post Helpful? 0
  • +
  • -

#7 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Allocating space on heap for struct - C

Posted 30 October 2011 - 01:01 PM

Sorry ishkab, I did change it but just posted the wrong code. I changed it to this:

struct flightInfo {
	int flightNum;
	char departAirport[4];
	char destAirport[4];
	char departTime[5];
}



Salem, your last paragraph went straight over my head.

I tried inserting a "return 0" at the end with how the code is now and it produces an error. I then left the return statement and changed the signature to "int main()" and still have an error. I even tried combinations with "void main()" but still nothing.

I do not absolutely have to use the code provided by the teacher so I can try some other things I guess; like taking out the void casts.
Was This Post Helpful? 0
  • +
  • -

#8 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1418
  • View blog
  • Posts: 2,681
  • Joined: 30-May 10

Re: Allocating space on heap for struct - C

Posted 30 October 2011 - 01:03 PM

Well saying "I did..." and "an error...." doesn't really help us that much, since we're not watching your screen while you do this.

Post actual messages.
Was This Post Helpful? 0
  • +
  • -

#9 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Allocating space on heap for struct - C

Posted 30 October 2011 - 01:11 PM

Yea I realize that. Was just letting you know I tried all combinations of those. I will just go another route and come back later unless someone has some other ideas.

Thanks for the help so far.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1