ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
First it creates input, an array of strings. Next I get the input and assign them all to input. I removed some pieces of code but that's more or less it.
The strlower(char *) function just converts the string to lowercase. Anyway, the problem is, when I put printf("%s\n", a) anywhere in the mysortcmp method, I get garbage data full of symbols instead of the string I want to be compared.
However, if I did not dynamically allocate input, such that the readinput method is like this:
it would work great and there aren't any problems. But I don't think that would really be the solution for me. I was wondering what I should be doing.
Should I
A) Create my own sort algorithm?
B) Use qsort properly for dynamically allocated arrays, if you could please show me how?
C) Do something else?
(And btw, on my
Code:
fgets(buf,80,file);
// sscanf(buf,"%s",input[i]);
I was wondering if there is a better way of doing something similar to fgets without taking along the \n character, but still getting whitespaces.)
To store a string, say "hello", you need 6 elements. One for each character plus the '\0'. However, strlen("hello") only returns 5. So you're not allocating enough memory and your strcpy()'s are overflowing the buffers.
I'm actually kind of surprised that this even compiles:
Code:
char alow[strlen(a)];
char blow[strlen(b)];
Creating an array on the stack like this generally requires a constant value for the size. (e.g. a value known at compile time so that the compiler knows the proper amount of memory to allocate on the stack..)
Originally posted by jim mcnamara The C99 standard allows for dynamically sized arays in functions other than main() when the size of the array is dictated by an argument.
They are called variable-length arrays. That said, I'm not sure if they work in the context of using strlen().
Cool. I learn something new all the time. Has gcc just recently implemented this standard? I don't know that I've ever actually tried to do this in gcc because I knew that you usually need a constant array size on the stack, but I could swear I have seen posts here in the past of people complaining because they got errors from similar code.
As a side note, I just tested it out in gcc/g++ and it seemed to work fine. However, it appears that VC++ .Net 2003 still reports a compiler error. I know that MS has made huge strides in improving ANSI compliance since VC++ 6.0, but evidently this is one area where it still is lacking.
Originally posted by deiussum As a side note, I just tested it out in gcc/g++ and it seemed to work fine. However, it appears that VC++ .Net 2003 still reports a compiler error. I know that MS has made huge strides in improving ANSI compliance since VC++ 6.0, but evidently this is one area where it still is lacking.
I'm working on a string class in VS.NET and came across the same 'problem'. I am using C++, not C, but I can't use variable length arrays in any of my class methods. Its not really an issue, but I did notice I can't do this:
Code:
char tmp[strlen(class_member)+1];
I have to do this instead:
Code:
char * tmp = new char[strlen(class_member)+1];
Just thought I'd point it out. I didn't really take a look at the main question for the thread, but I wonder if finding the buffer problems helped find the answer?
EDIT: BTW I'm using VS.NET 2002, not 2003 or the newer 2005 beta. They might have fixed it in these versions.
Let's say I fixed that using char * and then malloc on it, putting printf for a and b (and that is also before using alow and blow) still give me garbage strings. I was wondering if everything else is correct such that the only real problem is that the compiler I'm using does not support dynamically allocated arrays for qsort, or the arguments I used for qsort are incorrect?
I took a brief look at this for you, and after printing out memory addresses it appears that when you use the dynamic array method, qsort is actually sending you a char** to the sorting method. Try the following:
Your mysortcmp gets parameters whose addresses are
input, input + x, input + x*2, .. input + x*(count-1)
Those addresses themselves are just the address locations for another char* in the case where you are using dynamically allocated arrays.
It looks like you probably already understand the fundamental difference between a char** and a char[][], but just in case... The char[][] is all going to be one contiguous block of memory, where as the char** is going to be a pointer to an array of pointers, and thus, the memory for all the underlying char data is not contiguous...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.