A concatenation problem

K

Kevin Goodsell

Barry said:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
god, what's this?

Isn't it a (crude) worst case estimate of the maximum length needed to
hold the string representation of any possible integer? How big does
ch[] need to be for sprintf(ch, "%d", INT_MIN) to never overflow?

That is what the expression represents (though the two '+ 1's should
also be included - one reserves space for the null terminating
character, the other for a possible negative/minus sign).

The expression is taken from the FAQ for this group. You should be able,
with a bit of effort, to derive it yourself. The key point is that it
determines the maximum number of *octal* digits (and other characters)
needed for an int. The number of decimal (or hexadecimal) digits needed
is guaranteed to be no more than the number of octal digits needed.

-Kevin
 
C

Caroline

Thank you all for your help.
The following gave me the result I wanted on the first iteration:
char *nameOfFile;
char *ext ;
char *prefix;
sprintf(nameOfFile, "%s%d.%s", prefix, Comp, ext);
 
C

Caroline

Thank you all for your help.
The following gave me the result I wanted on the first iteration:
char *nameOfFile;
char *ext ;
char *prefix;

nameOfFile ="";
sprintf(nameOfFile, "%s%d.%s", prefix, SomeNumber, ext);
....
but when I the function executed again, similar results appeared.

1 - How can I reset the variables at each iteration?
2 - How (and why) would MALLOC be used in this case?
 
K

Kevin Goodsell

Caroline said:
Thank you all for your help.

Please stop top-posting. It's rude.
The following gave me the result I wanted on the first iteration:
char *nameOfFile;
char *ext ;
char *prefix;

nameOfFile ="";

This spells trouble later on. You should never assign a string literal
to a (non-const) pointer. String literals may not be modified, so you
should *only* use const pointers to point to a string literal.
sprintf(nameOfFile, "%s%d.%s", prefix, SomeNumber, ext);

This is a serious error that will be caught by the compiler if you
follow my advice above. nameOfFile points to a string literal, so you
are attempting to write your output over that string literal. This gives
undefined behavior. Even if string literals happen to be modifiable on
your implementation, you'll probably write past the end of the array.
...
but when I the function executed again, similar results appeared.

1 - How can I reset the variables at each iteration?

I don't know what you mean by that.
2 - How (and why) would MALLOC be used in this case?

malloc() is really only used in one way - it is called to allocate a
chunk of memory. The reason you might want to do that is because you
might only be able to determine how much memory you need at run-time
(not at compile-time).

You need an array to store the sprintf output into. You are currently
trying to use a string literal, which is not appropriate. You could
declare your array like this:

char nameOfFile[N];

But in order to do so you need to know an appropriate value for N when
you write the source. If you *don't* know how large the array needs to
be, but you can calculate it at run-time, you can do this instead:

char *nameOfFile;
size_t nameLength = /* calculate length here */;

nameOfFile = malloc(nameLength * sizeof(*nameOfFile));
if (nameOfFile != NULL)
{
sprintf(nameOfFile, "%s%d.%s", prefix, SomeNumber, ext);
}
else
{
/* handle allocation error */
}

-Kevin
 
P

Peter Pichler

Caroline said:
Thank you all for your help.


Caroline,

Please learn to post properly, starting with writing your post *below* the
post you are replying to and snipping the stuff that is not relevant.

Fortunately, nothing of that applies here because your post makes sense on
its own ;-)
The following gave me the result I wanted on the first iteration:
char *nameOfFile;
char *ext ;
char *prefix;

nameOfFile ="";
sprintf(nameOfFile, "%s%d.%s", prefix, SomeNumber, ext);

If this worked, then only purely by accident. You are causing an undefined
behaviour twice here:
1. by ovewriting a string literal ""
2. by writing beyond the space allocated for it
Please read on...
...
but when I the function executed again, similar results appeared.

1 - How can I reset the variables at each iteration?

What do you mean by "reset the variables?" If you want a new string, just
use new values.
2 - How (and why) would MALLOC be used in this case?

Hurray! I'm glad you've asked.

You need a space to write your string to. Your string (filename) will be N
bytes long, where N is strlen(prefix) + number of digits in your number + 1
for the dot + strlen(ext). You will need N+1 bytes for your string, the +1
is for a terminating 0. How you make that space is not important, you may
have an array declared somewhere, but in many cases you use malloc() to make
the space.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,129
Messages
2,570,770
Members
47,329
Latest member
FidelRauch

Latest Threads

Top