2

I found similar questions to mine, but I still can't figure out why my code is acting the way it is.

I have a struct called "rectangle", and here is the code that I am trying to run:

Rectangle *newRect = (Rectangle *)malloc(sizeof(Rectangle));
Rectangle *newRect2 = (Rectangle *)malloc(sizeof(Rectangle));
printf("rect1: %p rect2: %p",newRect,newRect2);

It outputs the same address for both of them, what am I doing wrong?

Thanks!

  • 1
    You do not need the cast – Ed Heal Oct 02 '14 at 05:56
  • 5
    You are either a) not running that exact code you posted or b) freeing `newRect` before the second call to `malloc`. – user703016 Oct 02 '14 at 05:57
  • 2
    Please show us an SSCCE that exhibits this behaviour: http://sscce.org/ – NPE Oct 02 '14 at 05:58
  • @EdHeal it's good practice, it's required in C++, and it's often a warning in C. – o11c Oct 02 '14 at 05:59
  • 1
    @o11c But question tagged in `C`.I don't think it's good.[You can check yourself](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Jayesh Bhoi Oct 02 '14 at 06:01
  • 2
    @o11c - You use new in C++ not malloc. C does not require the cast nor is it good practice – Ed Heal Oct 02 '14 at 06:02
  • 2
    @o11c no, it is not good practice. neither is a C-typecast in C++ good practice. – AndersK Oct 02 '14 at 06:06
  • can you check how mush is sizeof(Rectangle) – Mendi Barel Oct 02 '14 at 06:10
  • 1
    @EdHeal you most certainly do not use `new` just because it's C++; the semantics are not compatible. And it's quite important for a lot of code to be compatible with C++ as well as C. If anything, you would use a macro that's ifdef'ed to use `static_cast(p)` in C++ and `(T *)p` (glibc has a private one). – o11c Oct 02 '14 at 06:17
  • sizeof(Rectangle) = 48 – Greg Brinker Oct 02 '14 at 06:19
  • @o11c - You do as C++ is a different beast to C. It also avoids a whole lot of problems with programmers having to decide if they should use free/delete. As to using macors in C++ that is another can of worms than is best avoided – Ed Heal Oct 02 '14 at 06:19
  • @GregBrinker - What is the output? – Ed Heal Oct 02 '14 at 06:20
  • 1
    @EdHeal Rules about writing C++ from scratch do not apply to existing C legacy codebases. During a conversion , `new` and `delete` are exceptionally rare, and when they do exist they are wrapped in some sort of (non-smart) pointer class. And when used *carefully*, macros are capable of much more obviousness and flexibility than other code. I'm just about to rewrite some code that uses a template and lambdas to use a macro instead, in fact. – o11c Oct 02 '14 at 06:26
  • have you tried to change the pointer to `int*` or so? something like https://ideone.com/TKCYjQ – mch Oct 02 '14 at 06:29
  • it outputs Root rect ptr: 0x7f828ad00000 New rect ptr: 0x7f828ad00000 – Greg Brinker Oct 02 '14 at 06:35
  • As @NPE already has requested, please post a minimal but complete code example that produces your output, and also please tell us the specification of your platform, OS, compiler etc. – Jens Gustedt Oct 02 '14 at 09:51
  • 1
    Not shown earlier code corrupted memory. – chux - Reinstate Monica Oct 03 '14 at 03:39

1 Answers1

2

You probably don't compile with all warnings on, and forgot to include stdlib.h. malloc is then by some compilers interpreted to return int instead of void*. On a 64 bit architecture this looses significant information and thus at the end you see the same value in your print.

Don't cast the return of malloc.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177