We help IT Professionals succeed at work.

copying strings to a structure field

ambuli
ambuli asked
on
Hi Experts,

I have  the following in the .h file

typedef struct
{
   char *name;
   char *value;
} my_table_t;

void functionOne(my_table_t *table);


And I am doing something like below to copy some data.  I want to know if this valid or should I use strcpy to copy the values.



my_table_t table;

table.name = "nameOne";
table.value = "valueOne";


functionOne(&table);


Comment
Watch Question

ozo
SILVER EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
It can be valid, depending on what you want to do with functionOne
In particular, what might happen if you tried to write to *(table.name) or *(table.value) would be undefined.

Author

Commented:
functionOne is basically used for getting the xml message representing this C structure.  So, I am using libXML2 to get the xml messages.

So, it is used something like below.

 string_field = xmlNewChild(new_tuple, NULL, "StringField", NULL);
  xmlNewChild(string_field, NULL, "Name", "nameOne");
  xmlNewChild(string_field, NULL, "Value", table->name);

What I am trying to debug is that I am getting the following error, possibly from libXml2.  So, I am trying to see if my copying is incorrect.

xmlEscapeEntities : char out of range
ozo
SILVER EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
If you can pass "nameOne" to it, it should be equally valid to pass
table->name = "nameOne" to it.

Author

Commented:
It appears that
when I do this,

table->name contains  some trailing bytes( garbage characters...)
A bit more details.

I am getting the value from SPI_getvalue( ) from postgres database, which returns  char *.

So,

table->name = SPI_getvalue();

functionOne(&table);

...
In functionOne( )

xmlNewChild(string_field, NULL, "Value", table->name);
evilrixSenior Software Engineer (Avast)
SILVER EXPERT

Commented:
It would probably be best of you just posted the code here. Unfortunately, what the code codes and what you describe may be orthogonal. With code to view we can try to diagnose the issue rather than conjecture based upon the symptoms you describe.

-Rx.
evilrixSenior Software Engineer (Avast)
SILVER EXPERT

Commented:
s/what the code codes/what the code does
Senior Software Engineer (Avast)
SILVER EXPERT
Commented:
Incidentally, the documentation for SPO_getvalue documents two failure conditions. Do you check for both of these before attempting to pass the result to your XML function?

"Column value, or NULL if the column is null, colnumber is out of range (SPI_result is set to SPI_ERROR_NOATTRIBUTE), or no no output function available (SPI_result is set to SPI_ERROR_NOOUTFUNC). "


// Error checking, something like...
table->name = SPI_getvalue(...);
if((SPI_ERROR_NOATTRIBUTE != SPI_result) && (SPI_ERROR_NOOUTFUNC != SPI_result))
{
	functionOne(&table);;
}
else
{
	// Handle error
}

Open in new window

jkr
BRONZE EXPERT
Top Expert 2012
Commented:
You need to allocate memory for these variables, just assigning pointer values does not guarantee the validity of the data - these pointers may not necessarily point to valid memory locations later. A

#include <string.h>

table->name = strdup(SPI_getvalue());

or

table->name = _strdup(SPI_getvalue());

might help, don't forget to 'free()' the memory later.

(see also http://www.globalyzer.com/gi/help/gihelp/unsafeMethod/_strdup.htm)
evilrixSenior Software Engineer (Avast)
SILVER EXPERT

Commented:
>> You need to allocate memory for these variables, just assigning pointer values does not guarantee the validity of the data
"The result is returned in memory allocated using palloc. (You can use pfree to release the memory when you don't need it anymore.) "
http://www.postgresql.org/docs/8.2/static/spi-spi-getvalue.html

Author

Commented:
Sorry, I was tied up with something else and couldn't comment on this.  The code is pretty much identical to what I described.  It is a larger program, and I wasn't able to simplify that to post here.  Anyway, thanks for all your inputs.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.