Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with Char*

Ashit Vora
Guest
 
Posts: n/a
#1: Sep 5 '08
I friends,
I 'm facing a weird prob.

the code is

int total_buf_size=100;
char *buf;
int type= 0xFE10;

buf =(char*) malloc (total_buf_size);

memcpy(buf,&type, 10);
printf("Buf:: %s\n",buf);

The printf doesnt print anything but just "Buf::"

can anyone pls help me out resolve this???
Thanks a ton

Richard Heathfield
Guest
 
Posts: n/a
#2: Sep 5 '08

re: Problem with Char*


Ashit Vora said:
Quote:
the code is
>
int total_buf_size=100;
char *buf;
int type= 0xFE10;
>
buf =(char*) malloc (total_buf_size);
>
memcpy(buf,&type, 10);
printf("Buf:: %s\n",buf);
No, it isn't. The above will not even compile. Presumably you have some
real code that you can compile. Why not show that to us? If you can't find
the problem yourself, full disclosure is your best strategy.
Quote:
The printf doesnt print anything but just "Buf::"
What did you expect it to print, and why?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
eldredge.n@gmail.com
Guest
 
Posts: n/a
#3: Sep 5 '08

re: Problem with Char*


On Sep 4, 10:59 pm, Ashit Vora <a.k.v...@gmail.comwrote:
Quote:
I friends,
I 'm facing a weird prob.
>
the code is
>
int total_buf_size=100;
char *buf;
int type= 0xFE10;
>
buf =(char*) malloc (total_buf_size);
>
memcpy(buf,&type, 10);
printf("Buf:: %s\n",buf);
>
The printf doesnt print anything but just "Buf::"
>
can anyone pls help me out resolve this???
Thanks a ton
ObCLC: You're invoking undefined behavior, anything could happen, etc.

There are a number of problems.

First, why are you copying 10 bytes? Your integer probably isn't that
large. You probably want to use sizeof(int) here instead.

Second, what kind of system are you on? If it is big-endian with 32-
bit integers, the first byte of type (which will be the first byte in
buf) would be the most-significant byte, which is 0, and acts as the
terminating null of the string.

Just what is it that you're trying to accomplish, and what do you
expect it to do? If you want to print out the character with value
0xFE, just do

printf("character looks like %c\n", 0xfe);

or even

puts("character is \xfe");
Martin Ambuhl
Guest
 
Posts: n/a
#4: Sep 5 '08

re: Problem with Char*


Ashit Vora wrote:
Quote:
I friends,
I 'm facing a weird prob.
>
the code is
No, it isn't. If anyone cares to look at the uncompilable original
lines can look at the original post.
Try compiling this and running it on your machine:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int main(void)
{
int total_buf_size = 100;
char *buf;
int type = 0xFE10;

buf = malloc(total_buf_size); /* removed silly cast */
if (!buf) { /* and added error check */
fprintf(stderr, "attempt to allocated %d bytes for\n"
"buf failed. Giving up ...\n", total_buf_size);
exit(EXIT_FAILURE);
}

/* key addition */
if (sizeof type < 10) {
fprintf(stderr,
"I'm not even going to bother with an insane attempt\n"
"to move 10 bytes from an object only %zu bytes "
"long.\n",
sizeof type);
free(buf);
exit(EXIT_FAILURE);
}
memcpy(buf, &type, 10);
printf("Buf:: %s\n", buf);
free(buf);
return 0;
}

Quote:
The printf doesnt print anything but just "Buf::"
Uncompilable code will very rarely have output that makes any sense.
Quote:
>
can anyone pls help me out resolve this???
Stop trying to move 10 bytes from something not 10 bytes long.
And learn how to spell "please".

Quote:
Thanks a ton
Closed Thread