C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 21st, 2007, 04:54 AM
insaniak insaniak is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 2 insaniak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 7 sec
Reputation Power: 0
Malloc an ARRAY of structures

So I search and search but it seems that very few people want to do this - which I think should be rather simple to solve arrrrgh!

Currently my program defines a structure, then declares an array of 500 of them, which I then read values into one by one later on by assigning values to new_transaction[1].amount, new_transaction[7].year etc etc...

Code:
struct transaction{
		int date;
		int month;
		int year;
		float amount;
};
struct transaction new_transaction[500];

...
...

while(num_lines >= file_line){ 
		fscanf(input_file,"%d/%d/%d %f",&new_transaction[file_line].date,&new_transaction[file_line].month,&new_transaction[file_line].year,&new_transaction[file_line].amount); 
		file_line++;
}


BUT! I want to only declare the array to be as big as a figure I read in from a textfile further down the program. I have tried using malloc as follows, but I can't find the syntax anywhere for an example of using malloc with an array of structs!!:

Code:
struct transaction{
		int date;
		int month;
		int year;
		float amount;
};
struct transaction *new_transaction;

...
...

num_lines = 0;
while (!feof(input_file)){
		num_lines++;
		fgets(file_line_contents,99,input_file);
}

...
...

while(num_lines >= file_line){
	        new_transaction[file_line] = (struct *)malloc(sizeof(struct transaction));
		fscanf(input_file,"%d/%d/%d %f",&new_transaction[file_line].date,&new_transaction[file_line].month,&new_transaction[file_line].year,&new_transaction[file_line].amount); 
		file_line++;
}


Any help is greatly appreciated, even if just a nod in the right direction! I'm sure there must be a simple solution but I can't see where!

George

Reply With Quote
  #2  
Old March 21st, 2007, 05:46 AM
insaniak insaniak is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 2 insaniak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 7 sec
Reputation Power: 0
Thanks to anyone who looked at this...a friend on another forum managed to solve it for me thanks.


Reply With Quote
  #3  
Old March 21st, 2007, 06:48 AM
Oler1s Oler1s is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2006
Posts: 1,906 Oler1s User rank is General (90000 - 100000 Reputation Level)Oler1s User rank is General (90000 - 100000 Reputation Level)Oler1s User rank is General (90000 - 100000 Reputation Level)Oler1s User rank is General (90000 - 100000 Reputation Level)Oler1s User rank is General (90000 - 100000 Reputation Level)Oler1s User rank is General (90000 - 100000 Reputation Level)Oler1s User rank is General (90000 - 100000 Reputation Level)Oler1s User rank is General (90000 - 100000 Reputation Level)Oler1s User rank is General (90000 - 100000 Reputation Level)Oler1s User rank is General (90000 - 100000 Reputation Level)Oler1s User rank is General (90000 - 100000 Reputation Level)Oler1s User rank is General (90000 - 100000 Reputation Level)Oler1s User rank is General (90000 - 100000 Reputation Level)Oler1s User rank is General (90000 - 100000 Reputation Level)Oler1s User rank is General (90000 - 100000 Reputation Level)Oler1s User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 Days 11 h 36 m 27 sec
Reputation Power: 998
I hope he pointed out the syntax was type* p = malloc(n * sizeof p). In C, don't typecast malloc.

EDIT: There also seems to be confusion in your code. You have one struct pointer. Using index notation is pointless because it will be out of bounds.

What you probably should have done is have a pointer to a pointer.
__________________
Need help with broken code? Your question should be like a good bug report: (1) It has the smallest number of steps to reproduce the problem you see (2) It tells us precisely what you expected to see and (3) It tells us what you saw and how it differed from what you expected. We need all three to help you.
Want better answers? Tell us what you Googled for and what steps you took to answer your own question.

Last edited by Oler1s : March 21st, 2007 at 06:52 AM.

Reply With Quote
  #4  
Old March 21st, 2007, 03:06 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 3,422 clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 3 Weeks 8 h 12 m
Reputation Power: 969
Quote:
Originally Posted by Oler1s
I hope he pointed out the syntax was type* p = malloc(n * sizeof p). In C, don't typecast malloc.


Nonsense! Just because you are not required to cast the return value of malloc() in C does not mean that you shouldn't. There is no harm in making your C code C++ compilable (in fact there is a significant benefit).

Clifford

Reply With Quote
  #5  
Old March 21st, 2007, 04:54 PM
salem's Avatar
salem salem is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jun 2005
Posts: 2,026 salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 1 Month 2 Weeks 2 h 57 m 12 sec
Reputation Power: 839
I don't see how potentially hiding the fact that you failed to prototype the function properly counts as a benefit.

Failing to include stdlib.h without a cast will draw a compiler warning.

With a cast, your code is wrong. The compiler implicitly declares malloc as returning int (not a pointer), and the result is converted by the cast on that basis. This is a disaster waiting to happen on machines with different sizes for pointers and integers.

> There is no harm in making your C code C++ compilable
If that were the only difference between C and C++, then I might agree. But there's so much more that you might need to consider in getting your C program up to C++ standard that a few simple casts just won't solve.

It's better IMO to use each language to it's strengths than to reduce both to the lowest common denominator known as c/c++ .
Comments on this post
jwdonahue agrees: Salem is correct. Since the standard back in 1999, explicit casts of malloc()'s return are not
needed and should not be used.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
UK Voter? Please send a message to Incapability Brown and the rest of Zanu-Labour

Reply With Quote
  #6  
