3

Context: I'm trying to do is to make a program which would take text as input and store it in a character array. Then I would print each element of the array as a decimal. E.g. "Hello World" would be converted to 72, 101, etc.. I would use this as a quick ASCII2DEC converter. I know there are online converters but I'm trying to make this one on my own.

Problem: how can I allocate an array whose size is unknown at compile-time and make it the exact same size as the text I enter? So when I enter "Hello World" it would dynamically make an array with the exact size required to store just "Hello World". I have searched the web but couldn't find anything that I could make use of.

Cœur
39k25 gold badges206 silver badges282 bronze badges
asked Apr 1, 2012 at 8:38
3
  • 2
    What programming language? :) Commented Apr 1, 2012 at 8:40
  • I'm trying to do this in C :) Commented Apr 1, 2012 at 8:57
  • Glad that your problem is solved! You should now accept a correct answer, and optionally vote up/down as you wish. For more info see meta.stackexchange.com/a/5235 and stackoverflow.com/faq#howtoask Commented Apr 1, 2012 at 21:41

4 Answers 4

2

I see that you're using C. You could do something like this:

 #define INC_SIZE 10
 char *buf = (char*) malloc(INC_SIZE),*temp;
 int size = INC_SIZE,len = 0;
 char c;
 while ((c = getchar()) != '\n') { // I assume you want to read a line of input
 if (len == size) {
 size += INC_SIZE;
 temp = (char*) realloc(buf,size);
 if (temp == NULL) {
 // not enough memory probably, handle it yourself
 }
 buf = temp;
 }
 buf[len++] = c;
 }
 // done, note that the character array has no '0円' terminator and the length is represented by `len` variable
answered Apr 1, 2012 at 9:03
Sign up to request clarification or add additional context in comments.

8 Comments

But what if I enter text that is 7 chars long and the buffer was allocated to 10? How would you make an array that is as long as the text you input.
Seriously? Just shorten it afterwards. Have you done any programming before? Is this homework?
@Tuntuni: Simply add realloc(buf,len); at the end of the while loop.
@LeleDumbo: One more question, is the cast to a char* really required after calling malloc()? Thanks
Not to my knowledge, just a self ensuring behavior. the compiler won't complain though, not even a warning even with -Wall with gcc.
|
0

Typically, on environments like a PC where there are no great memory constraints, I would just dynamically allocate, (language-dependent) an array/string/whatever of, say, 64K and keep an index/pointer/whatever to the current end point plus one - ie. the next index/location to place any new data.

answered Apr 1, 2012 at 8:53

1 Comment

Could you give me an example of how you would do this in C? I'm really lost. :/
0

I'm going to guess you mean C, as that's one of the commonest compiled languages where you would have this problem.

Variables that you declare in a function are stored on the stack. This is nice and efficient, gets cleaned up when your function exits, etc. The only problem is that the size of the stack slot for each function is fixed and cannot change while the function is running.

The second place you can allocate memory is the heap. This is a free-for-all that you can allocate and deallocate memory from at runtime. You allocate with malloc(), and when finished, you call free() on it (this is important to avoid memory leaks).

With heap allocations you must know the size at allocation time, but it's better than having it stored in fixed stack space that you cannot grow if needed.

This is a simple and stupid function to decode a string to its ASCII codes using a dynamically-allocated buffer:

char* str_to_ascii_codes(char* str)
{
 size_t i;
 size_t str_length = strlen(str);
 char* ascii_codes = malloc(str_length*4+1);
 for(i = 0; i<str_length; i++)
 snprintf(ascii_codes+i*4, 5, "%03d ", str[i]);
 return ascii_codes;
}

Edit: You mentioned in a comment wanting to get the buffer just right. I cut corners with the above example by making each entry in the string a known length, and not trimming the result's extra space character. This is a smarter version that fixes both of those issues:

char* str_to_ascii_codes(char* str)
{
 size_t i;
 int written;
 size_t str_length = strlen(str), ascii_codes_length = 0;
 char* ascii_codes = malloc(str_length*4+1);
 for(i = 0; i<str_length; i++)
 {
 snprintf(ascii_codes+ascii_codes_length, 5, "%d %n", str[i], &written);
 ascii_codes_length = ascii_codes_length + written;
 }
 /* This is intentionally one byte short, to trim the trailing space char */
 ascii_codes = realloc(ascii_codes, ascii_codes_length);
 /* Add new end-of-string marker */
 ascii_codes[ascii_codes_length-1] = '0円';
 return ascii_codes;
}
answered Apr 1, 2012 at 9:24

2 Comments

Thanks. :) Is the cast to char* really needed after calling malloc()? This is not related to your example, I just want to know if it is really needed or not. :) Thanks again!
It's not necessary to cast a void*, and some actively discourage it. Wikipedia has a good overview of the argument for and against casting mallocs: en.wikipedia.org/wiki/Malloc#Type_safety
-1

if you use cpp language, you can use the string to store the input characters,and access the character by operator[] , like the following codes:

std::string input;
cin >> input; 
answered Apr 1, 2012 at 8:56

1 Comment

As the question is clearly about C rather than C++ and much better answers exist, this should probably be deleted.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.