Stack and heap are all just memory inside that 4-gigabyte array of bytes.
I repeat:
Quote:
Originally Posted by Corona688
I repeat: You've written your program around assumptions which don't hold water -- like the very idea of "str3" being a separate entity from "str1" and "str2" just because you threw pointers to the same memory into a different function.
Fixing it to the point it will work isn't a matter of finding the bit which is "stopping it from working". The fundamental design of your program is flawed, and will continue to be flawed, even if we correct the obvious things.
I will rewrite some of it instead. Give me some time.
Since VM can be mapped anywhere, they started by putting the environment, code, constants, initialized vaiables and unitialized variables in the bottom of memory, the heap, and the stack is started at top top of memory. growing toward each other. I think signal flags and open files are really in the kernel, and the fd is just an offset into a kernel per-process array of pointers to open files, so many processes can have the same open file on one or more fd numbers. The loader puts the arguments' pointers in an array with a terminating null pointer and starts up main(). If you wrote in C++, static objects' constructors would run before main(). Suppose you call strcmp(x,y). The code will push the adresses of x and y into the stack, and maybe allow a return value space, and call the strcpy code. Often values pushed are promoted to 4 byte integer even if short or char. If strcpy has automatic variables, they are located below the stack pointer setting after the call arguments. For calls within calls, each set of arguments and auto variables is called a stack frame. So, even though nobody else knows the address of a subroutine's static variables, the compiler/linker knows. The subroutine could pass a pointer to it, or load a global pointer with its address, to make it visible to others. Some even return it, which is not usual since it might not be MT-Safe! For instance, if you return a char* of a static char[50] with a null terminated string in it, unless it is const, someone might rewrite it, and if it is a product of the call input arguments, the next call will change the value, usually to something the first recipient of the pointer did not ask for. Some of the time libraries are like this, and have newer, safe variations.
This is very succinct code, and worked like charm. Thank you very much!
Questions: 1) As no malloc() was used to allocate memory for out, str1 and str2 in your strmerg() function. Is it because they are all declared const char* ?
2) In main() char out[4096] was used to hold the merged string, in practice, string 1 and string 2 can be as large as mega-bases. I thought dynamic allocation of the memory for out using a pointer would be better. It seems there is no such thing in C to dynamically allocate space for the merged string based on the two inputs (???). Can I ask if if there is anything inappropriate with my modification on main() function?
Thanks a lot!
Questions: 1) As no malloc() was used to allocate memory for out, str1 and str2 in your strmerg() function. Is it because they are all declared const char* ?
Look closer, they're not all const char *.
It's nothing to do with malloc.
The two which are 'const char *' are set that way because I don't need to write to their contents.
The one which isn't 'const char *' is because I need to write to its contents.
I could have made them all plain 'char *' and it would have still worked. The 'const' is a reminder to me, the programmer, of which arguments I should be writing to and which I shouldn't. It's also a safety mechanism, so if I try to cram unwritable things into it, the compiler will complain.
Quote:
2) In main() char out[4096] was used to hold the merged string, in practice, string 1 and string 2 can be as large as mega-bases. I thought dynamic allocation of the memory for out using a pointer would be better. It seems there is no such thing in C to dynamically allocate space for the merged string based on the two inputs (???).
...and having said so, you go ahead and do the "impossible" in the same breath
I avoided malloc because pointers still confuse you. I just thought it was more straightforward to do it that way.
Quote:
Can I ask if if there is anything inappropriate with my modification on main() function?[/COLOR]
Actually that looks fine. It allocates more memory than it needs to so isn't ideal, but will do what you want it to do, and won't crash.
Here's what I would do:
Last edited by Corona688; 04-04-2014 at 02:13 PM..
Here is a strmerg() similar to Corona688's. But, where his version checks for a match for every trailing substring of string 1; this version starts with the longest possible match based on the length of the shorter string and stops as soon as it finds a match. It also verifies that malloc() succeeded before copying data into the buffer allocated by malloc(). With multi-megabyte input strings of similar length and short matches, the speed difference is likely to be unnoticeable. But if there are frequent relatively long matching strings or if string 1 is longer than string 2, this version might be significantly faster.
This won't work on some older systems, because it uses <inttypes.h> to produce relatively portable printf() conversion specifications for objects of type size_t. And, Corona688's code makes much better use of the const qualifier, than this code does.
When compiled and linked into a.out, the command:
produces:
and the command:
produces the output:
These 2 Users Gave Thanks to Don Cragun For This Post:
I have a bi-lingual database of a large number of dictionaries. It so happens that in some a given string is in upper case and in others it is in lower case. An example will illustrate the issue.
toll Tax=पथ-कर
Toll tax=राहदारी कर
toll tax=टोल
I want to treat all three instances of toll tax... (3 Replies)
I am preparing a morphological grammar of Marathi to be placed in open-source.
I have two files.
The first file called Adverbs contains a whole list of words, one word per line
A sample is given below:
आधी
इतक
इतपत
उलट
एवढ
ऐवजी
कड
कडनं
कडल
कडील
कडून
कडे
करता
करिता
खाल (2 Replies)
Perl script to merge cells
---------- Post updated at 12:59 AM ---------- Previous update was at 12:54 AM ----------
I am using below code to read files from a dir and print to excel.
open(my $in, '<', $file) or die "Could not open file: $!";
my $rowCount = 0;
my $colCount = 0;... (11 Replies)
Hello,
I am working on an Urdu to Hindi dictionary which has the following structure:
a=b
a=c
n=d
n=q
and so on.
i.e. Headword separated from gloss by a =
I am giving below a live sample
بتا=बता
بتا=बित्ता
بتا=बुत्ता
بتان=बतान
بتان=बितान
بتانا=बिताना
I need the following... (3 Replies)
Dears my rootvg is missed up i can not extend the /opt
as soon as i try to extend the Filesystem its give me that there is not enough space .
as there any way to change the REGION of the LVs in HDISK1 ?
lspv -p hdisk0
hdisk0:
PP RANGE STATE REGION LV NAME TYPE ... (8 Replies)
Hi,
I would like to know how can I get the ID of a feature if its genomic coordinates overlap the coordinates of another file. Example:
Get the 4th column (ID) of this file1:
chr1 10 100 gene1
chr2 3000 5000 gene2
chr3 200 1500 gene3
if it overlaps with a feature in this file2:
chr2... (1 Reply)
How can I find the regions between specific lines?
I have a file which contains lines like this:
chr1 0 17388 0
chr1 17388 17444 1
chr1 17444 17599 2
chr1 17599 17601 1
chr1 17601 569791 0
chr1 569791 569795 1
chr1 569795 569808 2
chr1 569808 569890 3
chr1 569890 570047 4 ... (9 Replies)
Hello all,
I have a question about what you think the best practice is to determine what region you are running on when you have a system setup with a DEV/TEST, QA, and PROD regions running the same scripts in all.
So, when you run in DEV, you have a different directory structure, and you... (4 Replies)
str1="this oracle data base record"
str2="one two three four five"
Output:
this one
oracle two
data three
base four
record five
str1 and str2 have the same column but they are not fixed columns.
I can do it with "paste" but I do not want to create file everytime the
script runs from... (2 Replies)