Old March 21st, 2007, 05:05 PM
jwdonahue's Avatar
jwdonahue jwdonahue is online now
Bellevue WA, USA
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 1,303 jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 7 h 23 m 26 sec
Reputation Power: 140
For C and C++ compatibility, it is sometimes nescessary to resort to conditional compiler blocks (#if...#else...#endif). You should avoid malloc() in the C++ case at any rate.
__________________
Quality of responses my vary. If you pay me, I will engage my other neuron to solve your problem.

Reply With Quote
  #7  
Old March 21st, 2007, 07:44 PM
Purple Avenger Purple Avenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 271 Purple Avenger User rank is Sergeant (500 - 2000 Reputation Level)Purple Avenger User rank is Sergeant (500 - 2000 Reputation Level)Purple Avenger User rank is Sergeant (500 - 2000 Reputation Level)Purple Avenger User rank is Sergeant (500 - 2000 Reputation Level)Purple Avenger User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 1 h 51 m 31 sec
Reputation Power: 10
Why "avoid" malloc() in C++ ? Its part of the standard library, and I think you'll find many 'new' implementations calling it under the covers anyway.

Reply With Quote
  #8  
Old March 21st, 2007, 08:15 PM
jwdonahue's Avatar
jwdonahue jwdonahue is online now
Bellevue WA, USA
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 1,303 jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 7 h 23 m 26 sec
Reputation Power: 140
The fact that you have to cast the return value from malloc() to something else in C++ is sufficient reason in my book. But new/delete do supply stricter/better type'ing for the compiler to make use of and the standard does not require malloc() be "used under the covers" and in fact it does make a distinction between the free store (new/delete) and the heap (malloc/free). The two can in fact be seperate.

Becuase malloc() is required to allocate memory in such a way that it's alignment is sufficient for all conceivable data types, it can not be optimal. new can allocate strings on char boundaries for instance, allowing them to be packed without any wasted memory between them.

Getting back to the casting issue, which cast operation would you use? The old C style cast is not safe in C, much less C++. Do not forget that new and delete can be overridden for some types. Using malloc() to allocate space for such an object would violate the semantics/interface contract of that object type. I might want to use your code in an environment where all strings are stored in one kind of memory and all other data in some other type (I have done this btw, flash, eeproms and some kinds of RAM have address alignment and other issues)

Just because the language allows it, doesn't make it a good idea. Where the language and it's libraries do not provide implicit portability and you really do need portable code then you should be explicit about it. malloc() is part of <cstdlib> for backwards compatibility reasons only (as is the entire <cstdlib> library!). Some parts of that library are useful for writing C/C++ portable code, but not all of them, and malloc()/free() are among the interfaces that are there strictly for backwards compatibility. Using them in a C++ program is a bad idea (unless your writing the compiler and it's libraries).
Comments on this post
Lux Perpetua agrees!
salem agrees: Good catch on the C++ style casts being preferred

Reply With Quote
  #9  
Old March 22nd, 2007, 08:08 AM
salem's Avatar
salem salem is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jun 2005
Posts: 2,026 salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 1 Month 2 Weeks 2 h 57 m 12 sec
Reputation Power: 839
> Why "avoid" malloc() in C++ ?
malloc doesn't call constructors (and free doesn't call destructors).

Out of lazyness, the naive c/c++ programmer might just call malloc for some struct which another programmer decided to add a constructor to.

The only good reason (I can think of at the moment) to call malloc in C++ is to obtain memory to be passed on to some extern "C" API which might reallocate or free that memory.

Given another decade, C-style casts in C++ might just end up deprecated (some people want this). Then it will get even messier to convert malloc to C++ as the preferred C++ cast won't even be valid C.

And the only good reason for casting malloc in C is if you're using some ancient pre-ANSI C89 (C'mon people, that's nearly 20 years ago) which still uses char* as the return type of malloc instead of void*

Reply With Quote
  #10  
Old March 22nd, 2007, 06:54 PM
Purple Avenger Purple Avenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 271 Purple Avenger User rank is Sergeant (500 - 2000 Reputation Level)Purple Avenger User rank is Sergeant (500 - 2000 Reputation Level)Purple Avenger User rank is Sergeant (500 - 2000 Reputation Level)Purple Avenger User rank is Sergeant (500 - 2000 Reputation Level)Purple Avenger User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 1 h 51 m 31 sec
Reputation Power: 10
Quote:
Originally Posted by jwdonahue
Becuase malloc() is required to allocate memory in such a way that it's alignment is sufficient for all conceivable data types, it can not be optimal. new can allocate strings on char boundaries for instance, allowing them to be packed without any wasted memory between them.


Which of course slows down all string operations since they're no longer aligned. Why would you "require" programs to be slow?

Reply With Quote
  #11  
Old March 22nd, 2007, 08:20 PM
jwdonahue's Avatar
jwdonahue jwdonahue is online now
Bellevue WA, USA
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 1,303 jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 7 h 23 m 26 sec
Reputation Power: 140
Quote:
Originally Posted by Purple Avenger
Which of course slows down all string operations since they're no longer aligned. Why would you "require" programs to be slow?


As I said;

Quote:
Originally Posted by jwdonahue
allowing them to be packed without any wasted memory between them


Sometimes space is more valuable than time. Sometimes not.


Economics and system requirements (non-volitality for instance) can lead to system designs that are not as fast as they might otherwisse be. If speed of were the only consideration, we wouldn't have hard drives, flash or eproms. We probably wouldn't bother with general purpose computing systems; prefering instead to embed all application logic in custom ASIC's or FPGA's.

Last edited by jwdonahue : March 22nd, 2007 at 09:04 PM. Reason: Fixed a truncated quote.

Reply With Quote
  #12  
Old March 22nd, 2007, 09:59 PM
Purple Avenger Purple Avenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 271 Purple Avenger User rank is Sergeant (500 - 2000 Reputation Level)Purple Avenger User rank is Sergeant (500 - 2000 Reputation Level)Purple Avenger User rank is Sergeant (500 - 2000 Reputation Level)Purple Avenger User rank is Sergeant (500 - 2000 Reputation Level)Purple Avenger User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 1 h 51 m 31 sec
Reputation Power: 10
So why did you use the word "require" when its "sometimes yes sometimes no"?

Require is a very strong word.

Reply With Quote
  #13  
Old March 23rd, 2007, 01:35 AM
jwdonahue's Avatar
jwdonahue jwdonahue is online now
Bellevue WA, USA
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 1,303 jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level)jwdonahue User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 7 h 23 m 26 sec
Reputation Power: 140
Can you provide a more complet quote? I recall that I mentioned the standard required malloc() to supply a pointer that is properly aligned for all possible data types. If that is what you are referring to, I said it because it's true.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Malloc an ARRAY of structures


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT