Jump to content

help with printing in reverse with realloc

- - - - -

  • Please log in to reply
7 replies to this topic

#1
pointish

pointish

    Newbie

  • Members
  • Pip
  • 3 posts
qqq

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


#2
voral

voral

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
1.
not right:

Quote

scanf("%d", j);

right:

Quote

scanf("%d", &j);


#3
pointish

pointish

    Newbie

  • Members
  • Pip
  • 3 posts
yex tnx have to remember this things

#4
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts
  • Location:Missouri
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.
"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

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts

Sysop_fb said:

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

#6
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts
  • Location:Missouri
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
"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

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Yeah, right. Yeah, right. I did not think about it.

#8
pointish

pointish

    Newbie

  • Members
  • Pip
  • 3 posts
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?!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users