ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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:
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:
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:
I have to do this instead:
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...