char In structures

D

duramax

Hi,
I'm getting some weird results for the following code:

#include "stdafx.h"
#include <stdlib.h>
typedef struct ALLO
{
char a[3];
char b[4];
} Allo;

int _tmain(int argc, _TCHAR* argv[])
{
Allo al;
strcpy(al.a, "aloe");
strcpy(al.b, "vcxzv");
return 0;
}

I get an error "stack around arround the 'al' variable was corrupted". I
think that strcpy in this case isn't good but I can't realy tell... If it is
how could I set my char in my structure else than with strcpy?

TIA, Max.
 
D

Daniel T.

duramax said:
Hi,
I'm getting some weird results for the following code:

#include "stdafx.h"
#include <stdlib.h>
typedef struct ALLO
{
char a[3];
char b[4];
} Allo;

int _tmain(int argc, _TCHAR* argv[])
{
Allo al;
strcpy(al.a, "aloe");
strcpy(al.b, "vcxzv");
return 0;
}

I get an error "stack around arround the 'al' variable was corrupted". I
think that strcpy in this case isn't good but I can't realy tell... If it is
how could I set my char in my structure else than with strcpy?

strcpy works. Given that fact where do you think the problem might be?
Let's look at what strcpy does:

char* strcpy( char* out, const char* in ) {
char* result = out;
while ( *in ) {
*out++ = *in++
}
*out = 0;
return result;
}

The block of memory pointed to by 'out' is 3 bytes, while the block of
memory pointed to by 'in' is 5 bytes. See the problem yet?
 
J

John Carson

duramax said:
Hi,
I'm getting some weird results for the following code:

#include "stdafx.h"
#include <stdlib.h>
typedef struct ALLO
{
char a[3];
char b[4];
} Allo;

This typedef is necessary in C but not in C++. You can just make it

struct Allo
{
char a[3];
char b[4];
};

int _tmain(int argc, _TCHAR* argv[])
{
Allo al;
strcpy(al.a, "aloe");
strcpy(al.b, "vcxzv");
return 0;
}

I get an error "stack around arround the 'al' variable was
corrupted". I think that strcpy in this case isn't good but I can't
realy tell... If it is how could I set my char in my structure else
than with strcpy?


"aloe" has 4 characters plus a terminating nul character. Thus you need 5
characters to store it. al.a only has three. The problem with the second
strcpy is similar.
 
J

JKop

"aloe"

char ape[5] = { 'a', 'l', 'o', 'e', '\0' };


"vcxzv"

char monkey[6] = { 'v', 'c', 'x', 'z', 'v', '\0' };


-JKop
 
G

Gianni Mariani

duramax said:
Hi,
I'm getting some weird results for the following code:

#include "stdafx.h"
#include <stdlib.h>
typedef struct ALLO
{
char a[3];
char b[4];
} Allo;

int _tmain(int argc, _TCHAR* argv[])
{
Allo al;
strcpy(al.a, "aloe");
strcpy(al.b, "vcxzv");
return 0;
}

I get an error "stack around arround the 'al' variable was corrupted". I
think that strcpy in this case isn't good but I can't realy tell... If it is
how could I set my char in my structure else than with strcpy?

strcpy uses "C" strings where a string as a "nul" terminating character.

Hence, "aloe" is actually a string of length 5 !

i.e.
#include <iostream>
int main()
{
std::cout << sizeof( "aloe" );
}

will print 5

However you have allocated only 3 char for ALLO::a and 4 char's for
ALLO::b yet are trying to copy 5 and 6 char arrays into them !

Try this instead


int main()
{

::memcpy( al.a, "aloe", sizeof( al.a ) );
::memcpy( al.b, "vcxzv", sizeof( al.b ) );
}

But now you have the problem of possibly trying to copy more data than
there is - e.g.

::memcpy( al.b, "v", sizeof( al.b ) );

In the memcpy above, unpredictable things *may* happen because you may
be accessing memory that is unallocated (bytes after the "v" may or may
not be valid memory).

BTW - the _TCHAR and _tmain stuff is non-standard and I'd steer away
from it.
 

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

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,463
Latest member
FinleyMoye

Latest Threads

Top