Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C equivalent of C++ new()?

Reply
Thread Tools

C equivalent of C++ new()?

 
 
Steve555
Guest
Posts: n/a
 
      10-14-2010
Hi

I've been working with C++ and Objective-C and I've forgotten how to
allocate memory for an array of structs in C.

Given a struct:

typedef struct {
double a,b,c,d;
long e,f,g,h;
} MyStruct


I want to do the equivalent of :

MyStruct **myStructArray;
myStructArray = new MyStruct*[1024];
for(long i=0; i<1024; i++){
myStructArray[i] = new MyStruct;
}
so that I can access them by index:

double a = myStructArray[1]->a;


I'm aware of malloc as a simple lump of memory, but don't understand
how it would be aware of the boundaries of each struct and each
element.

Thanks

Steve
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      10-14-2010
On 2010-10-14, Steve555 <> wrote:
> I'm aware of malloc as a simple lump of memory, but don't understand
> how it would be aware of the boundaries of each struct and each
> element.


It wouldn't, nor would it need to be.

The boundaries of the structures and elements come from the type of
the pointer you use to hold the address returned from malloc().

C++ needs constructors and thus allocators care about types. C doesn't
have constructors, so there's no type to allocation -- you just get
a block of memory.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
 
 
 
Jon
Guest
Posts: n/a
 
      10-14-2010
Seebs wrote:
> On 2010-10-14, Steve555 <> wrote:
>> I'm aware of malloc as a simple lump of memory, but don't understand
>> how it would be aware of the boundaries of each struct and each
>> element.

>
> It wouldn't, nor would it need to be.
>
> The boundaries of the structures and elements come from the type of
> the pointer you use to hold the address returned from malloc().
>
> C++ needs constructors and thus allocators care about types. C
> doesn't have constructors, so there's no type to allocation -- you
> just get
> a block of memory.
>


You tell him Seebs. (He's your "straight man", c'mon, lame comedy).


 
Reply With Quote
 
Steve555
Guest
Posts: n/a
 
      10-14-2010
On 14 Oct, 07:39, Seebs <usenet-nos...@seebs.net> wrote:
> On 2010-10-14, Steve555 <foursh...@btinternet.com> wrote:
>
> > I'm aware of malloc as a simple lump of memory, but don't understand
> > how it would be aware of the boundaries of each struct and each
> > element.

>
> It wouldn't, nor would it need to be.
>
> The boundaries of the structures and elements come from the type of
> the pointer you use to hold the address returned from malloc().
>
> C++ needs constructors and thus allocators care about types. *C doesn't
> have constructors, so there's no type to allocation -- you just get
> a block of memory.
>
> -s
> --
> Copyright 2010, all wrongs reversed. *Peter Seebach / usenet-nos...@seebs.nethttp://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
> I am not speaking for my employer, although they do rent some of my opinions.


Thanks, I wasn't sure if that was the case.
But I still can't get my head around the fact it's pointers of
pointers:

Would this be correct, based on what you said:

myStructArray = (MyStruct*)malloc(1024 * sizeof(MyStruct*))
for(long i=0; i<1024; i++){
myStructArray[i] = malloc(sizeof(MyStruct));
}


 
Reply With Quote
 
Steve555
Guest
Posts: n/a
 
      10-14-2010
On 14 Oct, 07:46, Project China Blue Book <chine.b...@yahoo.com>
wrote:
> In article <8bc0d55d-db30-45c7-83fd-880fb82f5...@a36g2000yqc.googlegroups..com>,
>
> *Steve555 <foursh...@btinternet.com> wrote:
> > I've been working with C++ and Objective-C and I've forgotten how to
> > allocate memory for an array of structs in C.
> > MyStruct * **myStructArray;
> > myStructArray = new MyStruct*[1024];

>
> This is an array of struct pointers, not an array of structs.
>
> > for(long i=0; i<1024; i++){
> > * * myStructArray[i] = new MyStruct;
> > }
> > so that I can access them by index:

>
> > double a = myStructArray[1]->a;

>
> myStructArray = malloc(1024*sizeof(MyStruct));
> ...
> * * myStructArray[i] = malloc(sizeof(MyStruct));
>
> > I'm aware of malloc as a simple lump of memory, but don't understand
> > how it would be aware of the boundaries of each struct and each
> > element.

>
> It doesn't need to be aware. You allocate a separate block for each struct as a
> pointer, and one block for the array of pointers. The address arithmetic picks
> the correct pointer out of the array, assuming you don't lie about the array
> size, and the pointer deref gets the struct value.
>
> If you want to allocate an array of structs, that's just
> * * MyStruct **myStructArray = malloc(1024*sizeof(MyStruct));
> * * ...
> * * double a = myStructArray[1].a;
>
> --
> Damn the living - It's a lovely life. * * * * * I'm whoever you want me to be.
> Silver silverware - Where is the love? * * * At least I can stay in character.
> Oval swimming pool - Where is the love? * *Annoying Usenet one post at a time.
> Damn the living - It's a lovely life. * * May your creator bless and keep you.


double a = myStructArray[1].a;

Thanks, but in case I didn't explain well enough, the crucial point is
that I need pointers:

double a = myStructArray[1]->a;

I'm passing large structures by pointer to time-critical functions to
save the overhead of copying.
 
Reply With Quote
 
Steve555
Guest
Posts: n/a
 
      10-14-2010
On 14 Oct, 07:56, Steve555 <foursh...@btinternet.com> wrote:
> On 14 Oct, 07:46, Project China Blue Book <chine.b...@yahoo.com>
> wrote:
>
>
>
>
>
> > In article <8bc0d55d-db30-45c7-83fd-880fb82f5...@a36g2000yqc.googlegroups.com>,

>
> > *Steve555 <foursh...@btinternet.com> wrote:
> > > I've been working with C++ and Objective-C and I've forgotten how to
> > > allocate memory for an array of structs in C.
> > > MyStruct * **myStructArray;
> > > myStructArray = new MyStruct*[1024];

>
> > This is an array of struct pointers, not an array of structs.

>
> > > for(long i=0; i<1024; i++){
> > > * * myStructArray[i] = new MyStruct;
> > > }
> > > so that I can access them by index:

>
> > > double a = myStructArray[1]->a;

>
> > myStructArray = malloc(1024*sizeof(MyStruct));
> > ...
> > * * myStructArray[i] = malloc(sizeof(MyStruct));

>
> > > I'm aware of malloc as a simple lump of memory, but don't understand
> > > how it would be aware of the boundaries of each struct and each
> > > element.

>
> > It doesn't need to be aware. You allocate a separate block for each struct as a
> > pointer, and one block for the array of pointers. The address arithmetic picks
> > the correct pointer out of the array, assuming you don't lie about the array
> > size, and the pointer deref gets the struct value.

>
> > If you want to allocate an array of structs, that's just
> > * * MyStruct **myStructArray = malloc(1024*sizeof(MyStruct));
> > * * ...
> > * * double a = myStructArray[1].a;

>
> > --
> > Damn the living - It's a lovely life. * * * * * I'm whoever you want me to be.
> > Silver silverware - Where is the love? * * * At least I can stay in character.
> > Oval swimming pool - Where is the love? * *Annoying Usenet one post at a time.
> > Damn the living - It's a lovely life. * * May your creator bless and keep you.

>
> double a = myStructArray[1].a;
>
> Thanks, but in case I didn't explain well enough, the crucial point is
> that I need pointers:
>
> double a = myStructArray[1]->a;
>
> I'm passing large structures by pointer to time-critical functions to
> save the overhead of copying.


MyStruct **myStructArray;
myStructArray = (MyStruct*)malloc(1024 * sizeof(MyStruct*))
for(long i=0; i<1024; i++){
myStructArray[i] = malloc(sizeof(MyStruct));
myStructArray[i]->g = i;
}
printf("%d", myStructArray[123]->g);

OK, well that *seems* to work, but as with all memory problems, I
might just have got lucky.
If any kind soul spots a lurking catastrophe, I'd appreciate a heads-
up


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-14-2010
On 10/14/10 08:55 PM, Steve555 wrote:
>
> MyStruct **myStructArray;
> myStructArray = (MyStruct*)malloc(1024 * sizeof(MyStruct*))


