Re: [Dev-C++] Casting of int to string
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jan M. <jan...@vo...> - 2007-05-07 10:56:09
|
Oh God, thank you. It is enough for today I think.
Jan Mura
----- Original Message -----
From: "Jonathan Winterflood" <jon...@gm...>
To: "Jan Mura" <jan...@vo...>
Cc: "Per Westermark" <pw...@ia...>; "Philip Bennefall"
<phi...@pb...>; <dev...@li...>
Sent: Monday, May 07, 2007 12:32 PM
Subject: Re: [Dev-C++] Casting of int to string
> Since the name is just a pointer, the malloc does not allocate _any_
memory
> for the name, only 4 bytes(?) for the pointer.
> You should seperately allocate the memory for the name, or use another
> trick: allocte enough memory _after_ the structure to contain your name,
in
> the same malloc (this saves allocating lots of small blocks, in the case
> that the name for that structure will not change)
> eg:
> member = malloc(sizeof(struct bet_list)+length_of_name_including_nul);
> //allocate both
> member->name = (char *)member + sizeof(struct bet_list); // point the
name
> at the memory after the structure
> strcpy(member->name, name);
>
> It might look slightly hacky, but it works fine :)
> Any comments on this?
>
> Obviously if you dno't know the size of the string before allocatingthe
> struct, or it may change, you'll have to allocate them separately:
>
> member = malloc(sizeof(struct bet_list)); //allocate just one
> member->name=NULL; // for checking: malloc does not initialize data (I
guess
> you already know that)
> member->next = NULL;
> /* ... */
> if (member->name) {
> free(member->name); //in case you are changing names
> member->name=NULL; // if you aren't going to put the name back
immediately
> }
> member->name = malloc(strlen(name)+1); //allocate the name
> strcpy(member->name, name);
>
> Jonathan
>
> On 5/7/07, Jan Mura <jan...@vo...> wrote:
> >
> >
> > Thank you, it came to my mind what about the NULL character at the end
of
> > the string.
> >
> > Maybe one more thing. I am not sure if my code is ok because of the
> > similar
> > reasons about some illegal memory usage.
> > So I have something like
> >
> > struct bet_list
> > {
> > int id;
> > char *name;
> > struct bet_list *next;
> > };
> > ...
> > struct bet_list *member, *temp;
> >
> > member = malloc(sizeof(struct bet_list));
> >
> > member->id = 1;
> > strcpy(member->name, "some string here");
> > member->next = NULL;
> >
> > ...
> >
> > I am not sure if malloc allocates enough space if it dont know how long
is
> > a
> > string I would like to put in.
> >
> > Thanks again
> >
> > Jan Mura
> >
> > ---- Original Message -----
> > From: "Per Westermark" <pw...@ia...>
> > To: "Philip Bennefall" <phi...@pb...>
> > Cc: "Jonathan Winterflood" <jon...@gm...>; "Jan Mura"
> > <jan...@vo...>; <dev...@li...>
> > Sent: Monday, May 07, 2007 10:21 AM
> > Subject: Re: [Dev-C++] Casting of int to string
> >
> >
> > > Better C libraries have the snprintf() function, that takes the size
of
> > > the buffer as an input parameter.
> > >
> > > /pwm
> > >
> > > On Mon, 7 May 2007, Philip Bennefall wrote:
> > >
> > > > Sorry, I should've phrased it differently. Yup, allocating too
little
> > memory is a bad idea in any situation because the function has no way of
> > knowing when it is supposed to stop writing. I just put 32 in there
> > because
> > it ensures (100 %) that the entire value will be stored including the
Null
> > terminator.
> > > >
> > > > Regards
> > > > Philip Bennefall
> > > > ----- Original Message -----
> > > > From: Jonathan Winterflood
> > > > To: Jan Mura
> > > > Cc: dev...@li...
> > > > Sent: Monday, May 07, 2007 9:08 AM
> > > > Subject: Re: [Dev-C++] Casting of int to string
> > > >
> > > >
> > > > Just a note:
> > > > """ It basically prints your integer into the character array, as
> > long
> > as there is enough space to hold it """
> > > > Actually it will print the integer whether there is enough space
or
> > not.. It is entirely Your job to make sure there is enough space, or
else
> > sprintf will just keep on printing in memory after the end of the array.
> > Obviously this is a very bad idea, since there is other data there.
> > > > In this case, determining the necessary space is as easy as
'what's
> > the maximum digit count in an integer'... but in other cases, you can
get
> > wierd crashes, wrong data for no apparent reason, or even create an
> > exploit
> > in your program.
> > > >
> > > > Thought you might want to know that (and if you did, no offense
> > intended :)
> > > > Jonathan
> > > >
> > > >
> > > > On 5/7/07, Philip Bennefall < phi...@pb...>
wrote:
> > > > This will do the job.
> > > > char outputstring[32];
> > > > sprintf(outputstring, "%d", integer);
> > > >
> > > > It basically prints your integer into the character array, as
long
> > as there
> > > > is enough space to hold it. This method works both in C and C++.
> > There are
> > > > other methods such as stringstream, but they only work in C++.
If
> > you're
> > > > compiling a C program, for this function to work you need to
> > include
> > stdio.h
> > > > and for C++ it is cstdio.
> > > >
> > > > Regards
> > > > Philip Bennefall
> > > > ----- Original Message -----
> > > > From: "Jan Mura" <jan...@vo...>
> > > > To: <dev...@li... >
> > > > Sent: Monday, May 07, 2007 8:26 AM
> > > > Subject: [Dev-C++] Casting of int to string
> > > >
> > > >
> > > > > Hello,
> > > > >
> > > > > I would like to compare an integer with a string using
strcmp().
> > So I need
> > > > > to cast the int type to string (*char). Is there some commone
> > way
> > how to
> > > > > do
> > > > > it. Should I use & operator and adress of int in teh
function?
> > Or
> > > > > something
> > > > > else?
> > > > >
> > > > > Thank you very much
> > > > >
> > > > > Jan Mura
> > > > >
> > > > >
> > > >
> >
> >
> >
> > >
>
> -------------------------------------------------------------------------
> > > > > This SF.net email is sponsored by DB2 Express
> > > > > Download DB2 Express C - the FREE version of DB2 express and
> > take
> > > > > control of your XML. No limits. Just data. Click to get it
now.
> > > > > http://sourceforge.net/powerbar/db2/
> > > > > _______________________________________________
> > > > > Dev-cpp-users mailing list
> > > > > Dev...@li...
> > > > > TO UNSUBSCRIBE:
http://www23.brinkster.com/noicys/devcpp/ub.htm
> > > > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> > > > >
> > > >
> > > >
> > >
> >
>
-------------------------------------------------------------------------
> > > > This SF.net email is sponsored by DB2 Express
> > > > Download DB2 Express C - the FREE version of DB2 express and
take
> > > > control of your XML. No limits. Just data. Click to get it now.
> > > > http://sourceforge.net/powerbar/db2/
> > > > _______________________________________________
> > > > Dev-cpp-users mailing list
> > > > Dev...@li...
> > > > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> > > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> > > >
> > > >
> > > >
> > > >
> > > > --
> > > > <Morpheus> linux, c'est une question de VI ou de MORE
> > > >
> > > >
> > >
> > >
>
> --------------------------------------------------------------------------
> > ----
> > > >
> > > >
> > >
> >
>
-------------------------------------------------------------------------
> > > > This SF.net email is sponsored by DB2 Express
> > > > Download DB2 Express C - the FREE version of DB2 express and take
> > > > control of your XML. No limits. Just data. Click to get it now.
> > > > http://sourceforge.net/powerbar/db2/
> > > >
> > > >
> > >
> > >
>
> --------------------------------------------------------------------------
> > ----
> > > >
> > > >
> > > > _______________________________________________
> > > > Dev-cpp-users mailing list
> > > > Dev...@li...
> > > > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> > > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> > > >
> > >
> > >
> > >
> > > __________ Informace od NOD32 2245 (20070506) __________
> > >
> > > Tato zprava byla proverena antivirovym systemem NOD32.
> > > http://www.nod32.cz
> > >
> > >
> >
> >
>
>
> --
> <Morpheus> linux, c'est une question de VI ou de MORE
>
>
>
> __________ Informace od NOD32 2246 (20070507) __________
>
> Tato zprava byla proverena antivirovym systemem NOD32.
> http://www.nod32.cz
>
|