C Help please
Can some one pl. give me some pointers, my code works fine but crashes when I free the memory. any help is appreciated. I''m using a MSC VC++ compiler
thanks...
int main()
{
int valBase10 = 0;
char *hex = (char *)malloc(sizeof(100));
printf("Enter the Digit in Hex : ");
scanf("%s", &hex);
printf("The conversion is %i",Hex2Base10(&hex));
if(hex)free(&hex);
return 0;
}
You shouldn''t be passing the address of hex to the free function, just hex. So change free(&hex) to free(hex) and it should work.
-YoshiXGXCX ''99
I''ll be honest, I haven''t had to look at C code in a long time, but seeing as you''re writing in msvc++, why don''t you use their brand new memory management features. their safer and faster.
instead of :
char *hex = (char *)malloc(sizeof(100));
use:
char * hex = new char[100];
then:
memset(hex,0,sizeof(char)*100);
To free the memory use:
delete [] hex;
As to your code in particular, you problem seems to be how you are using the pointer,
scanf("%s", &hex);
scanf''s second arg wants the location to write to, the location is the pointers value, so you should write like this:
scanf("%s", hex);
after changing that problem, change your memory code to use "new" and "delete" if it still doesn''t work, post the new code and i''ll debug the memory stuff for you.
i hope that works
instead of :
char *hex = (char *)malloc(sizeof(100));
use:
char * hex = new char[100];
then:
memset(hex,0,sizeof(char)*100);
To free the memory use:
delete [] hex;
As to your code in particular, you problem seems to be how you are using the pointer,
scanf("%s", &hex);
scanf''s second arg wants the location to write to, the location is the pointers value, so you should write like this:
scanf("%s", hex);
after changing that problem, change your memory code to use "new" and "delete" if it still doesn''t work, post the new code and i''ll debug the memory stuff for you.
i hope that works
YoshiN: Actually I did try free(hex) as well, same problem.
TheLameDuck:Appreciate your detailed response, my constraint is I''ve to code in C, otherwise C++ in this scenario is easier, though I''m using msvc++, I need to write it in C.
like you pointed out if I change scanf("%s", hex); the program does something totally different and crashes. but when I do scanf("%s", &hex); then it computer the correct value and then crashes.
thanks
TheLameDuck:Appreciate your detailed response, my constraint is I''ve to code in C, otherwise C++ in this scenario is easier, though I''m using msvc++, I need to write it in C.
like you pointed out if I change scanf("%s", hex); the program does something totally different and crashes. but when I do scanf("%s", &hex); then it computer the correct value and then crashes.
thanks
scanf("%s", &hex);
this should be
scanf("%s", hex);
You don''t want to write to the location in memory where the pointer is, but where it points to. You are overriding the pointers value thus you cannot free the memory
this should be
scanf("%s", hex);
You don''t want to write to the location in memory where the pointer is, but where it points to. You are overriding the pointers value thus you cannot free the memory
Try changing:
char *hex = (char *)malloc(sizeof(100));
to
char *hex = (char *)malloc(100);
And it should be (I think) as TheLameDuck said:
scanf("%s", hex);
char *hex = (char *)malloc(sizeof(100));
to
char *hex = (char *)malloc(100);
And it should be (I think) as TheLameDuck said:
scanf("%s", hex);
February 23, 2003 11:56 AM
if you already know hex is an array of length 100, why not just do this?
char hex[100];
You won''t need to worry about freeing the memory in this case, otherwise if you don''t know the size of the array before hand, do this:
(assuming "int x" is define previously with the size of the array)
char *hex = (char *)malloc(x);
...
free(hex);
char hex[100];
You won''t need to worry about freeing the memory in this case, otherwise if you don''t know the size of the array before hand, do this:
(assuming "int x" is define previously with the size of the array)
char *hex = (char *)malloc(x);
...
free(hex);
quote: Original post by skrovi
Can some one pl. give me some pointers, my code works fine but crashes when I free the memory. any help is appreciated. I''m using a MSC VC++ compiler
thanks...
int main()
{
int valBase10 = 0;
char *hex = (char *)malloc(sizeof(100));
printf("Enter the Digit in Hex : ");
scanf("%s", hex);
printf("The conversion is %i",Hex2Base10(&hex));
if(hex)free(&hex);
return 0;
}
int main (){ int valBase10 = 0; char *hex = (char *)malloc(100 * sizeof(char)); //char *hex = (char *)calloc(100, sizeof(char)); printf ("Enter the Hex Digit here: "); gets(hex); printf("The conversion is %d.", Hex2Base10(hex)); if (hex != NULL) free(hex); return 0;}
when you using malloc() it''s safer (though in your specific case not necessary) to take the sizeof() the data type you''re using and mulitplying by the number you need.
also it''s safe(r) to use calloc instead of malloc only because calloc zeroes the "array" which means there won''t be any garbage in it.
use gets() instead of scanf for string input. easiest reason: less typing
for good measure just check to see if the pointer is NULL before freeing it.
in printf %i and %d are the same thing
This is the code to convert hexadecimal to decimal and it works...thanks to all who help me fix it....
cheers.......
#include <stdio.h>
#include <cmath>
int Hex2Base10(const char* pHex)
{
int base10 = 0;
int exponent = 0;
const char* pChar = pHex;
while (*pChar != ''\0'')
{
exponent++;
pChar++;
}
pChar = pHex;
while (*pChar)
{
int digit;
exponent--;
if ((*pChar >= ''0'') && (*pChar <= ''9''))
digit = *pChar - ''0'';
else if ((*pChar >= ''A'') && (*pChar <= ''F''))
digit = 10 + *pChar - ''A'';
else if ((*pChar >= ''a'') && (*pChar <= ''f''))
digit = 10 + *pChar - ''a'';
else
return -1; // error!
base10 += digit * pow(16, exponent);
pChar++;
}
return base10;
}
int main()
{
int valBase10 = 0;
char *hex = (char *)malloc(100);
printf("Enter the Digit in Hex : ");
scanf("%s", hex);
printf("The conversion is %i\n",Hex2Base10(hex));
if(hex)free(hex);
return 0;
}
cheers.......
#include <stdio.h>
#include <cmath>
int Hex2Base10(const char* pHex)
{
int base10 = 0;
int exponent = 0;
const char* pChar = pHex;
while (*pChar != ''\0'')
{
exponent++;
pChar++;
}
pChar = pHex;
while (*pChar)
{
int digit;
exponent--;
if ((*pChar >= ''0'') && (*pChar <= ''9''))
digit = *pChar - ''0'';
else if ((*pChar >= ''A'') && (*pChar <= ''F''))
digit = 10 + *pChar - ''A'';
else if ((*pChar >= ''a'') && (*pChar <= ''f''))
digit = 10 + *pChar - ''a'';
else
return -1; // error!
base10 += digit * pow(16, exponent);
pChar++;
}
return base10;
}
int main()
{
int valBase10 = 0;
char *hex = (char *)malloc(100);
printf("Enter the Digit in Hex : ");
scanf("%s", hex);
printf("The conversion is %i\n",Hex2Base10(hex));
if(hex)free(hex);
return 0;
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement