The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
allocating memory for an array structure member
Discuss allocating memory for an array structure member in the C Programming forum on Dev Shed. allocating memory for an array structure member C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 21st, 2004, 10:17 PM
|
|
Registered User
|
|
Join Date: Jan 2004
Posts: 13
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
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.
|

April 21st, 2004, 10:57 PM
|
|
Dinesh_P_V
|
|
Join Date: Aug 2003
Location: India
Posts: 259
Time spent in forums: 12 h 1 m 3 sec
Reputation Power: 0
|
|
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
|

May 12th, 2004, 10:45 AM
|
|
Registered User
|
|
Join Date: May 2004
Posts: 2
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
|

May 12th, 2004, 11:13 AM
|
|
Registered User
|
|
Join Date: May 2004
Location: Groningen, The Netherlands (Europe)
Posts: 4
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)
|

May 12th, 2004, 11:02 PM
|
|
Dinesh_P_V
|
|
Join Date: Aug 2003
Location: India
Posts: 259
Time spent in forums: 12 h 1 m 3 sec
Reputation Power: 0
|
|
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
|

May 12th, 2004, 11:30 PM
|
 |
Bellevue WA, USA
|
|
Join Date: May 2004
Location: Bellevue Washington, USA
|
|
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/
|

May 13th, 2004, 04:49 AM
|
|
Registered User
|
|
Join Date: May 2004
Posts: 2
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
|

May 13th, 2004, 04:59 AM
|
 |
Bellevue WA, USA
|
|
Join Date: May 2004
Location: Bellevue Washington, USA
|
|
Woops! Let me go lick my wounds....
Well, at least my advice about not using malloc() paid off  .
|

May 13th, 2004, 05:09 AM
|
 |
Bellevue WA, USA
|
|
Join Date: May 2004
Location: Bellevue Washington, USA
|
|
Lets see...
Code:
#ifdef DO_IT_THIS_WAYtypedef 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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|