extern char *calargv[] not having values populated earlier.

Y

yogeshmk

I've a problem with array[global] values getting lost in function call.
Here is the flow.

===========driver.c=================
/* this is just a driver to test a library wrapper
* originally it's a cobol program calling a c library function
*/

# include "il_entry.h" /* # define s SCANFUN as il_lib_entry */

int
main(void)
{
int SLEN = 8192;
char SIGNAT[SLEN];
char ISTAT[2]={'0', '0'};
char FNAME[32] = {'0'};

SCANFUN(ILREADIMAGE, FNAME, SIGNAT, "8192", ISTAT);
return 0;
}

=============== il_lib_entry.c =================================

# include <stdarg.h>
# include <stdlib.h>

# define NARGS 10 /* keeps track of memory allocated
*/

char **calargv;
int calargc;

void il_cob(void);


void
il_lib_entry(char *foo, ...)
{
int i=0, n=0;
va_list argv;
char *v;

if (foo != NULL)
{
n = NARGS;
/* start with 10 ptrs first */
if ((calargv = malloc (NARGS * sizeof(char*))) == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}

va_start(argv, *foo);

printf("%s\n", foo);
if((calargv[0] = malloc (strlen(foo)+1)) == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
strcpy(calargv[0], foo);

for (i=1; (v=va_arg(argv, char*)) != NULL; i++)
{
/* if (i > n)
{
n += NARGS;
calargv = realloc(calargv, n * sizeof(char*));
}
*/
if((calargv = malloc(strlen(v)+1)) == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
strcpy(calargv, v);
}

/* if (i > n)
{
n++;
calargv = realloc(calargv, n * sizeof(char*));
}
*/
calargv = NULL; /* terminate the
array */

va_end(argv);
calargc = i;

il_cobol(); /* join the old route */

for (i=0; calargv; i++)
free(calargv);

free(calargv);
}
}

==================== library code =================
#include <stdio.h>
#include <fcntl.h>
#include "il_lib.h"
#include "il_cob_lib.h"

extern char *calargv[] ;
extern int calargc ;

static int iretval;

#define NFUN sizeof(il_cob_call)/sizeof(int *)

il_cobol()
{
if (calargc > 0) {
int ifun = atoi(calargv[0]);
iretval = 0;
if(ifun < NFUN)
(*il_cob_call[ifun])();
}
return;
}
<snip>
===================== code end ========================

When control enters the library code, the contents of calargv[] are
already corrupt. (Though calargc is correct). Appears that values of
calargv[0] to [4] are changing somewhere after il_lib_entry calls
il_cobol. Can't understand why.

Should calargv be declared as const in il_lib_entry? but I'm confused
as to what should be const in the declaration.

il_lib_entry is the wrapper to the library which alredy existing, so
any change in library code is ruled out, but il_lib_entry() can be
changed to suit.

help appreciated.

~yogesh
 
E

Eric Sosman

yogeshmk wrote On 08/01/06 11:14,:
I've a problem with array[global] values getting lost in function call.
Here is the flow.

One of the problems (there are probably several, but
it's hard to be sure lacking so much of the actual code)
arises from this mismatch:
=============== il_lib_entry.c =================================
[...]
char **calargv;
==================== library code =================
[...]
extern char *calargv[] ;

A pointer-to-something ("something" is a char* in this
case) is not the same type as an array-of-something, so the
same object is being described in two conflicting ways. See
Question 6.1 in the comp.lang.c Frequently Asked Questions
(FAQ) list

http://www.c-faq.com/
 
T

trm

yogeshmk said:
When control enters the library code, the contents of calargv[]
are already corrupt. (Though calargc is correct). Appears that
values of calargv[0] to [4] are changing somewhere after
il_lib_entry calls il_cobol. Can't understand why.

Are you sure they were populated correctly to begin with?
The code as posted doesn't do anything to verify this.
void
il_lib_entry(char *foo, ...) [snip]
va_start(argv, *foo);

I don't know what dereferencing will do in this context, but
it's almost certainly something bad.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top