Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Is My Program OK? (http://www.velocityreviews.com/forums/t593616-is-my-program-ok.html)

Andy G. 02-23-2008 08:26 AM

Is My Program OK?
 
Greetings!

I am learning C and I came up with this program to test my knowledge.

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char s[128];
void *p;
sprintf(s, "%p", malloc(0x10));
puts(s);
sscanf(s, "%p", &p);
printf("%p\n", p);
free(p);
return 0;
}

Is it OK? Even if malloc returns NULL?

One thing I am not sure of is what the size of s should be. Is 128
enough? Or too much? How should I know how big to make it?

Thanks in advance -- Andy!

vippstar@gmail.com 02-23-2008 08:35 AM

Re: Is My Program OK?
 
On Feb 23, 10:26 am, "Andy G." <andyg...@spamcop.net> wrote:
> Greetings!
>
> I am learning C and I came up with this program to test my knowledge.
>
> #include <stdio.h>
> #include <stdlib.h>
> int main(void)
> {
> char s[128];
> void *p;
> sprintf(s, "%p", malloc(0x10));
> puts(s);
> sscanf(s, "%p", &p);
> printf("%p\n", p);
> free(p);
> return 0;
>
> }
>
> Is it OK? Even if malloc returns NULL?

Yes, except the sprintf() part.
> One thing I am not sure of is what the size of s should be. Is 128
> enough? Or too much? How should I know how big to make it?

You cannot know whether it will fit or not.
You should instead use snprintf() and check the return value.
Also, save the pointer somewhere, in case it does not fit in the
buffer so you can free it.


Vitillo Roberto 02-23-2008 11:07 AM

Re: Is My Program OK?
 
> You cannot know whether it will fit or not.
Are you sure? This should do the trick:

int mod = -1;
int len = ((mod = sizeof(void*) % sizeof(char)) == 0) ? sizeof(void*)/
sizeof(char) : sizeof(void*)/sizeof(char)+1;
char s[len];

Bartc 02-23-2008 11:13 AM

Re: Is My Program OK?
 

"Vitillo Roberto" <r.vitillo@gmail.com> wrote in message
news:1ad52cfa-4dac-4723-abaf-78920267f1f4@p73g2000hsd.googlegroups.com...
>> You cannot know whether it will fit or not.

> Are you sure? This should do the trick:
>
> int mod = -1;
> int len = ((mod = sizeof(void*) % sizeof(char)) == 0) ? sizeof(void*)/
> sizeof(char) : sizeof(void*)/sizeof(char)+1;
> char s[len];


I think if my life depended on getting len absolutely right, I would rather
take my chances with len=128.

Especially as your code seems to give len=4 :-)

--
Bart



Roberto V. 02-23-2008 11:45 AM

Re: Is My Program OK?
 
Ups...
int mod = -1;
int len = ((mod = sizeof(void*)*2 % sizeof(char)) == 0) ?
sizeof(void*)*2/sizeof(char) : sizeof(void*)*2/sizeof(char)+1;
char s[len+1];

Better now? ;)

vippstar@gmail.com 02-23-2008 12:07 PM

Re: Is My Program OK?
 
On Feb 23, 1:45 pm, "Roberto V." <r.viti...@gmail.com> wrote:
> Ups...
> int mod = -1;
> int len = ((mod = sizeof(void*)*2 % sizeof(char)) == 0) ?
> sizeof(void*)*2/sizeof(char) : sizeof(void*)*2/sizeof(char)+1;
> char s[len+1];
>
> Better now? ;)


What logic is that?
Do you have any understanding of the problem I addressed?
sizeof(void *)*2 % sizeof(char) will always 0 because sizeof(char) is
guaranteed to be 1. N % 1 == 0.
So, we completely ignore the first case, and let's see what's after
the ':'
sizeof(void *) * 2 / sizeof (char) + 1
Let us assume sizeof (void *) is 4. 4 * 2 / 1 + 1 = 8 / 1 + 1 = 8 + 1
= 9.
I think I start to understand your logic (which, alas is still flawed)
You want to calculate the N of bytes in the pointer size, so you can
'predict' the size of it's hex representation.
Ofcourse, the mistake here is that you *assume* the representation
will be in hex.
You also assume that EVEN if it's in hex, 0x will not be prefixed.
0xHHHHHHHH is 11 in size and not 9.

