• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 728
  • Last Modified:

copying strings to a structure field

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);


0
ambuli
Asked:
ambuli
  • 4
  • 3
  • 2
  • +1
3 Solutions
 
ozoCommented:
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.
0
 
ambuliAuthor 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
0
 
ozoCommented:
If you can pass "nameOne" to it, it should be equally valid to pass
table->name = "nameOne" to it.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ambuliAuthor 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);
0
 
evilrixSenior Software Engineer (Avast)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.
0
 
evilrixSenior Software Engineer (Avast)Commented:
s/what the code codes/what the code does
0
 
evilrixSenior Software Engineer (Avast)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

0
 
jkrCommented:
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)
0
 
evilrixSenior Software Engineer (Avast)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
0
 
ambuliAuthor 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.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now