Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C problem struct and string

Status
Not open for further replies.

lazyfat

Technical User
May 9, 2009
4
CA
Hi guys,

This is my first post and I'm hoping the experts here can help me solve my problem.

- I'm trying to create a linked list in C and the codes look like this:

#include "stdio.h"
#include <stdlib.h>
#include <string>
using namespace std;

struct dir_list
{
int level;
string dir_name;
struct dir_list * head;
struct dir_list * tail;
};

typedef struct dir_list * dir;

void main()
{
dir curr;

curr=(dir)malloc(sizeof(string)+sizeof(dir));
curr->level=0;
curr->dir_name="test ";
curr->head=NULL;
curr->tail=NULL;
}

The problem with this code happens at the string assignment curr->dir_name="test "; and the executable will stop responding once run. I suspect the problem could be caused by the size of malloc but the result stays the same after I increased that number.

Can anyone explain to me what the problem is?

Greatly appreciated!
 
try this
Code:
[COLOR=#a020f0]#include [/color][COLOR=#ff00ff]"stdio.h"[/color]
[COLOR=#a020f0]#include [/color][COLOR=#ff00ff]<stdlib.h>[/color]
[COLOR=#a020f0]#include [/color][COLOR=#ff00ff]<string>[/color]
[COLOR=#804040][b]using[/b][/color] [COLOR=#2e8b57][b]namespace[/b][/color] std;

[COLOR=#2e8b57][b]struct[/b][/color] dir_list
{
    [COLOR=#2e8b57][b]int[/b][/color] level;
    string dir_name;
    [COLOR=#2e8b57][b]struct[/b][/color] dir_list * head;
    [COLOR=#2e8b57][b]struct[/b][/color] dir_list * tail;
};

[COLOR=#2e8b57][b]typedef[/b][/color] [COLOR=#2e8b57][b]struct[/b][/color] dir_list dir;

[COLOR=#2e8b57][b]int[/b][/color] main()
{
    dir *curr;

    curr=(dir *)malloc([COLOR=#804040][b]sizeof[/b][/color](dir));
    curr->level=[COLOR=#ff00ff]0[/color];
    curr->dir_name=[COLOR=#ff00ff]"test "[/color];
    curr->head=[COLOR=#ff00ff]NULL[/color];
    curr->tail=[COLOR=#ff00ff]NULL[/color];
    [COLOR=#804040][b]return[/b][/color] [COLOR=#ff00ff]0[/color];
}
 
Same :(
if I comment out the string assignment no error will pop-out.
 
It's strange, when I compiled your original source and tried it to run then the executable stopped to respond as you said, but now when I take this source /I added only one printf() line at the end/
lst.cpp
Code:
[COLOR=#a020f0]#include [/color][COLOR=#ff00ff]"stdio.h"[/color]
[COLOR=#a020f0]#include [/color][COLOR=#ff00ff]<stdlib.h>[/color]
[COLOR=#a020f0]#include [/color][COLOR=#ff00ff]<string>[/color]
[COLOR=#804040][b]using[/b][/color] [COLOR=#2e8b57][b]namespace[/b][/color] std;

[COLOR=#2e8b57][b]struct[/b][/color] dir_list
{
    [COLOR=#2e8b57][b]int[/b][/color] level;
    string dir_name;
    [COLOR=#2e8b57][b]struct[/b][/color] dir_list * head;
    [COLOR=#2e8b57][b]struct[/b][/color] dir_list * tail;
};

[COLOR=#2e8b57][b]typedef[/b][/color] [COLOR=#2e8b57][b]struct[/b][/color] dir_list dir;

[COLOR=#2e8b57][b]int[/b][/color] main()
{
    dir *curr;

    curr=(dir *)malloc([COLOR=#804040][b]sizeof[/b][/color](dir));
    curr->level=[COLOR=#ff00ff]0[/color];
    curr->dir_name=[COLOR=#ff00ff]"test "[/color];
    curr->head=[COLOR=#ff00ff]NULL[/color];
    curr->tail=[COLOR=#ff00ff]NULL[/color];
    printf([COLOR=#ff00ff]"that was ..."[/color]);
    [COLOR=#804040][b]return[/b][/color] [COLOR=#ff00ff]0[/color];
}
and I compile and run it, it runs to end without stopping to respond - look:
Code:
$ g++ lst.cpp -o lst

$ lst
that was ...
You compiled probably the old source once again.
What compiler are you using?

 
I'm using Visual C++ compiler.
I guess I should try other compilers like yours and see the difference.

Anyway thanks for your time mikrom!
 
I tried it in Visual C++ as win32 console application.
These approaches seems to work:
lst01.cpp
Code:
[COLOR=#0000ff]// lst.cpp : Defines the entry point for the console application.[/color]
[COLOR=#0000ff]//[/color]

[COLOR=#a020f0]#include [/color][COLOR=#ff00ff]"stdafx.h"[/color]
[COLOR=#a020f0]#include [/color][COLOR=#ff00ff]"stdio.h"[/color]
[COLOR=#a020f0]#include [/color][COLOR=#ff00ff]<stdlib.h>[/color]
[COLOR=#a020f0]#include [/color][COLOR=#ff00ff]<string>[/color]
[COLOR=#804040][b]using[/b][/color] [COLOR=#2e8b57][b]namespace[/b][/color] std;

[COLOR=#2e8b57][b]struct[/b][/color] dir_list
{
    [COLOR=#2e8b57][b]int[/b][/color] level;
    [COLOR=#2e8b57][b]char[/b][/color] dir_name[[COLOR=#ff00ff]80[/color]];
    [COLOR=#2e8b57][b]struct[/b][/color] dir_list * head;
    [COLOR=#2e8b57][b]struct[/b][/color] dir_list * tail;
};

[COLOR=#2e8b57][b]typedef[/b][/color] [COLOR=#2e8b57][b]struct[/b][/color] dir_list dir;

[COLOR=#2e8b57][b]int[/b][/color] main([COLOR=#2e8b57][b]int[/b][/color] *argv, [COLOR=#2e8b57][b]char[/b][/color] **argc)
{
    dir *curr;

    curr=(dir *)malloc([COLOR=#804040][b]sizeof[/b][/color](dir));
    curr->level=[COLOR=#ff00ff]0[/color];
    strcpy(curr->dir_name,[COLOR=#ff00ff]"test"[/color]);
    curr->head=[COLOR=#ff00ff]NULL[/color];
    curr->tail=[COLOR=#ff00ff]NULL[/color];
    printf([COLOR=#ff00ff]"that was ...[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]);
    [COLOR=#804040][b]return[/b][/color] [COLOR=#ff00ff]0[/color];
}
or
lst02.cpp
Code:
[COLOR=#0000ff]// lst.cpp : Defines the entry point for the console application.[/color]
[COLOR=#0000ff]//[/color]

[COLOR=#a020f0]#include [/color][COLOR=#ff00ff]"stdafx.h"[/color]
[COLOR=#a020f0]#include [/color][COLOR=#ff00ff]"stdio.h"[/color]
[COLOR=#a020f0]#include [/color][COLOR=#ff00ff]<stdlib.h>[/color]
[COLOR=#a020f0]#include [/color][COLOR=#ff00ff]<string>[/color]
[COLOR=#804040][b]using[/b][/color] [COLOR=#2e8b57][b]namespace[/b][/color] std;

[COLOR=#2e8b57][b]struct[/b][/color] dir_list
{
    [COLOR=#2e8b57][b]int[/b][/color] level;
    string *dir_name;
    [COLOR=#2e8b57][b]struct[/b][/color] dir_list * head;
    [COLOR=#2e8b57][b]struct[/b][/color] dir_list * tail;
};

[COLOR=#2e8b57][b]typedef[/b][/color] [COLOR=#2e8b57][b]struct[/b][/color] dir_list dir;

[COLOR=#2e8b57][b]int[/b][/color] main([COLOR=#2e8b57][b]int[/b][/color] *argv, [COLOR=#2e8b57][b]char[/b][/color] **argc)
{
    dir *curr;

    curr=(dir *)malloc([COLOR=#804040][b]sizeof[/b][/color](dir));
    curr->level=[COLOR=#ff00ff]0[/color];
    curr->dir_name = [COLOR=#804040][b]new[/b][/color] string([COLOR=#ff00ff]"test"[/color]);
    curr->head=[COLOR=#ff00ff]NULL[/color];
    curr->tail=[COLOR=#ff00ff]NULL[/color];
    printf([COLOR=#ff00ff]"that was ...[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]);
    [COLOR=#804040][b]return[/b][/color] [COLOR=#ff00ff]0[/color];
}

For explanation read this
 
Umm... Is this supposed to be a C assignment or a C++ assignment? This is mostly C++ code, but you posted in the C forum.
If it's C++, then get rid of all the malloc()/Free() calls and replace them with new/delete, replace printf() with cout, replace strcpy() with the proper std::string member functions, and use the proper C++ versions of the C headers (i.e. <cstdlib> instead of <stdlib.h>)... although once you get rid of malloc(), you won't need <cstdlib> anymore anyways.
If it's supposed to be C, then get rid of all the C++ stuff.
 
it's supposed to be C assignment but i had trouble differentiating them until yesterday.
My problem is now solved and thanks for all the helps!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top