C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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 April 21st, 2004, 10:17 PM
gehenna_engine gehenna_engine is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 13 gehenna_engine User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to gehenna_engine
allocating memory for an array structure member

Can someone show me (if it's possible) how to appropriately allocate memory for an array structure member?

the structure would look something like this:

Code:

structure record {
    char name[32];
    char desc[80];
    char *items;

}

typedef struct record RC;


the member char *items is the one that will need to be allocated dynamically. The number of items will be determined by user input.

Thanks in advance for the help.

Last edited by gehenna_engine : April 21st, 2004 at 10:35 PM.

Reply With Quote
  #2  
Old April 21st, 2004, 10:57 PM
murugesan murugesan is offline
Dinesh_P_V
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: India
Posts: 259 murugesan New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 12 h 1 m 3 sec
Reputation Power: 0
Send a message via Yahoo to murugesan
A sample code for you
Code:
#include <stdio.h>
int main()
{
        struct record
        {
                char name[32];
                char desc[80];
                char *items;
        };
        int num=10;
        typedef struct record RC;
        RC *pp=(RC*)malloc(sizeof(RC));
        pp->items=(char*)malloc(sizeof(num));
        strcpy(pp->items,"DINESH_P_V");
        printf("%s",pp->items);
        free(pp);
}

HTH

-Murugesan

Reply With Quote
  #3  
Old May 12th, 2004, 10:45 AM
palex palex is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 2 palex User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi all !

I have a similar problem.
But I want to have an array of that kind of struct, something like :
RC* pp[NUMBEROF];
or RC** pp;
if it works...

When I do this :
pp[x] = new RC();
pp[x]->items=(char*)malloc(sizeof(num));
I don't have any crash, but the datas I put and retrieve inside later are invalid.

Any ideas, suggestions ?

Thanks

Reply With Quote
  #4  
Old May 12th, 2004, 11:13 AM
Hko Hko is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Groningen, The Netherlands (Europe)
Posts: 4 Hko User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by palex
pp[x] = new RC();
pp[x]->items=(char*)malloc(sizeof(num));


(We're talking C++ here, right?)
It won't solve your problem, but the second line here really belongs inside the constructor of class/struct RC. (And the "delete" for it, in the destructor)

Reply With Quote
  #5  
Old May 12th, 2004, 11:02 PM
murugesan murugesan is offline
Dinesh_P_V
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: India
Posts: 259 murugesan New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 12 h 1 m 3 sec
Reputation Power: 0
Send a message via Yahoo to murugesan
Quote:
Originally Posted by palex
pp[x] = new RC();
pp[x]->items=(char*)malloc(sizeof(num));


I think u need to allocate num size and not sizeof(num). Am I right ?

Or else here goes a sample.
Code:
class dinesh
{
public:
        char *items;
        dinesh()
        {
                cout<<"\ndinesh_p_v constructor\n";
        }
        ~dinesh()
        {
                cout<<"\ndinesh_p_v destructor\n";
                free(items);
        }
};
main()
{
        dinesh *dins[10];
        for(int i=0;i<10;i++)
        {
                dins[i]=new dinesh();
                dins[i]->items=(char*)malloc(10);
                sprintf(dins[i]->items,"dinesh");
                cout<<"\n"<<dins[i]->items<<"\n";
        }
        for(int i=0;i<10;i++)
                delete dins[i];
        return 0;
}


HTH
__________________
Dinesh_P_V

Reply With Quote
  #6  
Old May 12th, 2004, 11:30 PM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 3,398 jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 6 h 48 m 17 sec
Reputation Power: 886
Palex, this should help. If you programming in C++ quit using malloc. It's not type safe.

Code:
 // RC* pp[DIMENSION] ; // this works but it's not on the heap.
 RC **pp = new RC[DIMENSION] ;
 pp[0] = new RC() ;
 pp[0]->items = new char[ITEM_COUNT] ;
 
__________________
My worst nightmare was a pointless infinite loop.
Work in progress; don't poke the curmudgeon!
http://www.odonahue.com/

Reply With Quote
  #7  
Old May 13th, 2004, 04:49 AM
palex palex is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 2 palex User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks everybody...

My problem was (is...) what jwdonahue tried to solve.
But you can't do that :
RC **pp = new RC[DIMENSION];
(cannot convert from struct RC* to struct RC**)

-(of course, we're talking c++)-

How should I declare my pp for making it works ?

This isn't working :
------------------
structure record {
char name[32];
char desc[80];
char *items;
}
typedef struct record RC;

RC* pp[NUMBEROF];
(loop)
pp[x] = new RC();
pp[x]->items = new char[ITEM_COUNT];

My datas in items will be invalid.

This is working :
---------------
structure record {
char name[32];
char desc[80];
char items[ITEM_COUNT];
}
typedef struct record RC;

RC* pp[NUMBEROF];
(loop)
pp[x] = new RC();

My data in item are valid.

Of course, I can't use the second way, as I need lots of structs where items may be big.


So thanks and thanks in advance....

PAlex

Reply With Quote
  #8  
Old May 13th, 2004, 04:59 AM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 3,398 jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 6 h 48 m 17 sec
Reputation Power: 886
Woops! Let me go lick my wounds....

Well, at least my advice about not using malloc() paid off .

Reply With Quote
  #9  
Old May 13th, 2004, 05:09 AM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 3,398 jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 6 h 48 m 17 sec
Reputation Power: 886
Lets see...

Code:
  #ifdef DO_IT_THIS_WAY
typedef RC* pRC ; pRC pp = new pRC[NUMBEROF] ;
#else
RC *pp = new RC*[NUMBEROF] ;
#endif


I must have had 2D arrays on my brain. I apologize for the confusion. I think it was the pp moniker that had me over-thinking the problem. An array of pointers to struct might be better named ppRC. But that's just a nit.

Last edited by jwdonahue : May 13th, 2004 at 05:12 AM. Reason: Fixed indentation.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > allocating memory for an array structure member

Developer Shed Advertisers and Affiliates



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

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap