Re: obj function hello()



On Feb 9, 9:41 pm, Christopher Benson-Manica
<a...@xxxxxxxxxxxxxxxxxxx> wrote:
gert <gert.cuyk...@xxxxxxxxx> wrote:
I modified the example a bit to give it a more oo look but i still
having trouble understanding the memory allocation part ?
Obj* myobj = malloc(sizeof *myobj);
for me it would make more sense if i would wright
Obj* myobj = malloc(sizeof myobj);

Why? myobj is a *pointer* - you want enough memory for myobj to point
to an Obj, not however much memory the pointer takes up. This is a
crucial difference.

The thing is they both give the same output :)

Only because you seem to be lucky - the second version is wrong, and
either happens to allocate enough space for an Obj (implying
sizeof(Obj) < sizeof(Obj*)) or accesses memory it has no right to
access.


Ok i see, i have to allocate memory for myobj not the pointer it self


.



Relevant Pages

  • Re: Is There Any Reason to Even Use VC++ Anymore?
    ... If, for another reason, the calling function needs to allocate memory, ... It does this by taking a pointer to a ball object ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Pointer to the out of scope local variables
    ... of a pointer, and jumped to the conclusion that you allocated the memory ... In this particular case the funtion buildPoint would allocate ... If the caller ...
    (microsoft.public.vc.language)
  • Re: Memory management and allocation
    ... > As I'm writing a piece of code that basically acts as a server and ... > memory management is a topic that is quite crucial. ... Or can I just allocate the variable ... Nor is it usually necessary to set the pointer to ...
    (comp.lang.c)
  • Re: Virtual Machine implementation problem, Please help me to spot the bug
    ... I kept on getting error messages. ... You don't allocate enough memory here. ... Again conversions between pointer and integer types. ...
    (comp.lang.c)
  • Re: This is getting really weird.
    ... I thought 4 bytes for reference count and 4 for string length. ... > There should be no memory allocation for that line. ... > manager may allocate more space than requested for its own efficiency. ... > that New returned with a pointer to the string constant. ...
    (alt.comp.lang.borland-delphi)