Those two lines can be simplified to

MyStruct **myStructArray = malloc(1024 * sizeof(MyStruct*));

> for(long i=0; i<1024; i++){
> myStructArray[i] = malloc(sizeof(MyStruct));
> myStructArray[i]->g = i;
> }
> printf("%d", myStructArray[123]->g);
>
> OK, well that *seems* to work, but as with all memory problems, I
> might just have got lucky.
> If any kind soul spots a lurking catastrophe, I'd appreciate a heads-
> up


Just don't forget to free them all when you are done.

--
Ian Collins
 
Reply With Quote
 
Steve555
Guest
Posts: n/a
 
      10-14-2010
On 14 Oct, 09:04, Ian Collins <ian-n...@hotmail.com> wrote:
> On 10/14/10 08:55 PM, Steve555 wrote:
>
>
>
> > MyStruct * **myStructArray;
> > myStructArray = (MyStruct*)malloc(1024 * sizeof(MyStruct*))

>
> Those two lines can be simplified to
>
> MyStruct **myStructArray = malloc(1024 * sizeof(MyStruct*));
>
> > for(long i=0; i<1024; i++){
> > * * *myStructArray[i] = malloc(sizeof(MyStruct));
> > * * *myStructArray[i]->g = i;
> > }
> > printf("%d", myStructArray[123]->g);

>
> > OK, well that *seems* to work, but as with all memory problems, I
> > might just have got lucky.
> > If any kind soul spots a lurking catastrophe, I'd appreciate a heads-
> > up

>
> Just don't forget to free them all when you are done.
>
> --
> Ian Collins


Many thanks Ian
 
Reply With Quote
 
Alexander Klauer
Guest
Posts: n/a
 
      10-14-2010
Steve555 wrote:
> MyStruct **myStructArray;
> myStructArray = (MyStruct*)malloc(1024 * sizeof(MyStruct*))
> for(long i=0; i<1024; i++){
> myStructArray[i] = malloc(sizeof(MyStruct));
> myStructArray[i]->g = i;
> }
> printf("%d", myStructArray[123]->g);
>
> OK, well that *seems* to work, but as with all memory problems, I
> might just have got lucky.
> If any kind soul spots a lurking catastrophe, I'd appreciate a heads-
> up


Please do not forget to check explicitly whether your malloc()s succeeded.
In your case, malloc() has failed if and only if it has returned a null
pointer (this is quite unlike C++, where "new" never yields a null
pointer).
 
Reply With Quote
 
lawrence.jones@siemens.com
Guest
Posts: n/a
 
      10-14-2010
Steve555 <> wrote:
>
> MyStruct **myStructArray;
> myStructArray = (MyStruct*)malloc(1024 * sizeof(MyStruct*))
> for(long i=0; i<1024; i++){
> myStructArray[i] = malloc(sizeof(MyStruct));
> myStructArray[i]->g = i;
> }
> printf("%d", myStructArray[123]->g);
>
> OK, well that *seems* to work, but as with all memory problems, I
> might just have got lucky.


That works, but it's rather inefficient. You can allocate all the
structs in a single block instead:

MyStruct *myStructArray = malloc(1024 * sizeof *myStructArray);
for (int i = 0; i < 1024; i++) {
myStructArray[i].g = i;
}
printf("%d\n", myStructArray[123].g;

If you need pointers, use the "&" operator:

myFunc(&myStructArray[42]);
--
Larry Jones

Wow, how existential can you get? -- Hobbes
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Warning:Xst:382 - Register A is equivalent to B mag VHDL 1 05-19-2005 05:10 PM
instancename of current entity/architecture -- equivalent to C++ this??? Eric Peers VHDL 2 11-18-2004 05:23 PM
VHDL equivalent of verilog trireg Sanjeev VHDL 4 07-23-2004 09:55 AM
equivalent types in different packages Lolo VHDL 3 09-22-2003 03:23 PM
Re: Image Scanning - TWAIN equivalent Brendan Duffy ASP .Net 0 07-24-2003 08:29 AM



Advertisments