You *cannot* know the size.

santosh 02-23-2008 12:33 PM

Re: Is My Program OK?
 
vippstar@gmail.com wrote:

> On Feb 23, 10:26 am, "Andy G." <andyg...@spamcop.net> wrote:
>> Greetings!
>>
>> I am learning C and I came up with this program to test my knowledge.
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>> int main(void)
>> {
>> char s[128];
>> void *p;
>> sprintf(s, "%p", malloc(0x10));
>> puts(s);
>> sscanf(s, "%p", &p);
>> printf("%p\n", p);
>> free(p);
>> return 0;
>>
>> }
>>
>> Is it OK? Even if malloc returns NULL?

> Yes, except the sprintf() part.
>> One thing I am not sure of is what the size of s should be. Is 128
>> enough? Or too much? How should I know how big to make it?

> You cannot know whether it will fit or not.
> You should instead use snprintf() and check the return value.
> Also, save the pointer somewhere, in case it does not fit in the
> buffer so you can free it.


One more minor point is to cast the argument to printf to a void *,
since the %p specifier is defined as accepting a void pointer value
only.

Maybe also change 's' to unsigned char[].


vippstar@gmail.com 02-23-2008 01:14 PM

Re: Is My Program OK?
 
On Feb 23, 2:33 pm, santosh <santosh....@gmail.com> wrote:
> vipps...@gmail.com wrote:
> > On Feb 23, 10:26 am, "Andy G." <andyg...@spamcop.net> wrote:
> >> Greetings!

>
> >> I am learning C and I came up with this program to test my knowledge.

>
> >> #include <stdio.h>
> >> #include <stdlib.h>
> >> int main(void)
> >> {
> >> char s[128];
> >> void *p;
> >> sprintf(s, "%p", malloc(0x10));
> >> puts(s);
> >> sscanf(s, "%p", &p);
> >> printf("%p\n", p);
> >> free(p);
> >> return 0;

>
> >> }

>
> >> Is it OK? Even if malloc returns NULL?

> > Yes, except the sprintf() part.
> >> One thing I am not sure of is what the size of s should be. Is 128
> >> enough? Or too much? How should I know how big to make it?

> > You cannot know whether it will fit or not.
> > You should instead use snprintf() and check the return value.
> > Also, save the pointer somewhere, in case it does not fit in the
> > buffer so you can free it.

>
> One more minor point is to cast the argument to printf to a void *,
> since the %p specifier is defined as accepting a void pointer value
> only.

That is not needed in the source mr Andy G. posted.

Ben Bacarisse 02-23-2008 01:20 PM

Re: Is My Program OK?
 
santosh <santosh.k83@gmail.com> writes:

> vippstar@gmail.com wrote:
>
>> On Feb 23, 10:26 am, "Andy G." <andyg...@spamcop.net> wrote:

<snip>
>>> #include <stdio.h>
>>> #include <stdlib.h>
>>> int main(void)
>>> {
>>> char s[128];
>>> void *p;
>>> sprintf(s, "%p", malloc(0x10));
>>> puts(s);
>>> sscanf(s, "%p", &p);
>>> printf("%p\n", p);
>>> free(p);
>>> return 0;
>>> }
>>>
>>> Is it OK? Even if malloc returns NULL?

<snip>
> One more minor point is to cast the argument to printf to a void *,
> since the %p specifier is defined as accepting a void pointer value
> only.


In both cases it is a void * that is passed.

--
Ben.

Ben Bacarisse 02-23-2008 01:24 PM

Re: Is My Program OK?
 
Vitillo Roberto <r.vitillo@gmail.com> writes:

>> You cannot know whether it will fit or not.

> Are you sure? This should do the trick:
>
> int mod = -1;
> int len = ((mod = sizeof(void*) % sizeof(char)) == 0) ? sizeof(void*)/
> sizeof(char) : sizeof(void*)/sizeof(char)+1;
> char s[len];


Nothing about sizeof(void *) can tell you the required output size.
It could tell you something about the possible number of different
pointer values, but that is not the same thing. %p might produce:

<segment: 0x2345 offset: 2998788765 in cluster main:24>

who knows?

--
Ben.


All times are GMT. The time now is 06:39 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.