473,382 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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[i] = malloc(strlen(v)+1)) == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
strcpy(calargv[i], v);
}

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

va_end(argv);
calargc = i;

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

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

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

Aug 1 '06 #1
2 1331


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/

--
Er*********@sun.com

Aug 1 '06 #2
trm
yogeshmk schrieb:
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.

Aug 1 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Newsgroup - Ann | last post by:
Hi gurus, I have two files compiled together. One file has the following global definition: char s = "asdf"; Another file has the following declaration: extern char *s;
4
by: exits funnel | last post by:
Hello, I have the following three files //BEGIN test.h class foo { public: void print( ); private:
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
16
by: Martin Jørgensen | last post by:
Hi, Problem: ======== Some of my output functions are beginning to take pretty many arguments... I mean.... We're talking about 10-15 arguments :-) So I thought to myself, perhaps this is...
7
by: ruffiano | last post by:
I have a file called first.cpp that contains the following declarations: static const char *MY_STRING1 = "My first string"; static const char *MY_STRING2 = "My second string" If I wanted to use...
3
by: coder | last post by:
While reading the page on "Reading C Declarations" <http://www.ericgiguere.com/articles/reading-c-declarations.html> (recommended by Mr. Heathfield at...
2
by: Bharath | last post by:
Hi All, I am a newbie to C++ and I'm trying to figure this out. Can you please help? We have 3rd party library that's written in c++. We don't have the source for it. ldd on that shared...
17
by: dtschoepe | last post by:
Hi, I have a homework project I am working on, so be forwarned, I'm new to C programming. But anyway, having some trouble with a memory allocation issue related to a char * that is a variable...
21
by: Christian Meier | last post by:
Hello NG I have the following code: file1.h: static const int iValue = 5; <EOF>
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.