Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / questions about chars

Author
Message
RancidRat
16
Years of Service
User Offline
Joined: 19th Feb 2008
Location:
Posted: 18th Jan 2009 15:58
Hi

I was wondering what the differance was between:

A. char MYCHAR[50];
B. char* MYCHAR = new char[50];
C. char* MYCHAR;

A lot of times when I sprintf a char delclared like (A) to show a variable and then display it in the loop, I get a bufffer overrun. But when I use (B) with the same size, it works fine?
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 18th Jan 2009 16:33
A creates an array on the stack.
B is a pointer to a char, which you've pointed to an array on the heap.
C is an uninitialised pointer, that could be pointing anywhere.

If you get a buffer overrun with A, then you are getting a buffer overrun with B. It may not be quite as visible, but it's still happening. It's probably corrupting the heap structures, and may even be overwriting some of your other heap-allocated data.

Basically, fix the problem properly. I can't tell you how exactly because I see no code, but you should at least switch to using the snprintf function.

RancidRat
16
Years of Service
User Offline
Joined: 19th Feb 2008
Location:
Posted: 19th Jan 2009 07:24
Yeah thanks thats all good to know. I don't have any example that I can paste, but I do use sprintf and so generally I would do something like:



This would work great, up to a certain point, when there are a lot more things in memory, then I get an Overrun. As soon as I change it to char* Info = new char[256]; the I no longer get the overrun message. But you reckon it is still corrupting something somewhere?

I just saw that in a lot of examples people declare their chars as char* Info, or char* Info[256], even when used as a paramater. Im trying to understand it so that I can know how to manage my code better, memory wise.

Thanks
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 19th Jan 2009 16:50
Can you verify if the overrun error takes place during the sprintf() call or the dbText() call?

FWIW, char* Info should be fine if you initialize the space it points to with a new statement or point it to a char array with sufficient space.

char* Info[256] isn't workable for your purpose. It basically designates Info as an array of 256 char pointers.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Quadrazar
15
Years of Service
User Offline
Joined: 7th Jan 2009
Location: onboard the Kobayashi Maru
Posted: 19th Jan 2009 19:00 Edited at: 20th Jan 2009 00:59
[EDIT]: picture updated...
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 19th Jan 2009 19:08 Edited at: 19th Jan 2009 19:09
Quote: "But you reckon it is still corrupting something somewhere?"

You enlarged the array you are allocating - that reduces the problem, but as soon as you get a circumstance where you output 256 characters or more, you've overrun your buffer again.

If you know that your buffer is going to be 256 characters, use snprintf instead and tell it so.

Quote: "sprintf(Info,"%d",MyVar);"

Because you are passing an integer value in here, it's impossible for the output to be over 12 characters (sign, 10 digits, terminating null). The code in your second post is actually safe.

Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 19th Jan 2009 19:10
I'm not sure what that illustration is trying to demonstrate as processes have their own address space so the "used by other programs" part doesn't make so much sense.

Quadrazar
15
Years of Service
User Offline
Joined: 7th Jan 2009
Location: onboard the Kobayashi Maru
Posted: 19th Jan 2009 19:45
@benjamin:
just trying to visualise stack and heap. I'll adjust the picture and make it one address space.

I made another illustration to explane a terminating zero, should I post it or just forget about it?

Login to post a reply

Server time is: 2024-11-25 11:19:17
Your offset time is: 2024-11-25 11:19:17