Thread: Access violation while pushing newnode on list

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    10

    Access violation while pushing newnode on list

    Hi all,

    I have problem with pushing the node on the list.

    my program is as follows
    Code:
    struct LinkFiles 
    {
            int count;
            int index;
            wchar_t * filePath;
            wchar_t * fileName;
    };
    
    uint64_t length=0;
    						struct LinkFiles newnode ;
    						newnode.count = stats.f_nlink - 1;
    						newnode.fileIndex=index;
    						
    						newnode.fileName=(vfm_char_t*)malloc(wcslen(name.c_str())+1);
    						if (NULL != newnode.fileName)
    						{
    							wcsncpy(newnode.fileName,name.c_str(), wcslen(name.c_str())) ;
    							
    						}
    						else 
    						{
    							wprintf(L"\n\n CopyIndex : newnode.fileName is null ");
    							goto end ;
    						}
    						length = wcslen(name.c_str());
    						newnode.fileName[length]=L'\0' ;
    						
    						newnode.filePath=(vfm_char_t*)malloc(wcslen(path.c_str())+1);
    						wprintf(L"\n\n The strlen of path  is <%d> ",wcslen (path.c_str()));
    						if (NULL != newnode.filePath)
    						{
    							wcsncpy(newnode.filePath , path.c_str(),wcslen (path.c_str()));
    							
    						}
    						else 
    						{
    							wprintf(L"\n\n CopyIndex : newnode.filePath is null ");
    							goto end;
    						}
    						length=wcslen(path.c_str());
    						newnode.filePath[length]=L'\0' ;
    						try 
    						{
    							g_linkFilesList.push_back(newnode);
    						}
    						catch (...)
    						{
    							wprintf(L"\n\n CopyIndex : Error while pushing newnode on list ");
    							
    						}
    In this , i get an error "Access Violation : writing to location 0x00000000 "
    The newnode.filePath and newnode.fileName are valid .

    Please response asap.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's really hard to say in this small snippet of code.
    And since this is C++,
    struct LinkFiles
    can simply be written as
    LinkFiles
    No "struct" needed.
    Moreover, don't use malloc, use new. And why are you using WIDE pointers? Use std::wstring instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    malloc allocates bytes, wstring has 2 bytes per character. so you need to multiply your string length by sizeof( w_char)

    by I do not undestand why not to use the wstring class
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    10

    Smile

    Thanks Vart ! It works.

    Thanks Elysia ! It is my code requirement !

    Thank to all for quick reply!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  3. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  4. Random Access List
    By avalanche333 in forum C++ Programming
    Replies: 16
    Last Post: 01-22-2007, 07:29 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM