Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

help with printing in reverse with realloc

realloc printing

  • Please log in to reply
7 replies to this topic

#1 pointish

pointish

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 27 January 2012 - 05:22 AM

qqq

Edited by pointish, 27 January 2012 - 09:32 AM.

  • 0

#2 voral

voral

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 27 January 2012 - 05:56 AM

1.
not right:

scanf("%d", j);


right:

scanf("%d", &j);


  • 0

#3 pointish

pointish

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 27 January 2012 - 06:01 AM

yex tnx have to remember this things
  • 0

#4 Sysop_fb

Sysop_fb

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 151 posts
  • Location:Missouri

Posted 27 January 2012 - 06:22 AM

Also
string = realloc(string, capacity);
Using the pointer to your old memory to hold the return value of a realloc call is bad form, if that call fails then realloc isn't required to clean up the old allocated memory and you just lost your pointer to it so now you have a memory leak. Most people will use a temp pointer to hold the realloc call to check if it's null and then move your original pointer to the temp one.

Type casting malloc and realloc is pretty useless as well since C does type coercion from void to any other type automatically.
  • 0
"The best optimizer is between your ears" - Michael Abrash
Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.

#5 voral

voral

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 27 January 2012 - 06:36 AM

Type casting malloc and realloc is pretty useless as well since C does type coercion from void to any other type automatically.

I think it is useful for code clarity
  • 0

#6 Sysop_fb

Sysop_fb

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 151 posts
  • Location:Missouri

Posted 27 January 2012 - 06:48 AM

Yeah if you need clarity it can help if you know what you're doing.
It can cause problems as well and I usually recommend newer programmers to stay away from it. Depending on the compiler you're using if you cast malloc and forget to include the stdlib.h header file you could end up with broken code that compiles. Personally I'd just add a comment block if I need clarity that bad over telling the compiler that you're smarter than it via casting when it's not necessary.

FAQ: Casting malloc?
Question 7.7
  • 0
"The best optimizer is between your ears" - Michael Abrash
Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.

#7 voral

voral

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 27 January 2012 - 06:59 AM

Yeah, right. Yeah, right. I did not think about it.
  • 0

#8 pointish

pointish

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 27 January 2012 - 07:22 AM

hi i did some changes, is there a way to check that the memory allocation is succeeded?
because when i wright very big numbers it just ends the prog without gvining a message that allocation failed?!
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download