Hello, I wrote a function called eat_path() to split a string into
components
e.g. /a/b/c ==> namePtr[0] = a,namePtr[1] = a, namePtr[2] = c
// Global variable
char *namePtr[100] = {0};
int n; /*number of components*/
The function is like this:
void eat_path(char *pathname)
{
printf("Enter eat_path\n");
//eat_path() breaks up a pathname into component strings.
//Example : pathname = /this/is/a/test
//Then n=4, namePtr[0] ="this", namePtr[1] ="is", namePtr[2] ="a",
//namePtr[3] ="test"
//The component names will be used to search for a child under its
parent
char path[40];
char delim[] = "/"; /* delimiter is '/' */
int i = 0, j = 0;
strcpy(path, pathname);
namePtr[j++] = strtok( path, delim );
while(namePtr[j++] = strtok( NULL,delim ));
j--;
n = j;
printf("\nNumber of components: %d\n", n);
for(i = 0; i < n; i++)
{
printf("%d %s\n", i, namePtr[i]);
}
printf("eat_path ends\n");
}
It works fine when I printed the namePtr inside this function, but
when I print namePtr in another function. it prints out garbage...
This is another function that calls eat_path:
NODE *namei(char *pathname)
{
NODE *ret;
int i = 0;
//namei() returns the node pointer of a pathname, or 0;
//First, call eat_path() to break up pathname into component strings.
printf("In namei(), pathname = %s\n", pathname);
eat_path(pathname);
printf("After eat_path: In namei(), pathname = %s\n", pathname);
for(int j = 0; j < n; j++)
printf("%d %s\n", j, namePtr[j]);
//For each component string, call search_child() to look for the
child
//under its parent.
if (pathname[0] == '/')
ret = root;
else
ret = cwd;
while (ret != 0 && i < n)
{
//printf("namePtr[%d] = %s\n", i, namePtr[i]);
ret = search_child(ret, namePtr[i]);
i++;
}
return ret;
}
I checked the content of namePtr right after calling eat_path, so the
namePtr should contain the same thing when the program is still inside
eat_path...
I am so lost... thank you for helping.. 2 2347
"Estella" <es*********@hotmail.com> wrote in message
news:50**************************@posting.google.c om... Hello, I wrote a function called eat_path() to split a string into components e.g. /a/b/c ==> namePtr[0] = a,namePtr[1] = a, namePtr[2] = c
// Global variable char *namePtr[100] = {0};
There is no such thing as a Global variable.
While ``Global'' refers to scope, it is incorrect
to denote the variable as such. Its scope is
file-scope and its linkage is external. What
is generally referred to as ``global'' is the
appearance given when a particular
variable is used in more than one translation
unit. ``Global'' refers to scope but ``extern''
refers to linkage -- which does not affect scope,
and only when a local declaration is provided in
another translation unit may one be able to give
the appearance that the scope of a particular
variable is Global. However, the term
``Global variable'' in regards to how the abstract
machine defined by the C standard operates,
is a misnomer because it is linkage which acts
as the enabler of said appearance and not any
other mechanism.
int n; /*number of components*/
The function is like this: void eat_path(char *pathname) { printf("Enter eat_path\n"); //eat_path() breaks up a pathname into component strings. //Example : pathname = /this/is/a/test //Then n=4, namePtr[0] ="this", namePtr[1] ="is", namePtr[2] ="a", //namePtr[3] ="test" //The component names will be used to search for a child under its parent char path[40]; char delim[] = "/"; /* delimiter is '/' */ int i = 0, j = 0;
strcpy(path, pathname);
Your problem is that the scope of ``path'' is
local to ``eat_path'' and has automatic storage
duration which means that the value it represents
is not meaningful after ``eat_path'' returns.
In function ``namei'', when you call ``eat_path''
and after ``eat_path'' returns, you rely on an object
whose value is indeterminate and upon using it
invoke undefined behaviour.
--
j
On 5 Sep 2004 16:22:06 -0700, es*********@hotmail.com (Estella) wrote: Hello, I wrote a function called eat_path() to split a string into components e.g. /a/b/c ==> namePtr[0] = a,namePtr[1] = a, namePtr[2] = c
// Global variable char *namePtr[100] = {0}; int n; /*number of components*/
The function is like this: void eat_path(char *pathname) { printf("Enter eat_path\n"); //eat_path() breaks up a pathname into component strings. //Example : pathname = /this/is/a/test //Then n=4, namePtr[0] ="this", namePtr[1] ="is", namePtr[2] ="a", //namePtr[3] ="test" //The component names will be used to search for a child under its parent char path[40];
path exists only for the life of eat_path.
char delim[] = "/"; /* delimiter is '/' */ int i = 0, j = 0;
strcpy(path, pathname);
namePtr[j++] = strtok( path, delim );
Each non-NULL element of namePtr points to some part of path. while(namePtr[j++] = strtok( NULL,delim ));
j--;
n = j;
printf("\nNumber of components: %d\n", n);
for(i = 0; i < n; i++) { printf("%d %s\n", i, namePtr[i]);
path still exists at this point so the values in namePtr point to an
existing object.
} printf("eat_path ends\n"); }
It works fine when I printed the namePtr inside this function, but when I print namePtr in another function. it prints out garbage...
This is another function that calls eat_path: NODE *namei(char *pathname) { NODE *ret; int i = 0;
//namei() returns the node pointer of a pathname, or 0; //First, call eat_path() to break up pathname into component strings. printf("In namei(), pathname = %s\n", pathname); eat_path(pathname); printf("After eat_path: In namei(), pathname = %s\n", pathname); for(int j = 0; j < n; j++) printf("%d %s\n", j, namePtr[j]);
path no longer exists because eat_path has finished. The values in
namePtr now point to a non-existent object and any attempt to
dereference those values invokes undefined behavior.
snip
<<Remove the del for email>> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: R. Rajesh Jeba Anbiah |
last post by:
I'm supposed to do a big portal with multi-language support and I'm
decided to do my own instead of going for gettext. I have sort out few
implementations; most of them uses global variables. I'm...
|
by: Mark Shelor |
last post by:
I've encountered a troublesome inconsistency in the C-language Perl
extension I've written for CPAN (Digest::SHA). The problem involves the
use of a static array within a performance-critical...
|
by: Paminu |
last post by:
In math this expression:
(a < b) && (b < c)
would be described as:
a < b < c
But why is it that in C these two expressions evaluate to something
different for the same values of a, b and...
|
by: ern |
last post by:
I'm using strtok( ) to capture lines of input. After I call
"splitCommand", I call strtok( ) again to get the next line. Strtok( )
returns NULL (but there is more in the file...). That didn't...
|
by: Sandro Dentella |
last post by:
I'd like to understand why += operator raises an error while .append() does
not. My wild guess is the parses treats them differently but I cannot
understand why this depends on scope of the...
|
by: Robbie Hatley |
last post by:
A couple of days ago I dedecided to force myself to really learn
exactly what "strtok" does, and how to use it. I figured I'd
just look it up in some book and that would be that.
I figured...
|
by: Michael |
last post by:
Hi,
I have a proble I don't understand when using strtok(). It seems that if I
make a call to strtok(), then make a call to another function that also
makes use of strtok(), the original call is...
|
by: karthikbalaguru |
last post by:
Hi,
Memory allocated in heap remains until the end of the program.
So, global variables & static variables are allocated on heap.
In the same time, I find that BSS also allows the placement of...
|
by: krista |
last post by:
Hi,
I am beginner of C++. When I try to read a file and put it into X and
Y variables. I got that segmentation fault.
the data file is
data.txt:
4.4 6.8
3.2 -5.5
3.3 0.9
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |